我正在构建一个 Github Copilot 扩展。
但是,我在扩展中遇到了问题。 我正在使用 Copilot LLM 进行扩展。
这是我的代码,是我从 示例存储库复制的:
// Use Copilot's LLM to generate a response to the user's messages, with
// our extra system messages attached.
const copilotLLMResponse = await fetch(
"https://api.githubcopilot.com/chat/completions",
{
method: "POST",
headers: {
authorization: `Bearer ${tokenForUser}`,
"content-type": "application/json",
},
body: JSON.stringify({
messages,
stream: true,
}),
}
);
const stream = Readable.from(copilotLLMResponse.body);
stream.pipe(res);
这段代码完美地将响应流传输给用户,但现在,我想自己获取文本。
我尝试获取文本,但失败了。
我尝试了本指南,替换了所有变量:
import { Readable } from "stream";
const stream = Readable.from([Buffer.from("Hello, world!")]);
const text = await new Response(stream).text();
console.log(text); // "Hello, world!"
但是我无法直接得到副驾驶的回复,我只能得到这样的信息:
data: {"choices":[{"finish_reason":"stop","index":0,"content_filter_offsets":{"check_offset":1896,"start_offset":1792,"end_
offset":1897},"content_filter_results":{"error":{"code":"","message":""},"hate":{"filtered":false,"severity":"safe"},"self_
harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"seve
rity":"safe"}},"delta":{"content":null}}],"created":1730025876,"id":"chatcmpl-someId","usage":{"comp
letion_tokens":22,"prompt_tokens":404,"total_tokens":426},"model":"gpt-3.5-turbo-0613"}
(被打印了几次)
我想将一些文档注入copilot(它将在消息中请求),以便它向用户提供有效的响应。
问题是在
fetch
请求中,
我实际上将 stream
设置为 true
。
将其设置为
false
后,我能够获取json和实际响应