Please enable JavaScript to view this site.

Codolex

The AI Chat activity lets you connect to the open AI api to make use of Chat GTP in your codolex project.

Uses System.Net.HttpClientComponent.TNetHTTPClient

 

OpenAIChat

Activity properties

 

begin

 ...

  try

     NetClient := THTTPClient.Create;

     NetClient.CustHeaders.Add('Authorization', 'Bearer ' + 'YourApiKey');

     NetClient.ContentType := 'application/json';

     

    var Response := NetClient.Post('https://api.openai.com/v1/chat/completions', JsonStream);

     

    var IsSuccessful := (Response.StatusCode > 199) and (Response.StatusCode < 300);

    if not IsSuccessful then

      raise ENetException.CreateFmt('%d %s%s%s', [Response.StatusCode, Response.StatusText, sLineBreak, Response.ContentAsString]);

     

     JsonResponse := TJSONValue.ParseJSONValue(Response.ContentAsString);

     

    var ChoiceJson :=  JsonResponse.GetValue<TJSONArray>('choices');

    var MessageJson := ChoiceJson[0].GetValue<TJsonObject>('message');

     

     ChatMessage := JsonAdapter.CreateEntity;

     JsonAdapter.MapToEntity(MessageJson, ChatMessage);

  ...

end;

Part of resulting Code

 

More information about the API:

https://openai.com/api/

 

The model can be set to one of the 5 options, Choosing the right model for your needs and token usage is recommended.

gpt-4o (default)

gpt-4o-mini

gpt-4-turbo

gpt-4

gpt-3.5-turbo

 

The role can also be selected, depending on the type of conversation needed.

more about roles: https://platform.openai.com/docs/guides/chat-completions/overview

 

Provide a list of messages if you want to keep a conversation instead of just one question. this needs to be a list of the plugin datasource entity 'ChatMessage'.

Keeping track of the conversation is important for asking follow-up questions, so the AI knows what you are talking about.

The question and response messages are directly added in the activity.

 

'Chat message' and 'Authorization key' are string fields that can be filled with the expression editor.

 

© by GDK Software