网站攻击一般有那些,建设银行第三方网站鉴权,wordpress主题仿牛杂网,网站自动更新时间代码背景#xff1a;项目中需要获取chatgpt实时返回的数据
使用场景#xff1a;在对接chatgpt 语言模型的时候采取的这种方案#xff0c;因为目前的大语言的模型的结果都是需要一点点计算的#xff0c;如果提出的问题比较复杂就导致响应的时间过长。
好处#xff1a;流式获取…背景项目中需要获取chatgpt实时返回的数据
使用场景在对接chatgpt 语言模型的时候采取的这种方案因为目前的大语言的模型的结果都是需要一点点计算的如果提出的问题比较复杂就导致响应的时间过长。
好处流式获取一般在用于数据量比较大的情况一次性返回会导致前端页面加载时间过长或者请求超时等问题这时候我们就可以考虑使用流式的方式拿到部分数据并先展示从而提升用户体验。
async function getChatgptMsg() {const response await fetch(你的url, {method: POST,headers: {Content-Type: application/json},dataType: text/event-stream,body: JSON.stringify({model: gpt-4,messages: messages,frequency_penalty: 0;max_tokens:4000;model:gpt-4;presence_penalty: 0.6;temperature: 0.5;top_p :1;})});if (!response.ok) {throw new Error(HTTP error! status: ${response.status});}const reader response.body.getReader();let decoder new TextDecoder();let resultData ;let result true;while (result) {const { done, value } await reader.read();if (done) {console.log(Stream ended);result false;break;}resultData decoder.decode(value);}
}