OpenAI API permite build your own AI Chats using ChatGPT Turbo. Using um biblioteca sgcWebSockets is muito fácil para interactuate com o API, given um chat conversation, o model will return um chat completion response.
ChatGPT Exemplo Delphi
OpenAI requer para build um request were you pass o messages para sent para ChatGPT Turbo, o temperature (para obter um more ore less random output... veja abaixo uma lista de o available parâmetros.
- model: (Required) ID do model para usar. See o model endpoint compatibility table para details em which models work com o Chat API.
- messages: (Required) The messages para generate chat completions for, em o chat format.
- temperature: What sampling temperature para usar, entre 0 e 2. Higher values like 0.8 will make o output more random, while lower values like 0.2 will make it more focused e deterministic.
- top_p: An alternative para sampling com temperature, chamado nucleus sampling, where o model considers o results do tokens com top_p probability mass. So 0.1 means somente o tokens comprising o top 10% probability mass are considered.
- n: How many chat completion choices para generate para each input message.
- stream: If set, partial message deltas será sent, like em ChatGPT. Tokens será sent como data-somente server-sent events como they become available, com o stream terminated por um data: [DONE] message. See um OpenAI Cookbook por exemplo code.
- parar: Up para 4 sequences where um API will parar generating further tokens.
- max_tokens: The maximum number de tokens para generate em o chat completion. The total length de input tokens e generated tokens is limited por o model's context length.
- presence_penalty: Number entre -2.0 e 2.0. Positive values penalize new tokens baseado em whether they appear no text so far, increasing o model's likelihood para talk about new topics.
- frequency_penalty: Number entre -2.0 e 2.0. Positive values penalize new tokens baseado em their existing frequency no text so far, decreasing o model's likelihood para repeat o same line verbatim.
- logit_bias: Modificar o likelihood de specified tokens appearing no completion. Accepts um json object that maps tokens (specified por their token ID no tokenizer) para um associated bias value um partir de -100 para 100. Mathematically, o bias is added para o logits generated por o model prior para sampling. The exact effect will vary per model, but values entre -1 e 1 should decrease ou increase likelihood de selection; values like -100 ou 100 should result em um ban ou exclusive selection do relevant token.
- user: A unique identifier representing your end-user, which can help OpenAI para monitor e detect abuse.
Veja abaixo um simples exemplo sending uma mensagem to ChatGPT-Turbo.
procedure SendMessageChatGPT(const aMessage: string);
var
i: Integer;
oMessages: TsgcOpenAIArray_Request_Completion_Messages;
oMessage: TsgcOpenAIClass_Request_Completion_Message;
oRequest: TsgcOpenAIClass_Request_ChatCompletion;
oResponse: TsgcOpenAIClass_Response_ChatCompletion;
begin
oRequest := TsgcOpenAIClass_Request_ChatCompletion.Create;
Try
// ... model
oRequest.Model := 'gpt-3.5-turbo';
// ... create message
oMessage := TsgcOpenAIClass_Request_Completion_Message.Create;
oMessage.Content := aMessage;
oMessages := oRequest.Messages;
SetLength(oMessages, 1);
oMessages[0] := oMessage;
oRequest.Messages := oMessages;
// ... send message
oResponse := OpenAI.CreateChatCompletion(oRequest);
// ... process response
for i := 0 to Length(oResponse.Choices) - 1 do
DoLog('[' + oResponse.Choices[i]._Message.Role + '] ' + oResponse.Choices[i]._Message.Content);
Finally
oRequest.Free
End;
End;

Veja abaixo o compiled Demo para Windows using sgcWebSockets OpenAI Delphi Library.
