我想流式传输结果通过 OpenAI 的 API 完成 https://beta.openai.com/docs/api-reference/completions.
该文档提到使用服务器发送的事件 https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format- 看起来是这样的没有对烧瓶进行开箱即用的处理 https://flask-sse.readthedocs.io/en/latest/quickstart.html所以我试图在客户端执行此操作(我知道这会公开 API 密钥)。然而,因为 OpenAI API 要求它发布,所以看起来像这样与 eventSource API 不兼容 https://stackoverflow.com/questions/34261928/can-server-sent-events-sse-with-eventsource-pass-parameter-by-post。我尝试通过 fetch 来做到这一点(使用可读流 https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams)但是当我尝试通过示例转换为 JSON 时,出现以下错误:Uncaught (in promise) SyntaxError: Unexpected token 'd', "data: {"id"... is not valid JSON
(我知道这不是有效的 JSON)。看起来它正在解析整个结果而不是每个单独的流。
data: {"id": "cmpl-5l11I1kS2n99uzNiNVpTjHi3kyied", "object": "text_completion", "created": 1661887020, "choices": [{"text": " to", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-002"}
data: {"id": "cmpl-5l11I1kS2n99uzNiNVpTjHi3kyied", "object": "text_completion", "created": 1661887020, "choices": [{"text": " AL", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-002"}
data: {"id": "cmpl-5l11I1kS2n99uzNiNVpTjHi3kyied", "object": "text_completion", "created": 1661887020, "choices": [{"text": "I", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-002"}
我会喜欢一些关于如何执行此操作的指针或简单的代码示例,因为我已经为此努力了一段时间了。谢谢!