目次FetchEventSource在大模型流式输出应用先讲讲这个微软开辟的可以使用POST的SSE的api,github链接:GitHub - Azure/fetch-event-source: A better API for making Event Source requests, with all the features of fetch() [code]FetchEventSource[/code] 是微软在 ASP.NET Core 中引入的一个功能,它允许开辟者以一种更简单和高效的方式处置惩罚 HTTP 请求和响应。 这个功能是作为 ASP.NET Core 的一部分提供的,它利用了 [code]System.Net.Http.Desktop[/code] 命名空间中的 [code]FetchResult[/code] 范例。 在 ASP.NET Core 中,[code]FetchEventSource[/code] 通常用于处置惩罚服务器发送的变乱(Server-Sent Events,SSE),这是一种允许服务器向客户端异步推送实时数据的技术。 使用 [code]FetchEventSource[/code],开辟者可以更容易地创建和消耗这些实时数据流。 使用 FetchEventSource 的一些关键点
以调用Qwen大模型为例[code]import { fetchEventSource } from '@microsoft/fetch-event-source'; export default { data() { return { output: '', apiKey: '$your-dashscope-api-key', // 替换为你的 DashScope API-KEY url: 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation', body: { model: 'qwen-turbo', input: { messages: [ { role: 'system', content: 'You are a helpful assistant.' }, { role: 'user', content: '你好' } ] }, parameters: { incremental_output: true } } }; }, methods: { async startSSE() { const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}`, 'X-DashScope-SSE': 'enable' }; try { this.eventSource = await fetchEventSource(this.url, { method: 'POST', headers: headers, body: JSON.stringify(this.body), onopen: (response) => { if (!response.ok) { throw new Error('Server returned an error'); } }, onmessage: (event) => { const data = JSON.parse(event.data); if (data.output && data.output.choices) { const content = data.output.choices[0].message.content; this.output += content; // 将内容添加到输出中 } }, onerror: (err) => { console.error('EventSource failed:', err); this.stopSSE(); } }); } catch (error) { console.error('Failed to start SSE:', error); } }, stopSSE() { if (this.eventSource) { this.eventSource.close(); this.eventSource = null; } } }, unmounted() { this.stopSSE(); } };[/code]这样就可以创建SSE的链接了。 那么有小搭档就要问了,那前端怎么实时显示吸收到的输出呢? [code]onmessage: (event) => { const data = JSON.parse(event.data); if (data.output && data.output.choices) { const content = data.output.choices[0].message.content; this.output += content; // 将内容添加到输出中 } }[/code]
这里界说了一个 [code]onmessage[/code] 变乱处置惩罚器。 当通过 SSE 连接吸收到消息时,会触发这个处置惩罚器。
[code]event.data[/code] 包罗了服务器发送的消息内容,通常是以 JSON 格式的字符串。 [code]JSON.parse[/code] 函数用于将这个 JSON 字符串解析为 JavaScript 对象。
这里使用 [code]if[/code] 语句来确保 [code]data[/code] 对象中存在 [code]output[/code] 属性,而且 [code]output[/code] 属性中存在 [code]choices[/code] 数组。 这是一种防御性编程的做法,用来克制在数据结构不完整时出现错误。
这行代码进一步从 [code]choices[/code] 数组中的第一个元素(通常是最相干的大概默认的消息)中提取 [code]message.content[/code]。 这通常是服务器推送的有用信息或数据。
[code]this.output[/code] 是 Vue 组件实例的一个数据属性,用于累积从服务器吸收到的全部消息内容。 这里使用 [code]+=[/code] 利用符将新吸收到的 [code]content[/code] 追加到 [code]this.output[/code] 的当前值上。 整个 [code]onmessage[/code] 变乱处置惩罚器的作用是:当通过 SSE 吸收到消息时,它将解析消息内容,从中提取有用的信息,并将其追加到 Vue 组件的 [code]output[/code] 数据属性中。 这样,组件的模板中的 [code]<pre>{{ output }}</pre>[/code] 就可以显示全部吸收到的消息内容,保持其原始的格式。 总结以上为个人履历,盼望能给各人一个参考,也盼望各人多多支持脚本之家。 来源:https://www.jb51.net/javascript/326701cxc.htm 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|手机版|小黑屋|梦想之都-俊月星空
( 粤ICP备18056059号 )|网站地图
GMT+8, 2025-7-2 03:25 , Processed in 0.035080 second(s), 19 queries .
Powered by Mxzdjyxk! X3.5
© 2001-2025 Discuz! Team.