如何在 Delphi 中获取对 POST 请求的正确 JSON 响应?

问题描述 投票:0回答:1

我的程序访问本地 Nitro 服务器(适用于 Windows 的 API Jan - https://jan.ai/api-reference#tag/chat/post/chat/completions)。我正在尝试为“创建聊天完成”API 请求编写 POST 请求。这是与 AI 模型的通信。

Python 中一切正常,我得到了答案。在 Delphi 11.3 中,我得到一些奇怪的块而不是响应。但它们包含必要的数据(每个块包含 1 个字)。虽然我必须接收 JSON 并突出显示“内容”对象中的响应。

这是我的代码:

procedure TForm2.Button1Click(Sender: TObject);
var
  IdHTTP: TIdHTTP;
  RequestData: TStringStream;
  ResponseData: string;
  JsonResponse: TJSONObject;
begin
  IdHTTP := TIdHTTP.Create;
  RequestData := TStringStream.Create;
  try
    IdHTTP.Request.ContentType := 'application/json';
    RequestData.WriteString('{"messages":[{"content":"You are a helpful assistant.","role":"system"},{"content":"How are you?","role":"user"}],"model":'+
    '"tinyllama-1.1b","stream":true,"max_tokens":2048,"stop":["hello","Hello","HELLO","hELLO"],"frequency_penalty":0,"presence_penalty":0,"temperature":0.7,"top_p":0.95}');
    ResponseData := IdHTTP.Post('http://localhost:1337/v1/chat/completions', RequestData);
    Memo2.Lines.Add(ResponseData);
    JsonResponse := TJSONObject.ParseJSONValue(ResponseData) as TJSONObject;
  finally
    IdHTTP.Free;
    RequestData.Free;
  end;
end;

我发送了一个数组、流、HTTP 200 响应,好的,但这是我能得到的最好的。不幸的是,我无法发送屏幕截图,但是 Delphi 的 HTTP 分析器和 Python 的代码工作正常,但 Delphi 中的答案不是 JSON。

[![图片][1]][1]

这是一个适用于我的情况的有效 Python 查询:

import requests

url = "http://localhost:1337/v1/chat/completions"

payload = {
    "messages": [
        {
            "content": "You are a helpful assistant.",
            "role": "system"
        },
        {
            "content": "Hello!",
            "role": "user"
        }
    ],
    "model": "tinyllama-1.1b",
    "stream": True,
    "max_tokens": 2048,
    "stop": ["hello", "Hello", "HELLO", "hELLO"],
    "frequency_penalty": 0,
    "presence_penalty": 0,
    "temperature": 0.7,
    "top_p": 0.95
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.json())

我得到了这个正确的答案。但在 Delphi 中,答案如屏幕截图所示。

{
  "choices": [
    {
      "finish_reason": null,
      "index": 0,
      "message": {
        "content": "Hello user. What can I help you with?",
        "role": "assistant"
      }
    }
  ],
  "created": 1700193928,
  "id": "ebwd2niJvJB1Q2Whyvkz",
  "model": "_",
  "object": "chat.completion",
  "system_fingerprint": "_",
  "usage": {
    "completion_tokens": 500,
    "prompt_tokens": 33,
    "total_tokens": 533
  }
}
json delphi post
1个回答
0
投票

非常感谢大家的回答。我决定看看什么会影响结果。事实证明我得到了正确的答案。这由服务器的内部请求调试器确认。但!我在 RequestData.WriteString

"stream":true
中设置了一个参数。当我将其更改为
"stream":false
时,我得到了适合我的案例的正确 JSON

procedure TForm2.Button1Click(Sender: TObject);
var
  IdHTTP: TIdHTTP;
  RequestData: TStringStream;
  ResponseData: string;
  JsonResponse: TJSONObject;
begin
  IdHTTP := TIdHTTP.Create;
  RequestData := TStringStream.Create;
  try
    IdHTTP.Request.ContentType := 'application/json';
    RequestData.WriteString('{"messages":[{"content":"You are a helpful assistant.","role":"system"},{"content":"How are you?","role":"user"}],"model":'+
    '"tinyllama-1.1b","stream":false,"max_tokens":2048,"stop":["hello","Hello","HELLO","hELLO"],"frequency_penalty":0,"presence_penalty":0,"temperature":0.7,"top_p":0.95}');
    ResponseData := IdHTTP.Post('http://localhost:1337/v1/chat/completions', RequestData);
    Memo2.Lines.Add(ResponseData);
    JsonResponse := TJSONObject.ParseJSONValue(ResponseData) as TJSONObject;
      try
        Memo1.Lines.Add(JsonResponse.FindValue('choices[0].message.content').Value);
      finally
        JsonResponse.Free;
      end;
  finally
    IdHTTP.Free;
    RequestData.Free;
  end;
end;

现在一切正常,我可以将 AI 集成到我的 Delphi 程序中。非常感谢大家!

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.