如何调用流式接口?-灵析社区

时光旅人

请求接口的代码: import fetch from "node-fetch"; fetch("https://api.openai-sb.com/v1/moderations", { method: "POST", headers: { Authorization: "Bearer ***********", "Content-Type": "application/json", }, body: JSON.stringify({ input: "树上有九只鸟,开枪打死一只,请问树上还剩几只鸟?" }), }).then(data => { console.log(data) });

阅读量:134

点赞量:0

问AI
import fetch from "node-fetch"; fetch("https://api.openai-sb.com/v1/moderations", { method: "POST", headers: { Authorization: "Bearer ***********", "Content-Type": "application/json", }, body: JSON.stringify({ input: "树上有九只鸟,开枪打死一只,请问树上还剩几只鸟?" }), }).then(res => { const reader = res.body.getReader(); while (true) { const { done, value } = await reader.read(); if (done) { break; } const str = new TextDecoder().decode(value) console.log(str) } });