TTMSFNCCloudAI
Usage
A component that provides access to several LLM AI services. The context can be sent to the service with an optional system and/or assistant role and the service returns its response.
At this moment, there is support for OpenAI, Grok, Gemini, Claude, Perplexity and Ollama.
The LLM AI service API keys are set via TTMSFNCCloudAI.APIKeys
Properties
Property name |
Description |
APIKeys |
Class holding the API keys for the different supported AI services. |
Busy |
A boolean property that is true while the REST API request to the service is running. |
Context |
A stringlist holding multiline context information for which the LLM should respond. |
Service |
Selects the LLM AI service to use. The choices are aiOpenAI, aiGemini, aiGrok, aiClaude, aiPerplexity, aiOllama. |
Settings |
Class holding additional settings for the different AI services. This includes the model name & temperature for all services, the model for the Perplexity service. For the Ollama service, the host & port for the Ollama server to use is provided. |
Methods
Method name |
Description |
Execute(id: string = '') |
Call to start the request to the LLM AI service. |
Events
Event name |
Description |
OnExecuted |
Event triggered when the LLM AI service returned its response containing the response details. |
The response object TTMSFNCCloudAIResponse
Property name |
Description |
Id: string |
The Id that was used as parameter for the Execute() call. |
CompletionTokens |
The number of tokens that the response contains. |
Content: TStrings |
The LLM response as text. |
PromptTokens |
The number of tokens taken by the LLM prompt (i.e. the Context). |
ServiceId: string |
Contains the unique response Id returned by the service itself |
ServiceModel: string |
Contains the exact model name the LLM service used |
TotalTokens |
The total number of tokens used by the LLM request. |
Sample
The request:
TMSFNCCloudAI.Context.Text := 'What do you know about Delphi?';
TMSFNCCloudAI.OnExecuted := Executed;
TMSFNCCloudAI.Execute;
The response handler:
procedure Executed(Sender: TObject; AResponse: TTMSFNCCloudAIResponse; AHttpStatusCode: Integer;
AHttpResult: string);
begin
if Assigned(AResponse) then
begin
memo.Lines.Text := AResponse.Content.Text;
label.Caption := 'Prompt tokens:' + AResponse.PromptTokens.ToString+' / CompletionTokens:' + AResponse.CompletionTokens.ToString+ ' / TotalTokens:'+AResponse.TotalTokens.ToString;
end
else
ShowMessage('Error with HTTP status code:' + AHttpStatusCode.ToString)
end;