Recent

Author Topic: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?  (Read 10412 times)

elender

  • New Member
  • *
  • Posts: 35
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #45 on: January 06, 2023, 01:28:05 pm »
This Github repository has a Delphi example:

https://github.com/fpiette/DelphiChatGPT



Use ChatGPT from Delphi https://www.tmssoftware.com/site/blog.asp?post=1045

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

Code: Pascal  [Select][+][-]
  1. uses
  2.   System.JSON, VCL.TMSFNCCloudBase;
  3.  
  4. function AskChatGPT(AQuestion: string): string;
  5. var
  6.   LCb: TTMSFNCCloudBase;
  7.   LPostdata: string;
  8.   LJsonValue: TJsonValue;
  9.   LJsonArray: TJsonArray;
  10.   LJSonString: TJsonString;
  11. begin
  12.   Result := '';
  13.  
  14.   LPostData := '{' +
  15.     '"model": "text-davinci-003",'+
  16.     '"prompt": "' + AQuestion + '",'+
  17.     '"max_tokens": 2048,'+
  18.     '"temperature": 0'+
  19.     '}';
  20.  
  21.   // create instance of TMS FNC Cloud Base class
  22.   LCb := TTMSFNCCloudBase.Create;
  23.  
  24.   try
  25.     // Use JSON for the REST API calls and set API KEY via Authorization header
  26.     LCb.Request.AddHeader('Authorization','Bearer ' + CHATGPT_APIKEY);
  27.     LCb.Request.AddHeader('Content-Type','application/json');
  28.  
  29.     // Select HTTPS POST method, set POST data and specify endpoint URL
  30.     LCb.Request.Method := rmPOST;
  31.     LCb.Request.PostData := LPostData;
  32.     LCb.Request.Host := 'https://api.openai.com';
  33.     LCb.Request.Path := 'v1/completions';
  34.  
  35.     // Execute the HTTPS POST request synchronously (last param Async = false)
  36.     LCb.ExecuteRequest(nil,nil,false);
  37.  
  38.     // Process returned JSON when request was successful
  39.     if Lcb.RequestResult.Success then
  40.     begin
  41.       LJsonValue := TJSonObject.ParseJSONValue(Lcb.RequestResult.ResultString);
  42.       LJsonValue := LJsonValue.GetValue<TJSonValue>('choices');
  43.       if LJsonValue is TJSonArray then
  44.       begin
  45.         LJSonArray := LJsonValue as TJSonArray;
  46.         LJSonString := LJSonArray.Items[0].GetValue<TJSONString>('text');
  47.         Result := LJSonString.Value;
  48.       end
  49.       else
  50.     end
  51.     else
  52.       raise Exception.Create('HTTP response code: ' + LCb.RequestResult.ResponseCode.ToString);
  53.   finally
  54.     LCb.Free;
  55.   end;
  56. end;
  57.  

With this code, using ChatGPT from a Delphi app becomes as simple as:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);  
  2. begin  
  3.   Memo1.Lines.Text := AskChatGPT(Edit1.Text);  
  4. end;  
  5.  

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.
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.

Make sure to download & install TMS FNC Core as well. https://www.tmssoftware.com/site/tmsfnccore.asp




Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #46 on: January 06, 2023, 02:11:36 pm »
That is the same as for our code .... But way more heavy weight.
In the mean time there are IDE's and components under development based on my core code demo code by multiple FreePascal users.
Anyway, my simple core code supports way more platforms and only uses standard FreePascal libraries.
« Last Edit: January 06, 2023, 02:19:32 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

fabiopesaju

  • Jr. Member
  • **
  • Posts: 93
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #47 on: January 06, 2023, 04:08:34 pm »
https://www.youtube.com/watch?v=V7bumJP6YAY&

Here one more example using Delphi.

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #48 on: January 07, 2023, 03:44:32 pm »
This is funny,.

 Recently teachers and students in NYC public schools are barred from using ChatGPT.

 Is there something they know we don't ?
 :o
The only true wisdom is knowing you know nothing

