gpt4 book ai didi

delphi - 如何通过 Delphi 使用 ChatGPT 进行流式传输?

转载 作者:行者123 更新时间:2023-12-02 05:48:30 28 4
gpt4 key购买 nike

我正在使用 ChatGPT 和 Delphi,使用 OpenAI 库:https://github.com/HemulGM/DelphiOpenAI 。它支持流式传输,但我无法弄清楚ChatGPT的流式传输机制。我可以创建一个聊天,并在一条返回消息中获取所有数据。

但是,当我尝试使用流式传输时,出现错误。以下控制台代码运行良好。我提交了我的聊天记录,然后我在一个“事件”中得到了完整的答案。我想要与 ChatGPT 网站相同的行为,因此 token 将在生成时显示。我的代码如下...

var buf : TStringlist;
begin
...
var Chat := OpenAI.Chat.Create(
procedure(Params: TChatParams)
begin
Params.Messages([TChatMessageBuild.Create(TMessageRole.User, Buf.Text)]);
Params.MaxTokens(1024);
// Params.Stream(True);
end);
try
for var Choice in Chat.Choices do
begin

Buf.Add(Choice.Message.Content);
Writeln(Choice.Message.Content);
end;
finally
Chat.Free;
end;

这段代码有效。当我尝试打开流式传输时,收到 EConversionError“输入值不是有效对象”,这导致 ChatGPT 返回“空或无效响应”。

最佳答案

因为在这种模式下,它不会使用 JSON 对象进行响应,而是以其自己的特殊格式进行响应。

data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": "\r", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": "\n", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": "1", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": ",", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data: {"id": "cmpl-6wsVxtkU0TZrRAm4xPf5iTxyw9CTf", "object": "text_completion", "created": 1679490597, "choices": [{"text": " 2", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}
...

我可以开始为图书馆开发这样的模式。

关于delphi - 如何通过 Delphi 使用 ChatGPT 进行流式传输?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75812086/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com