I'm facing on ChatGpt to create an application using delphi 11, where the purpose is send data to AI then get a response related to it.
This is the code (but I get SSL protocol error on httpClient.Post as error:1409442E:SSL routines:SSL3_READ_BYTES:tlsv1 alert protocol version)
I think that the problem is related to Indy 10 Components and its SSL. Any suggestion?
function SendDataToChatGPT(data: TJSONObject): string;
var
httpClient: TIdHTTP;
requestURL: string;
requestBody: TStringStream;
IdSSLIOHandlerSocketOpenSSL: TIdSSLIOHandlerSocketOpenSSL ;
begin
httpClient := TIdHTTP.Create(nil);
try
IdSSLIOHandlerSocketOpenSSL := TIdSSLIOHandlerSocketOpenSSL.create(httpClient) ;
httpClient.IOHandler := IdSSLIOHandlerSocketOpenSSL;
IdSSLIOHandlerSocketOpenSSL.SSLOptions.SSLVersions :=[sslvTLSv1_2];
requestURL := 'https://api.openai.com/v1/chat/completions';
httpClient.Request.ContentType := 'application/json';
httpClient.Request.CustomHeaders.Add('Authorization: Bearer myAPI_KEY'); //MyAPI_KEY is my personal token
requestBody := TStringStream.Create(data.ToString, TEncoding.UTF8);
try
Result := httpClient.Post(requestURL, requestBody);
finally
requestBody.Free;
end;
finally
httpClient.Free;
end;
end;
procedure TForm10.Button5Click(Sender: TObject);
var
data: TJSONObject;
dataArray: TJSONArray;
dataItem: TJSONObject;
response: string;
begin
// Prepare your data as a JSON object
data := TJSONObject.Create;
try
data.AddPair('prompt', 'comment data and trend during interval date');
data.AddPair('max_tokens', '50');
// Create a JSON array to hold the data items
dataArray := TJSONArray.Create;
// Create a JSON object for each data item and add it to the array
dataItem := TJSONObject.Create;
dataItem.AddPair('Acid', 'Acetico');
dataItem.AddPair('value', '10');
dataItem.AddPair('data', '10/02/2003');
dataArray.AddElement(dataItem);
dataItem := TJSONObject.Create;
dataItem.AddPair('Acid', 'Acetico');
dataItem.AddPair('value', '11');
dataItem.AddPair('data', '12/02/2003');
dataArray.AddElement(dataItem);
dataItem := TJSONObject.Create;
dataItem.AddPair('Acid', 'Acetico');
dataItem.AddPair('value', '8.5');
dataItem.AddPair('data', '15/02/2003');
dataArray.AddElement(dataItem);
dataItem := TJSONObject.Create;
dataItem.AddPair('Acid', 'Acetico');
dataItem.AddPair('value', '10.7');
dataItem.AddPair('data', '22/02/2003');
dataArray.AddElement(dataItem);
// Add the data array to the main data object
data.AddPair('data', dataArray);
// Send data to ChatGPT
response := SendDataToChatGPT(data);
// Process the response as needed
Memo1.Lines.Text := response;
finally
data.Free;
end;
end;
Searching around, it seems that https://api.openai.com
may require TLS 1.3 only, which TIdSSLIOHandlerSocketOpenSSL
does not support, it can only do TLS 1.2 and lower.
To use TLS 1.3 in Indy, you will have to use this WIP SSLIOHandler instead, or an alternative solution, such as this SChannel SSLIOHandler 1.
1: Note that TLS 1.3 is supported in SChannel only on Windows 11, Windows Server 2022, and later.