Using ChatGPT from Delphi

ChatGPT is a large language model developed by OpenAI. It was trained using a technique called unsupervised learning, which means that it was fed a large dataset of text and learned to predict the next word in a sequence based on the words that came before it. This allows ChatGPT to generate human-like text, which can be used for a variety of purposes, such as generating responses to user input in a chatbot or generating content for websites and social media.

One of the key features of ChatGPT is its ability to understand context and generate appropriate responses based on the conversation. For example, if you ask ChatGPT a question, it will generate a response that is relevant to the question and that follows the natural flow of conversation. This is made possible by the model's ability to "remember" previous statements in the conversation and use that information to inform its response.

Another important feature of ChatGPT is its ability to generate diverse and varied responses. Because it was trained on a large dataset of text, it has a wide range of knowledge and can generate responses that are appropriate for different situations and topics. This makes it a useful tool for generating responses to user input in a chatbot, as it can provide relevant and engaging responses regardless of the topic of conversation.

Overall, ChatGPT is a powerful language model that can generate human-like text and understand context and conversation flow. It has a wide range of applications, including chatbots, content generation, and language translation, and has the potential to revolutionize the way we interact with technology.

ChatGPT currently has not API, but you can use OpenAI beta API to interactuate with GPT-3 Text transformer, you only need an API key, which can be obtained from:

https://beta.openai.com/account/api-keys

To start to interact with the API.

GPT-3 Delphi Code

Once you have an API key, interacting with the API is very easy. Find below a sample code to interact using the sgcWebSockets library. 

function AskChatGPT(const aAPI, aQuestion: string): string;
var
  oHTTP: TsgcHTTP1Client;
  oStream: TStringStream;
  vPostData: string;
  oJSON: TJSONValue;
  oArray: TJSonArray;
begin
  oHTTP := TsgcHTTP1Client.Create(nil);
  Try
    oHTTP.Request.CustomHeaders.Add('Authorization: Bearer ' + aAPI);
    oHTTP.Request.ContentType := 'application/json';
    vPostData :=
      Format('{"model": "text-davinci-003","prompt": "%s","max_tokens": 2048,"temperature": 0}',
      [aQuestion]);

    // send request
    oStream := TStringStream.Create(vPostData);
    Try
      result := oHTTP.Post('https://api.openai.com/v1/completions', oStream);
      // parse response
      oJSON := TJSonObject.ParseJSONValue(result).GetValue<TJSONValue>
        ('choices');
      result := TJSonArray(oJSON).Items[0].GetValue<TJSONString>('text').Value;
    Finally
      oStream.Free;
    End;
  Finally
    oHTTP.Free;
  End;
end; 

The function has 2 arguments:

API: is the API key obtained from https://beta.openai.com/account/api-keys

Question: is the message with the question.

The API key is sent as a Bearer Token and the message is encoded using a JSON object. The server returns a response as a JSON object where the text message can be found.


Find below the full project and the compiled executable for Windows. 

chatgpt_esegece
6 mb
×
Stay Informed

When you subscribe to the blog, we will send you an e-mail when there are new updates on the site so you wouldn't miss them.

sgcWebSockets 2023.1
Updated Telegram Libraries for Delphi, CBuilder & ...

Related Posts