Use ChatGPT from Delphi

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

For a couple of weeks now, ChatGPT is the talk of the town.

In record time, millions of users signed up and tested the new AI NLP service from OpenAI and are amazed by the answers for questions it comes up with.

For us Delphi developers, it's even more fun having the ability to go a step further and automate sending questions and getting answers from a Delphi app. This is possible thanks to the ChatGPT REST API.

 

We'd liked to present simple code here that shows how this can be done in any type of Delphi app (or even Lazarus Object Pascal app) on any platform. To ensure this code can be used in Windows, macOS, iOS, Android, Linux apps, we opted to use the TTMSFNCCloudBase class from TMS FNC Core. This offers a rich interface to perform REST API requests from any platform and any framework. To get started with the OpenAI ChatGPT API, request your API key herehttps://beta.openai.com/account/api-keys 

The code to ask questions and get answer from ChatGPT from a Delphi app is with TMS FNC TTMSCloudBase as simple as:

 

view plain text

  1. uses  
  2.   System.JSON, VCL.TMSFNCCloudBase;  
  3.   
  4. function AskChatGPT(AQuestion: string): string;  
  5. var  
  6.   LCb: TTMSFNCCloudBase;  
  7.   LPostdata: string;  
  8.   LJsonValue: TJsonValue;  
  9.   LJsonArray: TJsonArray;  
  10.   LJSonString: TJsonString;  
  11. begin  
  12.   Result := '';  
  13.   
  14.   LPostData := '{' +  
  15.     '"model": "text-davinci-003",'+  
  16.     '"prompt": "' + AQuestion + '",'+  
  17.     '"max_tokens": 2048,'+  
  18.     '"temperature": 0'+  
  19.     '}';  
  20.   
  21.   // create instance of TMS FNC Cloud Base class  
  22.   LCb := TTMSFNCCloudBase.Create;  
  23.   
  24.   try  
  25.     // Use JSON for the REST API calls and set API KEY via Authorization header  
  26.     LCb.Request.AddHeader('Authorization','Bearer ' + CHATGPT_APIKEY);  
  27.     LCb.Request.AddHeader('Content-Type','application/json');  
  28.   
  29.     // Select HTTPS POST method, set POST data and specify endpoint URL  
  30.     LCb.Request.Method := rmPOST;  
  31.     LCb.Request.PostData := LPostData;  
  32.     LCb.Request.Host := 'https://api.openai.com';  
  33.     LCb.Request.Path := 'v1/completions';  
  34.   
  35.     // Execute the HTTPS POST request synchronously (last param Async = false)  
  36.     LCb.ExecuteRequest(nil,nil,false);  
  37.   
  38.     // Process returned JSON when request was successful   
  39.     if Lcb.RequestResult.Success then  
  40.     begin  
  41.       LJsonValue := TJSonObject.ParseJSONValue(Lcb.RequestResult.ResultString);  
  42.       LJsonValue := LJsonValue.GetValue<TJSonValue>('choices');  
  43.       if LJsonValue is TJSonArray then  
  44.       begin  
  45.         LJSonArray := LJsonValue as TJSonArray;  
  46.         LJSonString := LJSonArray.Items[0].GetValue<TJSONString>('text');  
  47.         Result := LJSonString.Value;  
  48.       end  
  49.       else  
  50.     end  
  51.     else  
  52.       raise Exception.Create('HTTP response code: ' + LCb.RequestResult.ResponseCode.ToString);  
  53.   finally  
  54.     LCb.Free;  
  55.   end;  
  56. end;  

With this code, using ChatGPT from a Delphi app becomes as simple as:

view plain text

  1. procedure TForm1.Button1Click(Sender: TObject);  
  2. begin  
  3.   Memo1.Lines.Text := AskChatGPT(Edit1.Text);  
  4. end;  

When you want to use this in a FireMonkey cross-platform app, all you need to do is change in the uses list VCL.TMSFNCCloudBase to FMX.TMSFNCCloudBase. Or when you want to use this from Lazarus, change the unit name to LCLTMSFNCCloudBase.

Note that we do not mention TMS WEB Core here that TMS FNC also supports. This is because OpenAI specifies that the API key cannot be used in a web client application where this key would be visible to anyone.

You can get the app full source to replicate this test from Github. Make sure to download & install TMS FNC Core as well.

While playing a little bit with ChatGPT from the Delphi app, here are some of the answers it came up with:
 

And this was a question asked in connection with our TMS VCL TAdvStringGrid component and surprisingly, the answer is pretty spot-on and accurate. Who knows that shortly ChatGPT will make our support engineers redundant? :)

We are curious to hear what you think about ChatGPT, how you envision using it in your Delphi apps or business in general?

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: ChatGPT