Does anyone have a working example where a connection is made with chatgpt, a question is asked and a response successfully returned.
I have tried with both indy and ipworks components. I get a variety of errors.
Here are 2 IPworks functions:
Unit ads.ChatGpt;
Interface
Function ChatWithGPT(Const question, Api_Key: String): String;
Function ChatWithGPT2(Const question, Api_Key: String): String;
Implementation
Uses
ipwcore,
ipwhttp,
ipwrest,
ipwtypes,
System.SysUtils;
Function ChatWithGPT(Const question, Api_Key: String): String;
Var
http : TIpwHttp;
response: String;
Begin
http := TIpwHttp.Create(Nil);
Try
http.AddCookie('Authorization', 'Bearer ' + Api_Key);
http.Get('https://api.openai.com/v1/chatgpt/chat?question=' + question);
response := http.TransferredData;
Result := response;
Finally
http.Free;
End;
End;
Function ChatWithGPT2(Const question, Api_Key: String): String;
Var
rest : TipwREST;
response: String;
Begin
rest := TipwREST.Create(Nil);
Try
rest.ContentType := 'application/json';
rest.OtherHeaders := 'Authorization: Bearer ' + Api_Key;
rest.PostData :=
'{\"model\": \"text-davinci-003\", \"prompt\": \"" + question + "\", \"temperature\": 0, \"max_tokens\": 64}';
rest.Post('https://api.openai.com/v1/completions');
rest.XPath := '/json/choices/[1]/text';
response := rest.XText.Replace('\"', '');
Result := response;
Finally
FreeAndNil(rest);
End;
End;
End.
Richard Maley