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.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:
- 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.com';
- 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:
- 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?
相关文章
- 国内“谁”能实现chatgpt,短期穷出的类ChatGPT简评(算法侧角度为主),以及对MOSS、ChatYuan给出简评,一文带你深入了解宏观技术路线。
- ChatGPT集成之前,让我们复习一下即将过时的知识
- 火爆全网的ChatGPT上手体验,免费使用次数!国内就能用
- 别只骂谷歌Bard了,ChatGPT加持的微软New Bing也错误频出
- 微软杀疯了!全家桶嵌入ChatGPT,云平台Azure强到发指
- 点石成金!ChatGPT爆火,英伟达搭便车,老黄身价飞涨30亿
- FreeBuf 周报 | 再度思考CSMA;ChatGPT 或带来新的网络攻击
- [ChatGPT]一文读懂GPT-4!
- Chatgpt使用指南(如何高效合理的使用ChatGPT)
- ChatGPT修bug横扫全场,准确率达78%!网友:程序员要开心了
- [ChatGPT]SpringBoot快速接入ChatGpt
- ChatGPT滑铁卢:大面积封号;遭意大利封禁
- 意大利宣布禁用ChatGPT,限制OpenAI处理本国用户信息
- 科普文:国内ChatGPT怎么用,ChatGPT国内怎么用,关于ChatGPT你需要了解的内容应该都在这
- 巧用 ChatGPT,让开发者的学习和工作更轻松
- ChatGPT及大模型专题研讨会在蓉成功举办共探前沿技术与产业发展