alpine

  • Hero Member
  • *****
  • Posts: 1067
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #49 on: January 07, 2023, 05:54:52 pm »
This is funny,.

 Recently teachers and students in NYC public schools are barred from using ChatGPT.

 Is there something they know we don't ?
 :o
Searching more info about that, I've found this: https://meta.stackoverflow.com/questions/421831/temporary-policy-chatgpt-is-banned

I'm not a regular visitor in stackoverflow, perhaps other members should have been noticed that earlier.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #50 on: January 07, 2023, 07:41:13 pm »
This is funny,.

 Recently teachers and students in NYC public schools are barred from using ChatGPT.

 Is there something they know we don't ?
 :o
Searching more info about that, I've found this: https://meta.stackoverflow.com/questions/421831/temporary-policy-chatgpt-is-banned

I'm not a regular visitor in stackoverflow, perhaps other members should have been noticed that earlier.

ChatGPT is an Artificial Intelligence based chat bot by OpenAI and was announced on 30-11-2022.
And already so much noise.
2023 will be the OpenAI revolution.
« Last Edit: January 07, 2023, 07:44:14 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #51 on: January 07, 2023, 08:01:45 pm »
I told you it can do homework  :o ;D
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Fred vS

  • Hero Member
  • *****
  • Posts: 3168
    • StrumPract is the musicians best friend
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #52 on: January 07, 2023, 08:15:32 pm »
I told you it can do homework  :o ;D

Did you read the answers of : https://meta.stackoverflow.com/questions/421831/temporary-policy-chatgpt-is-banned ?
Funny to read all those developers who are afraid to loose their medals because of OpenAI....
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #53 on: January 07, 2023, 08:23:19 pm »
Did you read the answers of : https://meta.stackoverflow.com/questions/421831/temporary-policy-chatgpt-is-banned ?
Funny to read all those developers who are afraid to loose their medals because of OpenAI....
That is the scariest of all!! :-X %) :o Programmers that do not understand what other programmers are capable of.
They are just jealous on the amount of things that can be achieved if MS$ invests a billion dollars so the project can hire the best people in the field and on every single aspect of the project..
And maybe Jealous on the hardware they are playing with... I am!!

