feat: add conversation module
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Body, Controller, Post } from '@nestjs/common';
|
||||
import { Body, Controller, Post, Res } from '@nestjs/common';
|
||||
import { ChatbotService } from './providers/chatbot.service';
|
||||
import { Public } from '../auth/decorators/public.decorator';
|
||||
import type { Response } from 'express';
|
||||
|
||||
@Controller('chatbot')
|
||||
export class ChatbotController {
|
||||
@@ -17,4 +18,52 @@ export class ChatbotController {
|
||||
public async chat(@Body('message') message: string) {
|
||||
return this.chatbotService.chat(message);
|
||||
}
|
||||
|
||||
@Post('stream')
|
||||
// TEMP:
|
||||
@Public()
|
||||
public async streamChat(
|
||||
@Body('message') message: string,
|
||||
@Res() res: Response,
|
||||
) {
|
||||
res.setHeader('Content-Type', 'text/event-stream');
|
||||
res.setHeader('Cache-Control', 'no-cache');
|
||||
res.setHeader('Connection', 'keep-alive');
|
||||
|
||||
for await (const content of this.chatbotService.streamChat(message)) {
|
||||
res.write(
|
||||
`data: ${JSON.stringify({
|
||||
message: content,
|
||||
})}\n\n`,
|
||||
);
|
||||
}
|
||||
|
||||
res.end();
|
||||
}
|
||||
|
||||
// @Sse('stream')
|
||||
// @Public()
|
||||
// public streamChat(
|
||||
// @Body('message') message: string,
|
||||
// ): Observable<MessageEvent> {
|
||||
// return new Observable((subscriber) => {
|
||||
// const run = async () => {
|
||||
// try {
|
||||
// for await (const content of this.chatbotService.streamChat(message)) {
|
||||
// subscriber.next({
|
||||
// data: {
|
||||
// message: content,
|
||||
// },
|
||||
// });
|
||||
// }
|
||||
|
||||
// subscriber.complete();
|
||||
// } catch (err) {
|
||||
// subscriber.error(err);
|
||||
// }
|
||||
// };
|
||||
|
||||
// void run();
|
||||
// });
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user