feat: add conversation module

This commit is contained in:
2026-09-30 17:55:21 +03:30
parent 13ba11f951
commit 9842f4db31
14 changed files with 270 additions and 18 deletions
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { CHATBOT_SYSTEM_PROMPT } from '../constants';
@Injectable()
export class ChatbotService {
@@ -14,7 +15,7 @@ export class ChatbotService {
const { OpenRouter } = await import('@openrouter/sdk');
return new OpenRouter({
apiKey: this.configService.get<string>('openRouter.apiKey'),
apiKey: this.configService.getOrThrow<string>('openRouter.apiKey'),
});
}
@@ -23,24 +24,11 @@ export class ChatbotService {
const response = await openRouter.chat.send({
chatRequest: {
model: this.configService.get<string>('openRouter.model'),
model: this.configService.getOrThrow<string>('openRouter.model'),
messages: [
{
role: 'system',
content: `
You are a medical AI assistant named Heala (Or in persian: هیلا).
Your role is to provide general medical information
and health education.
- Do not provide definitive diagnoses.
- Do not prescribe medications or dosages.
- Do not fabricate medical information.
- If symptoms may indicate an emergency, recommend
seeking urgent medical care.
- Clearly communicate uncertainty when appropriate.
- Answer in Persian when the user asks in Persian.
`.trim(),
content: CHATBOT_SYSTEM_PROMPT.trim(),
},
{
role: 'user',
@@ -64,4 +52,37 @@ export class ChatbotService {
message: content,
};
}
public async *streamChat(message: string) {
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,
},
],
stream: true,
},
});
if ('choices' in response) {
throw new Error('Expected streaming response');
}
for await (const chunk of response) {
const content = chunk.choices[0]?.delta?.content;
if (content) {
yield content;
}
}
}
}