代码之家  ›  专栏  ›  技术社区  ›  Gianluca Colombo

使用delphi应用程序向ChatGpt提问

  •  0
  • Gianluca Colombo  · 技术社区  · 2 年前

    我在ChatGpt上使用Delphi11创建一个应用程序,目的是向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;
    
    0 回复  |  直到 2 年前
        1
  •  3
  •   Remy Lebeau    2 年前

    四处搜寻,似乎 https://api.openai.com 可能只需要TLS 1.3 TIdSSLIOHandlerSocketOpenSSL 不支持,它只能执行TLS 1.2及更低版本。

    要在Indy中使用TLS 1.3,您必须使用 this WIP SSLIOHandler 相反,或者替代解决方案,例如 this SChannel SSLIOHandler 1.

    1. :请注意,只有在Windows 11、Windows Server 2022及更高版本上,SChannel才支持TLS 1.3。

        2
  •  1
  •   HemulGM    2 年前

    只要使用现成的图书馆。工作起来会方便快捷得多。 https://github.com/HemulGM/DelphiOpenAI

    或者从官方的GetIt包管理器安装

        3
  •  0
  •   Gianluca Colombo    2 年前

    正如雷米·勒博(Remy Lebeau)所建议的那样,ICS组件的apprach似乎可以工作(至少在登录服务器时) 我可以说,由于如果我试图使用错误的api密钥,服务器会返回

    { “错误”:{ “message”:“提供了不正确的API密钥:sk-a83ks********************************jBgz。您可以找到 您的API密钥位于 https://platform.openai.com/account/api-keys." , “type”:“invalid_request_error”, “param”:null, “code”:“invalid_api_key” }}

    因此,基于以下代码,除了发送的Json数据外,其他所有工作都正常。在这种情况下,服务器返回“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;