From da533ae1eddbf2093b7624d5f38b39f8a0d98751 Mon Sep 17 00:00:00 2001 From: cool Date: Fri, 10 May 2024 12:44:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9ESSE=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/demo/controller/open/sse.ts | 49 +++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/modules/demo/controller/open/sse.ts diff --git a/src/modules/demo/controller/open/sse.ts b/src/modules/demo/controller/open/sse.ts new file mode 100644 index 0000000..7b32a4b --- /dev/null +++ b/src/modules/demo/controller/open/sse.ts @@ -0,0 +1,49 @@ +import { CoolController, BaseController } from '@cool-midway/core'; +import { Get, Inject } from '@midwayjs/core'; +import { Context } from 'koa'; +import { PluginService } from '../../../plugin/service/info'; +import { PassThrough } from 'stream'; + +/** + * 事件流 服务端主动推送 + */ +@CoolController() +export class OpenDemoSSEController extends BaseController { + @Inject() + ctx: Context; + + @Inject() + pluginService: PluginService; + + @Get('/call', { summary: '事件流 服务端主动推送' }) + async call() { + // 设置响应头 + this.ctx.set('Content-Type', 'text/event-stream'); + this.ctx.set('Cache-Control', 'no-cache'); + this.ctx.set('Connection', 'keep-alive'); + + const stream = new PassThrough(); + + // 发送数据 + const send = (data: any) => { + stream.write(`data: ${JSON.stringify(data)}\n\n`); + }; + + // 获取插件实例 + const instance = await this.pluginService.getInstance('ollama'); + // 调用chat + const messages = [ + { role: 'system', content: '你叫小酷,是个编程助手' }, + { role: 'user', content: '用js写个Hello World' }, + ]; + instance.chat(messages, { stream: true }, res => { + send(res); + if (res.isEnd) { + this.ctx.res.end(); + } + }); + + this.ctx.status = 200; + this.ctx.body = stream; + } +}