Quantcast
Viewing all articles
Browse latest Browse all 1006

Use ChatGTP from Delphi



Image may be NSFW.
Clik here to view.
TMS Software Delphi  Components


For a couple of weeks now, ChatGTP 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 ChatGTP 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 ChatGTP API, request your API key here: https://beta.openai.com/account/api-keys 


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

uses
  System.JSON, VCL.TMSFNCCloudBase;

function AskChatGTP(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 ' + CHATGTP_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 ChatGTP from a Delphi app becomes as simple as:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Text := AskChatGTP(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 ChatGTP from the Delphi app, here are some of the answers it came up with:

Image may be NSFW.
Clik here to view.
TMS Software Delphi  Components

Image may be NSFW.
Clik here to view.
TMS Software Delphi  Components


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 ChatGTP will make our support engineers redundant? :)

Image may be NSFW.
Clik here to view.
TMS Software Delphi  Components


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









Viewing all articles
Browse latest Browse all 1006

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>