正如雷米·勒博(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;