feat: connect chatbot to conversation database
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { CHATBOT_SYSTEM_PROMPT } from '../constants';
|
||||
import { User } from '@/modules/users/entities/user.entity';
|
||||
import { Conversation } from '@/modules/conversation/entities/conversation.entity';
|
||||
import { ConversationService } from '@/modules/conversation/providers/conversation.service';
|
||||
import { MessageRole } from '@/modules/conversation/enums/message-role.enum';
|
||||
|
||||
@Injectable()
|
||||
export class ChatbotService {
|
||||
@@ -9,6 +13,11 @@ export class ChatbotService {
|
||||
* Inject Config Service
|
||||
*/
|
||||
private readonly configService: ConfigService,
|
||||
|
||||
/**
|
||||
* Inject Conversation Service
|
||||
*/
|
||||
private readonly conversationService: ConversationService,
|
||||
) {}
|
||||
|
||||
private async getOpenRouter() {
|
||||
@@ -19,22 +28,61 @@ export class ChatbotService {
|
||||
});
|
||||
}
|
||||
|
||||
public async chat(message: string) {
|
||||
private async getConversationMessages(
|
||||
user: User,
|
||||
message: string,
|
||||
conversationId?: number,
|
||||
) {
|
||||
let conversation: Conversation;
|
||||
|
||||
if (conversationId) {
|
||||
conversation = await this.conversationService.findConversationById(
|
||||
conversationId,
|
||||
user,
|
||||
);
|
||||
} else {
|
||||
conversation = await this.conversationService.createConversation(user);
|
||||
}
|
||||
|
||||
await this.conversationService.addMessage(
|
||||
conversation,
|
||||
MessageRole.USER,
|
||||
message,
|
||||
);
|
||||
|
||||
const messages = await this.conversationService.getMessages(conversation);
|
||||
|
||||
return {
|
||||
conversation,
|
||||
messages: [
|
||||
{
|
||||
role: 'system' as const,
|
||||
content: CHATBOT_SYSTEM_PROMPT.trim(),
|
||||
},
|
||||
...messages.map((message) => ({
|
||||
role:
|
||||
message.role === MessageRole.USER
|
||||
? ('user' as const)
|
||||
: ('assistant' as const),
|
||||
content: message.content,
|
||||
})),
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
public async chat(user: User, message: string, conversationId?: number) {
|
||||
const { conversation, messages } = await this.getConversationMessages(
|
||||
user,
|
||||
message,
|
||||
conversationId,
|
||||
);
|
||||
|
||||
const openRouter = await this.getOpenRouter();
|
||||
|
||||
const response = await openRouter.chat.send({
|
||||
chatRequest: {
|
||||
model: this.configService.getOrThrow<string>('openRouter.model'),
|
||||
messages: [
|
||||
{
|
||||
role: 'system',
|
||||
content: CHATBOT_SYSTEM_PROMPT.trim(),
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: message,
|
||||
},
|
||||
],
|
||||
messages,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -48,27 +96,35 @@ export class ChatbotService {
|
||||
throw new Error('OpenRouter returned an unexpected response');
|
||||
}
|
||||
|
||||
await this.conversationService.addMessage(
|
||||
conversation,
|
||||
MessageRole.ASSISTANT,
|
||||
content,
|
||||
);
|
||||
|
||||
return {
|
||||
conversationId: conversation.id,
|
||||
message: content,
|
||||
};
|
||||
}
|
||||
|
||||
public async *streamChat(message: string) {
|
||||
public async *streamChat(
|
||||
user: User,
|
||||
message: string,
|
||||
conversationId?: number,
|
||||
) {
|
||||
const { conversation, messages } = await this.getConversationMessages(
|
||||
user,
|
||||
message,
|
||||
conversationId,
|
||||
);
|
||||
|
||||
const openRouter = await this.getOpenRouter();
|
||||
|
||||
const response = await openRouter.chat.send({
|
||||
chatRequest: {
|
||||
model: this.configService.getOrThrow<string>('openRouter.model'),
|
||||
messages: [
|
||||
{
|
||||
role: 'system',
|
||||
content: CHATBOT_SYSTEM_PROMPT.trim(),
|
||||
},
|
||||
{
|
||||
role: 'user',
|
||||
content: message,
|
||||
},
|
||||
],
|
||||
messages,
|
||||
stream: true,
|
||||
},
|
||||
});
|
||||
@@ -77,12 +133,26 @@ export class ChatbotService {
|
||||
throw new Error('Expected streaming response');
|
||||
}
|
||||
|
||||
let assistantMessage = '';
|
||||
|
||||
for await (const chunk of response) {
|
||||
const content = chunk.choices[0]?.delta?.content;
|
||||
|
||||
if (content) {
|
||||
yield content;
|
||||
if (!content) {
|
||||
continue;
|
||||
}
|
||||
|
||||
assistantMessage += content;
|
||||
|
||||
yield { conversationId: conversation.id, content };
|
||||
}
|
||||
|
||||
if (assistantMessage) {
|
||||
await this.conversationService.addMessage(
|
||||
conversation,
|
||||
MessageRole.ASSISTANT,
|
||||
assistantMessage,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user