使用 Delphi 应用程序向ChatGpt提问

u4dcyp6a  于 2023-10-18  发布在  其他
关注(0)|答案(3)|浏览(134)

我在ChatGpt上使用 Delphi 11创建一个应用程序,目的是将数据发送给AI,然后获得与之相关的响应。
这是代码(但我在httpClient上得到SSL协议错误。发布为错误:1409442E:SSL例程:SSL3_READ_BYTES:tlsv1警报协议版本)
我认为这个问题与Indy 10组件及其SSL有关。有什么建议吗?

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;
xeufq47z

xeufq47z1#

搜索一下,似乎https://api.openai.com可能只需要TLS 1.3,TIdSSLIOHandlerSocketOpenSSL不支持,它只能做TLS 1.2和更低。
要在Indy中使用TLS 1.3,您将不得不使用this WIP SSLIOHandler,或者其他解决方案,例如this SChannel SSLIOHandler 1。
1:请注意,TLS 1.3仅在Windows 11、Windows Server 2022和更高版本上的SChannel中受支持。

0mkxixxg

0mkxixxg2#

使用现成的图书馆。工作起来会方便快捷得多。https://github.com/HemulGM/DelphiOpenAI
或者从官方的GetIt包管理器安装它

kknvjkwl

kknvjkwl3#

正如雷米Lebeau所建议的那样,使用ICS组件的方法似乎可以工作(至少在登录到服务器时),我可以说,因为如果我尝试使用错误的API密钥,服务器将返回
{“error”:{“message”:“提供的API密钥不正确:sk-a83ks*jBgz.您可以在https://platform.openai.com/account/api-keys.",“type”找到您的API密钥:“invalid_request_error”,“param”:null,“code”:“invalid_API_key”} }
所以,基于下面的代码,除了发送的Json Data之外,所有的工作都正常。在这种情况下,服务器返回“httpPOST:状态#400错误请求”

procedure TForm10.HttpClientBeforeHeaderSend(Sender: TObject; const Method : String;Headers:TStrings);
begin
 // Headers.Clear;
  headers.Add('Authorization: Bearer ' + 'sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
  memo1.Lines.text:=Headers.Text;
end;

function TForm10.SendDataToChatGPT(data: TJSONObject): string;
var
  httpClient: TSSlHttpCli;
  requestURL: string;
  requestBody: TStringStream;
  SslContext:TSslContext;
begin
  httpClient := TSSlHttpCli.Create(nil);
  try
    SslContext:=TSslContext.Create(httpClient);
    SslContext.SslMinVersion := sslVerTLS1_2; // Set the minimum SSL/TLS version
    SslContext.SslMaxVersion := sslVerTLS1_3; // Set the maximum SSL/TLS version
    SslContext.SslVerifyPeer := False; // Set to True if you want to verify the server's certificate

    httpClient.SslContext := SslContext;
//    httpClient.SslContext.SslOptions:= [SslvTLSv1_3];

    requestURL := 'https://api.openai.com/v1/chat/completions';

   // httpClient.ContentType := 'application/json';
   // httpClient.RequestHeader_Add('Authorization: Bearer myAPI_KEY'); //MyAPI_KEY is my personal token

   //
    httpClient.OnBeforeHeaderSend := HttpClientBeforeHeaderSend;

    requestBody := TStringStream.Create(data.ToString, TEncoding.UTF8);
    try
      httpClient.URL := requestURL;
      httpClient.RcvdStream := TStringStream.Create('');
      httpClient.SendStream := requestBody;
      httpClient.Post;

      Result := TStringStream(httpClient.RcvdStream).DataString;
    finally
      requestBody.Free;
    end;
  finally
    httpClient.Free;
  end;

end;

procedure TForm10.Button2Click(Sender: TObject);
var
  data: TJSONObject;
  dataArray: TJSONArray;
  dataItem: TJSONObject;
  response: string;
begin
  // Prepare your data as a JSON object
  Memo1.Clear;
  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('date', '10/02/2003');
    dataArray.AddElement(dataItem);

    dataItem := TJSONObject.Create;
    dataItem.AddPair('Acid', 'Acetico');
    dataItem.AddPair('value', '11');
    dataItem.AddPair('date', '12/02/2003');
    dataArray.AddElement(dataItem);

    dataItem := TJSONObject.Create;
    dataItem.AddPair('Acid', 'Acetico');
    dataItem.AddPair('value', '8.5');
    dataItem.AddPair('date', '15/02/2003');
    dataArray.AddElement(dataItem);

    dataItem := TJSONObject.Create;
    dataItem.AddPair('Acid', 'Acetico');
    dataItem.AddPair('value', '10.7');
    dataItem.AddPair('date', '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.add(response);
  finally
    data.Free;
  end;
end;

相关问题