From 7262a500331d87de1fa09c7bf477fc0e28157c4e Mon Sep 17 00:00:00 2001 From: David Alecrim Date: Mon, 7 Aug 2023 16:19:37 +0100 Subject: [PATCH] chore: small improvements (#125) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: small improvements´ * feat: improve config --- packages/api/config/default.json | 6 +++++- packages/api/src/ai/services/ai.service.ts | 5 +++-- .../api/src/ai/services/memory.service.ts | 4 ++-- .../api/src/app-config/app-config.service.ts | 21 ++++++++++++++----- packages/api/src/chats/chat-socket.gateway.ts | 6 ++++-- .../transform-doc-to-vector.job-consumer.ts | 4 ++-- .../upload-documents-to-chat.usecase.ts | 11 +++++++--- packages/api/tsconfig.json | 1 + 8 files changed, 41 insertions(+), 17 deletions(-) diff --git a/packages/api/config/default.json b/packages/api/config/default.json index 878f4d4..9de8f01 100644 --- a/packages/api/config/default.json +++ b/packages/api/config/default.json @@ -2,7 +2,11 @@ "ai": { "defaultTemperature": 0.2, "defaultChatContextTTL": 604800, - "defaultTokenLimitForSummarization": 15000 + "defaultTokenLimitForSummarization": 15000, + "defaultAiModel": "gpt-3.5-turbo-16k" + }, + "chat": { + "maxNumberOfDocumentsPerChat": 2 }, "authorization": { "roles": [ diff --git a/packages/api/src/ai/services/ai.service.ts b/packages/api/src/ai/services/ai.service.ts index a44c372..c168d66 100644 --- a/packages/api/src/ai/services/ai.service.ts +++ b/packages/api/src/ai/services/ai.service.ts @@ -21,7 +21,7 @@ export class AiService { ) { this.llmModel = new ChatOpenAI({ temperature: this.appConfigService.getAiAppConfig().defaultTemperature, - modelName: 'gpt-3.5-turbo-16k', + modelName: this.appConfigService.getAiAppConfig().defaultAiModel, }); } @@ -74,7 +74,8 @@ export class AiService { const chain = RetrievalQAChain.fromLLM(this.llmModel, vectorStoreRetriever); const title = await chain.call({ - query: 'Give me a single word that can reflect this document content', + query: + 'Give me three words, joined together with "-", that can reflect this document content', }); const description = await chain.call({ query: 'Summarize this document content in a single sentence. Be concise', diff --git a/packages/api/src/ai/services/memory.service.ts b/packages/api/src/ai/services/memory.service.ts index aaa2982..28d02a0 100644 --- a/packages/api/src/ai/services/memory.service.ts +++ b/packages/api/src/ai/services/memory.service.ts @@ -2,7 +2,7 @@ import { AppConfigService } from '@/app-config/app-config.service'; import { CACHE_CLIENT } from '@/common/constants/cache'; import { Inject, Injectable } from '@nestjs/common'; import { BufferMemory, ChatMessageHistory } from 'langchain/memory'; -import { AIChatMessage } from 'langchain/schema'; +import { AIMessage } from 'langchain/schema'; import { RedisChatMessageHistory } from 'langchain/stores/message/redis'; import { RedisClientType } from 'redis'; @@ -56,7 +56,7 @@ export class MemoryService { new BufferMemory({ returnMessages: true, memoryKey: 'history', - chatHistory: new ChatMessageHistory([new AIChatMessage(summary)]), + chatHistory: new ChatMessageHistory([new AIMessage(summary)]), }) ); } diff --git a/packages/api/src/app-config/app-config.service.ts b/packages/api/src/app-config/app-config.service.ts index e4a642d..04c5fd1 100644 --- a/packages/api/src/app-config/app-config.service.ts +++ b/packages/api/src/app-config/app-config.service.ts @@ -2,11 +2,14 @@ import { AppConfigNotFoundException } from '@/app-config/exceptions/app-config-n import { Injectable } from '@nestjs/common'; import * as appConfig from 'config'; -export type AiAppConfig = { - defaultTemperature: number; - defaultChatContextTTL: number; - defaultTokenLimitForSummarization: number; -}; +import { + ai as aiAppConfig, + chat as chatAppConfig, +} from '../../config/default.json'; + +type ChatAppConfig = typeof chatAppConfig; + +type AiAppConfig = typeof aiAppConfig; @Injectable() export class AppConfigService { @@ -18,6 +21,14 @@ export class AppConfigService { return appConfig.get('ai'); } + getChatConfig(): ChatAppConfig { + if (!appConfig.has('chat')) { + throw new AppConfigNotFoundException(); + } + + return appConfig.get('chat'); + } + async getAppRoles(): Promise { if (!appConfig.has('authorization.roles')) { throw new AppConfigNotFoundException(); diff --git a/packages/api/src/chats/chat-socket.gateway.ts b/packages/api/src/chats/chat-socket.gateway.ts index a0b7edb..d2ae952 100644 --- a/packages/api/src/chats/chat-socket.gateway.ts +++ b/packages/api/src/chats/chat-socket.gateway.ts @@ -1,4 +1,5 @@ import { AiService } from '@/ai/services/ai.service'; +import { AppConfigService } from '@/app-config/app-config.service'; import { ChatsRepository } from '@/chats/chats.repository'; import { createSocketMessageResponseFactory } from '@/chats/factory/create-socket-message.factory'; import { AddMessageToChatUsecase } from '@/chats/usecases/add-message-to-chat.usecase'; @@ -25,7 +26,8 @@ export class ChatSocketGateway { private readonly joinChatUsecase: JoinChatUsecase, private readonly addMessageToChatUsecase: AddMessageToChatUsecase, private readonly chatsRepository: ChatsRepository, - private readonly aiService: AiService + private readonly aiService: AiService, + private readonly appConfigService: AppConfigService ) {} @SubscribeMessage('joinRoom') @@ -107,7 +109,7 @@ export class ChatSocketGateway { tokens: encode(aiResponse).length, replyTo: addedMessage.id, ai: { - llmModel: 'gpt-3.5-turbo-16k', + llmModel: this.appConfigService.getAiAppConfig().defaultAiModel, }, }, }) diff --git a/packages/api/src/chats/job-consumers/transform-doc-to-vector.job-consumer.ts b/packages/api/src/chats/job-consumers/transform-doc-to-vector.job-consumer.ts index 43a3b4b..893f413 100644 --- a/packages/api/src/chats/job-consumers/transform-doc-to-vector.job-consumer.ts +++ b/packages/api/src/chats/job-consumers/transform-doc-to-vector.job-consumer.ts @@ -70,8 +70,8 @@ export class TransformDocToVectorJobConsumer { ): Promise { let loader: BaseDocumentLoader; const textSplitter = new RecursiveCharacterTextSplitter({ - chunkSize: 500, // default is 1000 - chunkOverlap: 100, // default is 200 + chunkSize: 1000, // default is 1000 + chunkOverlap: 200, // default is 200 }); const documentBlob = new Blob([document.src]); diff --git a/packages/api/src/chats/usecases/upload-documents-to-chat.usecase.ts b/packages/api/src/chats/usecases/upload-documents-to-chat.usecase.ts index e3bde25..3fe7c23 100644 --- a/packages/api/src/chats/usecases/upload-documents-to-chat.usecase.ts +++ b/packages/api/src/chats/usecases/upload-documents-to-chat.usecase.ts @@ -1,3 +1,4 @@ +import { AppConfigService } from '@/app-config/app-config.service'; import { ClerkAuthUserProvider } from '@/auth/providers/clerk/clerk-auth-user.provider'; import { ChatsRepository } from '@/chats/chats.repository'; import { ChatNotFoundException } from '@/chats/exceptions/chat-not-found.exception'; @@ -19,7 +20,8 @@ export class UploadDocumentsToChatUsecase implements Usecase { private readonly chatsRepository: ChatsRepository, private readonly clerkAuthUserProvider: ClerkAuthUserProvider, @InjectQueue(CHAT_DOCUMENT_UPLOAD_QUEUE) - private readonly chatDocUploadQueue: Queue + private readonly chatDocUploadQueue: Queue, + private readonly appConfigService: AppConfigService ) {} async execute( @@ -59,9 +61,12 @@ export class UploadDocumentsToChatUsecase implements Usecase { chat: Chat, files: Express.Multer.File[] ) { + const maxNumberOfDocumentsPerChat = + this.appConfigService.getChatConfig().maxNumberOfDocumentsPerChat; + if ( - chat.documents.length >= 2 || - files.length + chat.documents.length >= 2 + chat.documents.length >= maxNumberOfDocumentsPerChat || + files.length + chat.documents.length > maxNumberOfDocumentsPerChat ) { throw new NoMoreDocumentsCanBeUploadedToChatException(); } diff --git a/packages/api/tsconfig.json b/packages/api/tsconfig.json index 3ba6dea..a91f2ee 100644 --- a/packages/api/tsconfig.json +++ b/packages/api/tsconfig.json @@ -6,6 +6,7 @@ "emitDecoratorMetadata": true, "experimentalDecorators": true, "allowSyntheticDefaultImports": true, + "resolveJsonModule": true, "target": "es2017", "sourceMap": true, "outDir": "./dist",