I asked a very personal friend about his opinion and he said he has "only half that budget", telling for one of the most recognized people in the field of AI.
( https://usa.nissannews.com/en-US/releases/nissans-dr-maarten-sierhuis-named-motortrend-software-defined-vehicle-innovator-awards-winner# )
We used to get very drunk together about 45-30 years ago.... And we still see eachother every so often.
« Last Edit: January 07, 2023, 09:06:18 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #54 on: January 07, 2023, 08:38:11 pm »
And maybe Jealous on the hardware they are playing with... I am!!

I'm not. It's just PCs... plus a few graphics cards and so on... meh.

Now, if Lenat et al had ever got some decent ontologies working, well, then I might be jealous.

But right now it's just pattern matching, and even though it's scarily impressive... well, I'd rather not be anywhere near it when things start going wrong.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #55 on: January 07, 2023, 08:52:08 pm »
I'm not. It's just PCs... plus a few graphics cards and so on... meh.
No Mark, and you can predict my answer:
They use the PC's just like we did in the 70ths and 80ths, as a Terminal to bigiron. (Well, cloud is basically accumulated iron, but very much bigger than bigiron in the past...and still uses old school bigiron too.)
Nothing new there.... ;D ;D Been there, seen it, used it and I know for sure you did too...

Seems you have forgotten part of your past?  :P

And it is not pattern matching as such, it is set theory. Pattern matching is just one of the ways to reduce to a set, and the slowest one, combinatory theory being the fastest. (pattern matching starts with a given pattern or partial pattern, combinatory theory reduces to a pattern. It does not need a pattern or partial pattern)
What worries me personally about those complainers is that they have forgotten about such simple things in math like Venn diagrams and set theory to either explain big data and its risks or implement access to big data . Now that is for another story... Hm, you can explain sixth graders what risks they are taking on the internet, just by showing them Venn diagrams  (I did that on my youngest daughter's (14 - 15 year age range) secondary school and they all got the point. No big math involved.) I bet you many on this forum can not even use them or set theory properly, let alone see the impact. And, hey it is a Pascal forum, they should know sets.....(NOT  >:D ) and combinatory math on sets (NOT AT ALL  >:D )

Yes, I am a grumpy old man...

Regards,

Thaddy
« Last Edit: January 07, 2023, 09:44:31 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #56 on: January 07, 2023, 09:41:04 pm »
They use the PC's just like we did in the 70ths and 80ths, as a Terminal to bigiron. (Well, cloud is basically accumulated iron, but very much bigger than bigiron in the past...and still uses old school bigiron too.)

Yes, but the "bigiron" is basically PCs with GPU-like extensions. Nothing really sexy and nothing- despite appearances- that /understands/ what it's doing.

And as for "The Cloud": it's basically everybody buying mainframe time from a megacorporation. It'll end in tears...

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

alpine

  • Hero Member
  • *****
  • Posts: 1067
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #57 on: January 07, 2023, 09:48:19 pm »
Quote from: MarkMLl
But right now it's just pattern matching, and even though it's scarily impressive... well, I'd rather not be anywhere near it when things start going wrong.
Yeah, me too.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #58 on: January 07, 2023, 09:53:54 pm »
/understands/ what it's doing.
No Mark. I accept it is rudimentary, but the models from OpenAI are disinguished from being dumb:
They are capable of understanding they might have given a wrong answer or a below par answer and start looking for and gathering additional information. And the models do that without the need for our responses. They analyse their answers themselves. (Not only based on weight, but based on weight and interpretation of context)
To me that is some form of intelligence. Not perfect - but intelligence is flawed by default- and this approach is certainly not new, but in the past very hard to implement. It still is.
A much earlier example of the applied technology is:
Code: [Select]
1. e4 e5 2. Nf3 Nc6 3. Bb5 a6 4. Ba4 Nf6 5. O-O Be7 6. Re1 b5 7. Bb3 d6 8. c3 O-O 9. h3 Na5 10. Bc2 c5 11. d4 Qc7 12. Nbd2 cxd4 13. cxd4 Nc6 14. Nf1 Bg4 15. Be3 Rac8 16. Qd2 Bxf3 17. Nxf3 Nd8 18. Nh4 Bf8 19. Nf5 Qe7 20. Nxg7 Kxg7 21. Bxh7+ Kxh7 22. Qh6+ Kg8 23. Qh7+ Kf8 24. Qh8+ Ke7 25. Qxg7+ Kd8 26. Bf4 Kc8 27. Re3 d5 28. exd5 Rcd7 29. Qf6 Kb8 30. Qxd8+ Rxd8 31. Bxd6 Rd6 32. Bf4 Rg6 33. Re7 Kc8 34. Rae1 Kd8 35. g3 Kc8 36. Kf1 Kd8 37. Re8+ Kc7 38. R8e7+ Kb6 39. Rd7 Kc5 40. Be3+ Kxd5 41. Bd4+ Kc6 42. Re6+ Kd7 43. Re7+ Kd6 44. Re6+ Kc7 45. Rc6+ Kb8 46. Rb6+ Kc8 47. Rd6 Kc7 48. Bxg7 Rd7 49. Rxd7+ Kxd7 50. Bf6 a5 51. Ke2 Kc6 52. Kd3 Kb5 53. Kc4 Ka4 54. Kb3 Kb4 55. Bb2+ Kc5 56. Kc3 Kd4 57. Bg7 a4 58. Bf6 a3 59. Kb3 Kc5 60. Be5 a2 61. Kxa2 1-0
Cudo's to the ones that know what I mean here  ::)
« Last Edit: January 07, 2023, 10:16:42 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: Has anyone used Free Pascal to connect with chatGPT (OpenAI)?
« Reply #59 on: January 07, 2023, 11:08:50 pm »
They are capable of understanding

They don't /understand/. Understanding is a sentient prerogative, and they're not.

Quote
Cudo's to the ones that know what I mean here  ::)

Kudos. And I'm not working through it right now.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018