admin管理员组文章数量:1336660
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?
版权声明:本文标题:Use ChatGPT from Delphi 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1742419863a2471440.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论