OpenAI is a research organization that develops and publishes machine learning models and tools. You can use OpenAI’s models and tools in Delphi by using the OpenAI API, which allows you to access the models over the web and use them to perform various tasks such as language translation, text generation, and image recognition.
To use the OpenAI API in Delphi, you will need to obtain an API key from OpenAI. You can then use the IdHTTP component from the Indy library to send HTTP requests to the OpenAI API and receive responses.
Here is an example of how you could use the OpenAI API to generate text using the GPT-3 model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
uses IdHTTP, System.JSON; const API_KEY = ‘your-api-key-here’; API_URL = ‘httpsapi.openai.comv1textcompletions’; var http: TIdHTTP; response: TStringStream; params: TStringList; json: TJSONObject; text: String; begin http := TIdHTTP.Create(nil); try response := TStringStream.Create; try params := TStringList.Create; try params.Add(‘api_key=’ + API_KEY); params.Add(‘model=text-davinci-002’); params.Add(‘prompt=The quick brown fox jumps over the lazy dog’); params.Add(‘max_tokens=100’); http.Post(API_URL, params, response); response.Position = 0; json = TJSONObject.ParseJSONValue(response.DataString) as TJSONObject; try text := json.Values[‘text’].Value; finally json.Free; end; finally params.Free; end; finally response.Free; end; finally http.Free; end; end; |
his example sends an HTTP/POST request to the OpenAI API with the specified parameters, and parses the JSON response to retrieve the generated text.
You can find more information about the OpenAI API and how to use it in the documentation at https://beta.openai.com/docs .
Leave a Reply