[SOLVED] OpenSSL error in Chat GPT API access lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- egsuh · 2025-03-09 13:19 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,  OpenSSLSockets; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Memo1: TMemo;    procedure Button1Click(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation uses  fphttpclient, // For HTTP requests  fpjson,       // For JSON parsing  jsonparser;    // For JSON parsing {$R *.lfm} const  API_URL = 'https://api.openai.com/v1/completions';  API_KEY = 'sk-proj-XLu-ZZZf6 kq9Ztu2ZR4BrXdVevgZq- 24oifN5AiWuNHEnMzgzx6Q_X9I454Oa1NLqhMIf4Yi1XT3BlbkFJSyY3vJ6io8LMWMKJSV y-pof7s06XbfRxSE-Hij4IGmLcPehc2DaFOwcHSUnpKXYCO4mkf81M0A'; var  HttpClient: TFPHTTPClient;  Response: TStringStream;  JsonRequest, JsonResponse: TJSONObject;  JsonBody: TStringStream;  Prompt: string;  Temperature: Double;  MaxTokens: Integer;  ResponseText: string; { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin  // Prompt for ChatGPT  Prompt := 'Translate the following English text to French: "Hello, how are you?"';  Temperature := 0.7; // Controls randomness. 0.0 for deterministic responses  MaxTokens := 50;    // Maximum number of tokens to generate   // Create the HTTP client  HttpClient := TFPHTTPClient.Create(nil);  Response := TStringStream.Create('');  JsonBody := TStringStream.Create('');   try    // Prepare the JSON body    JsonRequest := TJSONObject.Create;    try      JsonRequest.Add('model', 'text-davinci-003'); // or whichever model you are using      JsonRequest.Add('prompt', Prompt);      JsonRequest.Add('temperature', Temperature);      JsonRequest.Add('max_tokens', MaxTokens);       JsonBody.WriteString(JsonRequest.AsJSON);       // Set HTTP headers    HttpClient.AddHeader('Content-Type', 'application/json');      HttpClient.AddHeader('Authorization', 'Bearer ' + API_KEY);       // HttpClient.Post(API_URL, JsonBody, Response);      // ResponseText := Response.DataString;       HttpClient.Post(API_URL, JsonBody);      ResponseText := JsonBody.DataString;       // Parse the response JSON      JsonResponse := TJSONObject(GetJSON(ResponseText));      try      memo1.lines.add(JsonResponse.Get('choices[0].text', 'No response text.'));      finally        JsonResponse.Free;      end;     finally      JsonRequest.Free;    end;   except    on E: Exception do      ShowMessage('Error: ' + E.Message);  end;   // Clean up  HttpClient.Free;  Response.Free;  JsonBody.Free;end; end. ---------------------------------------------------------------------- cdbc · 2025-03-09 13:27 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 ---------------------------------------------------------------------- egsuh · 2025-03-09 13:53 It's FPC 3.2.2. I'll check. Thank you. ---------------------------------------------------------------------- egsuh · 2025-03-09 14:45 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? ---------------------------------------------------------------------- egsuh · 2025-03-09 14:53 Anyway I succeeded in running the attached example, but I got "No response text". Probably I need some tokens. ---------------------------------------------------------------------- Thaddy · 2025-03-09 15:41 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 ---------------------------------------------------------------------- egsuh · 2025-03-09 16:02 QuoteActually 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.