Use ChatGPT from Delphi
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 here: https://beta.openai/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
- uses
- System.JSON, VCL.TMSFNCCloudBase;
- function AskChatGPT(AQuestion: string): string;
- var
- LCb: TTMSFNCCloudBase;
- LPostdata: string;
- LJsonValue: TJsonValue;
- LJsonArray: TJsonArray;
- LJSonString: TJsonString;
- begin
- Result := '';
- LPostData := '{' +
- '"model": "text-davinci-003",'+
- '"prompt": "' + AQuestion + '",'+
- '"max_tokens": 2048,'+
- '"temperature": 0'+
- '}';
- // create instance of TMS FNC Cloud Base class
- LCb := TTMSFNCCloudBase.Create;
- try
- // Use JSON for the REST API calls and set API KEY via Authorization header
- LCb.Request.AddHeader('Authorization','Bearer ' + CHATGPT_APIKEY);
- LCb.Request.AddHeader('Content-Type','application/json');
- // Select HTTPS POST method, set POST data and specify endpoint URL
- LCb.Request.Method := rmPOST;
- LCb.Request.PostData := LPostData;
- LCb.Request.Host := 'https://api.openai';
- LCb.Request.Path := 'v1/completions';
- // Execute the HTTPS POST request synchronously (last param Async = false)
- LCb.ExecuteRequest(nil,nil,false);
- // Process returned JSON when request was successful
- if Lcb.RequestResult.Success then
- begin
- LJsonValue := TJSonObject.ParseJSONValue(Lcb.RequestResult.ResultString);
- LJsonValue := LJsonValue.GetValue<TJSonValue>('choices');
- if LJsonValue is TJSonArray then
- begin
- LJSonArray := LJsonValue as TJSonArray;
- LJSonString := LJSonArray.Items[0].GetValue<TJSONString>('text');
- Result := LJSonString.Value;
- end
- else
- end
- else
- raise Exception.Create('HTTP response code: ' + LCb.RequestResult.ResponseCode.ToString);
- finally
- LCb.Free;
- end;
- end;
With this code, using ChatGPT from a Delphi app becomes as simple as:
view plain text
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- Memo1.Lines.Text := AskChatGPT(Edit1.Text);
- 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?
- 亚马逊迫于微软竞争压力调低云服务价格
- 位图BITMAP结构
- 什么是AQS?AQS有什么作用?常见的基于AQS的组件有哪些?
- 如何评测软件系统的安全性
- Java后端学习路线图
- js callee与caller 区别
- 【转载】深入浅出的讲解傅里叶变换
- 【数字图像处理】秒懂傅里叶变换,仅需此文
- 困扰的Byte、bit 、和16进制之间的关系
- 详解Node.js API系列 Module模块(2) 案例分析
- 如何进行自媒体创业?你是否能把握住,短视频都有哪些变现方式?
- 如何刷PTA,达到PTA甲级、乙级、顶级应具备的能力
- 云端漫步
- javascript call用法及好处
- ros端口转发
- Maven仓库的使用
- 用Python从零复现A星寻路算法