Recent

Author Topic: [SOLVED] OpenSSL error in Chat GPT API access  (Read 993 times)

egsuh

  • Hero Member
  • *****
  • Posts: 1738
[SOLVED] OpenSSL error in Chat GPT API access
« on: March 09, 2025, 02:19:51 pm »
I'm trying to write a program that access Chat-GPT API. But when I execute following program, it raises "Count not initialize OpenSSL library" exception.  What should I check, and can I do?

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   OpenSSLSockets;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Memo1: TMemo;
  18.     procedure Button1Click(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. uses
  31.   fphttpclient, // For HTTP requests
  32.   fpjson,       // For JSON parsing
  33.   jsonparser;    // For JSON parsing
  34.  
  35. {$R *.lfm}
  36.  
  37. const
  38.   API_URL = 'https://api.openai.com/v1/completions';
  39.   API_KEY = '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';
  40.  
  41. var
  42.   HttpClient: TFPHTTPClient;
  43.   Response: TStringStream;
  44.   JsonRequest, JsonResponse: TJSONObject;
  45.   JsonBody: TStringStream;
  46.   Prompt: string;
  47.   Temperature: Double;
  48.   MaxTokens: Integer;
  49.   ResponseText: string;
  50.  
  51. { TForm1 }
  52.  
  53. procedure TForm1.Button1Click(Sender: TObject);
  54. begin
  55.   // Prompt for ChatGPT
  56.   Prompt := 'Translate the following English text to French: "Hello, how are you?"';
  57.   Temperature := 0.7; // Controls randomness. 0.0 for deterministic responses
  58.   MaxTokens := 50;    // Maximum number of tokens to generate
  59.  
  60.   // Create the HTTP client
  61.   HttpClient := TFPHTTPClient.Create(nil);
  62.   Response := TStringStream.Create('');
  63.   JsonBody := TStringStream.Create('');
  64.  
  65.   try
  66.     // Prepare the JSON body
  67.     JsonRequest := TJSONObject.Create;
  68.     try
  69.       JsonRequest.Add('model', 'text-davinci-003'); // or whichever model you are using
  70.       JsonRequest.Add('prompt', Prompt);
  71.       JsonRequest.Add('temperature', Temperature);
  72.       JsonRequest.Add('max_tokens', MaxTokens);
  73.  
  74.       JsonBody.WriteString(JsonRequest.AsJSON);
  75.  
  76.       // Set HTTP headers
  77.       HttpClient.AddHeader('Content-Type', 'application/json');
  78.       HttpClient.AddHeader('Authorization', 'Bearer ' + API_KEY);
  79.  
  80.       // HttpClient.Post(API_URL, JsonBody, Response);
  81.       // ResponseText := Response.DataString;
  82.  
  83.       HttpClient.Post(API_URL, JsonBody);
  84.       ResponseText := JsonBody.DataString;
  85.  
  86.       // Parse the response JSON
  87.       JsonResponse := TJSONObject(GetJSON(ResponseText));
  88.       try
  89.         memo1.lines.add(JsonResponse.Get('choices[0].text', 'No response text.'));
  90.       finally
  91.         JsonResponse.Free;
  92.       end;
  93.  
  94.     finally
  95.       JsonRequest.Free;
  96.     end;
  97.  
  98.   except
  99.     on E: Exception do
  100.       ShowMessage('Error: ' + E.Message);
  101.   end;
  102.  
  103.   // Clean up
  104.   HttpClient.Free;
  105.   Response.Free;
  106.   JsonBody.Free;
  107. end;
  108.  
  109. end.
« Last Edit: March 09, 2025, 05:02:55 pm by egsuh »

cdbc

  • Hero Member
  • *****
  • Posts: 2612
    • http://www.cdbc.dk
Re: OpenSSL error in Chat GPT API access
« Reply #1 on: March 09, 2025, 02:27:59 pm »
Hi
Which compiler?!?
I'm thinking "Too old ssl-libs", search the forum...
The built-in openssl that comes with fpc 3.2.2 doesn't cut it anymore, need newer...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

egsuh

  • Hero Member
  • *****
  • Posts: 1738
Re: OpenSSL error in Chat GPT API access
« Reply #2 on: March 09, 2025, 02:53:12 pm »
It's FPC 3.2.2. I'll check. Thank you.

egsuh

  • Hero Member
  • *****
  • Posts: 1738
Re: OpenSSL error in Chat GPT API access
« Reply #3 on: March 09, 2025, 03:45:58 pm »
For somebody who has similar problems,

There were mentions that : TfpHttpClient needs OpenSSL version 1.1, not other versions.

I could download it at following page:

https://wiki.overbyte.eu/wiki/index.php/ICS_Download

Direct download link is as follows.
https://wiki.overbyte.eu/arch/openssl-1.1.1w-win64.zip

I copied the dll's to the folder where .exe file reside in. Then the program worked.

But isn't it a problem that we have to use OpenSSL version 1.1 while the recentest one is 3.4.x?
« Last Edit: March 09, 2025, 03:47:36 pm by egsuh »

egsuh

  • Hero Member
  • *****
  • Posts: 1738
Re: OpenSSL error in Chat GPT API access
« Reply #4 on: March 09, 2025, 03:53:14 pm »
Anyway I succeeded in running the attached example, but I got "No response text". Probably I need some tokens.

Thaddy

  • Hero Member
  • *****
  • Posts: 18729
  • To Europe: simply sell USA bonds: dollar collapses
Re: OpenSSL error in Chat GPT API access
« Reply #5 on: March 09, 2025, 04:41:07 pm »
Actually it is 1.1.x It is not an issue because of LTS for 1.1.x , but I would recommend using 3.x where supported (trunk/main, not 3.2.2).
The current support in 3.2.x is already much better than it was in 3.0.4 with its hardcoded protocols and wrong order of handshake.

Bottom line is if your openssl version supports tls 1.2 which is basically the bottom line  8)
If Europe sells their USA bonds the USD will collapse. Europe can affort that given average state debts. The USA can't affort that. Just an advice...

egsuh

  • Hero Member
  • *****
  • Posts: 1738
Re: OpenSSL error in Chat GPT API access
« Reply #6 on: March 09, 2025, 05:02:32 pm »
Quote
Actually it is 1.1.x It is not an issue because of LTS for 1.1.x , but I would recommend using 3.x where supported (trunk/main, not 3.2.2).

It's fine for now, then.

 

TinyPortal © 2005-2018