feat: add open router proxy
This commit is contained in:
@@ -23,7 +23,9 @@ export class AdminPanelSessionStore extends Store {
|
||||
private ttlSeconds(session: SessionData) {
|
||||
const maxAgeMs = session.cookie?.maxAge;
|
||||
|
||||
return typeof maxAgeMs === 'number' ? Math.floor(maxAgeMs / 1000) : undefined;
|
||||
return typeof maxAgeMs === 'number'
|
||||
? Math.floor(maxAgeMs / 1000)
|
||||
: undefined;
|
||||
}
|
||||
|
||||
override get(
|
||||
|
||||
@@ -11,6 +11,7 @@ import type { Response } from 'express';
|
||||
import { ChatDTO } from './dtos/chat.dto';
|
||||
import { ActiveUser } from '@/common/decorators/active-user.decorator';
|
||||
import { User } from '../users/entities/user.entity';
|
||||
import { ApiAppResponse } from '@/common/decorators/api-app-response.decorator';
|
||||
|
||||
@Controller('chatbot')
|
||||
export class ChatbotController {
|
||||
@@ -23,6 +24,16 @@ export class ChatbotController {
|
||||
|
||||
@Post('chat')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
@ApiAppResponse([
|
||||
{
|
||||
status: HttpStatus.OK,
|
||||
variants: [
|
||||
{
|
||||
model: ChatDTO,
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
public async chat(@Body() chatDto: ChatDTO, @ActiveUser() user: User) {
|
||||
return this.chatbotService.chat(
|
||||
user,
|
||||
|
||||
@@ -2,9 +2,10 @@ import { Module } from '@nestjs/common';
|
||||
import { ChatbotController } from './chatbot.controller';
|
||||
import { ChatbotService } from './providers/chatbot.service';
|
||||
import { ConversationModule } from '../conversation/conversation.module';
|
||||
import { OpenRouterModule } from '@/common/modules/open-router/open-router.module';
|
||||
|
||||
@Module({
|
||||
imports: [ConversationModule],
|
||||
imports: [ConversationModule, OpenRouterModule],
|
||||
controllers: [ChatbotController],
|
||||
providers: [ChatbotService],
|
||||
})
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsInt, IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||
import { i18nValidationMessage as t } from 'nestjs-i18n';
|
||||
|
||||
export class ChatDTO {
|
||||
@ApiProperty({
|
||||
type: 'number',
|
||||
required: false,
|
||||
example: 1,
|
||||
description: 'Id of chat conversation',
|
||||
})
|
||||
@IsInt({
|
||||
message: t('validation.wrongFieldFormat', {
|
||||
field: '$t(chatbot.fields.conversationId)',
|
||||
@@ -10,6 +17,12 @@ export class ChatDTO {
|
||||
@IsOptional()
|
||||
conversationId?: number;
|
||||
|
||||
@ApiProperty({
|
||||
type: 'string',
|
||||
required: true,
|
||||
example: 'Ask something',
|
||||
description: 'Chat message',
|
||||
})
|
||||
@IsString({
|
||||
message: t('validation.wrongFieldFormat', {
|
||||
field: '$t(chatbot.fields.message)',
|
||||
|
||||
@@ -5,6 +5,7 @@ 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';
|
||||
import { OpenRouterService } from '@/common/modules/open-router/providers/open-router.service';
|
||||
|
||||
@Injectable()
|
||||
export class ChatbotService {
|
||||
@@ -18,16 +19,13 @@ export class ChatbotService {
|
||||
* Inject Conversation Service
|
||||
*/
|
||||
private readonly conversationService: ConversationService,
|
||||
|
||||
/**
|
||||
* Inject OpenRouter Service
|
||||
*/
|
||||
private readonly openRouterService: OpenRouterService,
|
||||
) {}
|
||||
|
||||
private async getOpenRouter() {
|
||||
const { OpenRouter } = await import('@openrouter/sdk');
|
||||
|
||||
return new OpenRouter({
|
||||
apiKey: this.configService.getOrThrow<string>('openRouter.apiKey'),
|
||||
});
|
||||
}
|
||||
|
||||
private async getConversationMessages(
|
||||
user: User,
|
||||
message: string,
|
||||
@@ -77,7 +75,7 @@ export class ChatbotService {
|
||||
conversationId,
|
||||
);
|
||||
|
||||
const openRouter = await this.getOpenRouter();
|
||||
const openRouter = await this.openRouterService.getOpenRouter();
|
||||
|
||||
const response = await openRouter.chat.send({
|
||||
chatRequest: {
|
||||
@@ -119,7 +117,7 @@ export class ChatbotService {
|
||||
conversationId,
|
||||
);
|
||||
|
||||
const openRouter = await this.getOpenRouter();
|
||||
const openRouter = await this.openRouterService.getOpenRouter();
|
||||
|
||||
const response = await openRouter.chat.send({
|
||||
chatRequest: {
|
||||
|
||||
@@ -9,6 +9,7 @@ import { AppException } from '@/common/exceptions/app.exception';
|
||||
import { AppTimeoutException } from '@/common/exceptions/app-timeout.exception';
|
||||
import { User } from '@/modules/users/entities/user.entity';
|
||||
import { SUBMISSION_ANALYSIS_SYSTEM_PROMPT } from '../../constants/analysis.constants';
|
||||
import { OpenRouterService } from '@/common/modules/open-router/providers/open-router.service';
|
||||
|
||||
@Injectable()
|
||||
export class AnalyzeSubmissionProvider {
|
||||
@@ -29,6 +30,11 @@ export class AnalyzeSubmissionProvider {
|
||||
* Inject Config Service
|
||||
*/
|
||||
private readonly configService: ConfigService,
|
||||
|
||||
/**
|
||||
* Inject OpenRouter Service
|
||||
*/
|
||||
private readonly openRouterService: OpenRouterService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -162,18 +168,10 @@ export class AnalyzeSubmissionProvider {
|
||||
return 'بیپاسخ';
|
||||
}
|
||||
|
||||
private async getOpenRouter() {
|
||||
const { OpenRouter } = await import('@openrouter/sdk');
|
||||
|
||||
return new OpenRouter({
|
||||
apiKey: this.configService.getOrThrow<string>('openRouter.apiKey'),
|
||||
});
|
||||
}
|
||||
|
||||
private async *streamAnalysis(
|
||||
questionnaire: string,
|
||||
): AsyncGenerator<string, void, unknown> {
|
||||
const openRouter = await this.getOpenRouter();
|
||||
const openRouter = await this.openRouterService.getOpenRouter();
|
||||
|
||||
const response = await openRouter.chat.send({
|
||||
chatRequest: {
|
||||
|
||||
@@ -19,6 +19,7 @@ import { HistoryGuestProvider } from './providers/history/history-guest.provider
|
||||
import { SubmissionHistoryProvider } from './providers/history/submission-history.provider';
|
||||
import { UserSubmissionListingProvider } from './providers/listing/user-submission-listing.provider';
|
||||
import { AnalyzeSubmissionProvider } from './providers/analyze/analyze-submission.provider';
|
||||
import { OpenRouterModule } from '@/common/modules/open-router/open-router.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -27,6 +28,7 @@ import { AnalyzeSubmissionProvider } from './providers/analyze/analyze-submissio
|
||||
QuestionsModule,
|
||||
RedisModule,
|
||||
ScoringModule,
|
||||
OpenRouterModule,
|
||||
],
|
||||
controllers: [SubmissionsController],
|
||||
providers: [
|
||||
|
||||
Reference in New Issue
Block a user