chatbot : step 1

This commit is contained in:
morteza-mortezai
2025-11-29 12:25:13 +03:30
parent 3ebae01605
commit db266a1cd6
18 changed files with 689 additions and 765 deletions
+19 -14
View File
@@ -1,29 +1,32 @@
import { GoogleGenerativeAI, HarmBlockThreshold, HarmCategory } from "@google/generative-ai";
import { Injectable, Logger } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { inject, injectable } from "inversify";
import { DataContextService } from "./data-context.service";
import { Logger } from "../../../core/logging/logger";
import { IOCTYPES } from "../../../IOC/ioc.types";
import { CHATBOT_CONSTANTS } from "../constants/chatbot.constants";
import { IChatContext, IChatbotResponse, ILLMConfig } from "../interfaces/chatbot.interface";
@Injectable()
@injectable()
export class LLMService {
private readonly logger = new Logger(LLMService.name);
private readonly logger: Logger;
private genAI: GoogleGenerativeAI;
private config: ILLMConfig;
constructor(
private configService: ConfigService,
private dataContextService: DataContextService,
) {
constructor(@inject(IOCTYPES.ChatbotDataContextService) private dataContextService: DataContextService) {
this.logger = new Logger("LLMService");
this.config = {
model: this.configService.get("GEMINI_MODEL", CHATBOT_CONSTANTS.DEFAULT_MODEL),
temperature: Number(this.configService.get("GEMINI_TEMPERATURE", CHATBOT_CONSTANTS.DEFAULT_TEMPERATURE)),
maxTokens: Number(this.configService.get("GEMINI_MAX_TOKENS", CHATBOT_CONSTANTS.DEFAULT_MAX_TOKENS)),
topP: Number(this.configService.get("GEMINI_TOP_P", 0.95)),
apiKey: this.configService.getOrThrow("GEMINI_API_KEY"),
model: process.env.GEMINI_MODEL || CHATBOT_CONSTANTS.DEFAULT_MODEL,
temperature: Number(process.env.GEMINI_TEMPERATURE || CHATBOT_CONSTANTS.DEFAULT_TEMPERATURE),
maxTokens: Number(process.env.GEMINI_MAX_TOKENS || CHATBOT_CONSTANTS.DEFAULT_MAX_TOKENS),
topP: Number(process.env.GEMINI_TOP_P || "0.95"),
apiKey: process.env.GEMINI_API_KEY || "",
};
if (!this.config.apiKey) {
throw new Error("GEMINI_API_KEY is required");
}
this.genAI = new GoogleGenerativeAI(this.config.apiKey);
}
@@ -162,7 +165,9 @@ export class LLMService {
}
const history = [];
const recentHistory = context.conversationHistory.slice(-CHATBOT_CONSTANTS.MAX_CONVERSATION_HISTORY).filter((msg) => msg.type !== "system");
const recentHistory = context.conversationHistory
.slice(-CHATBOT_CONSTANTS.MAX_CONVERSATION_HISTORY)
.filter((msg) => msg.type !== "system");
for (const msg of recentHistory) {
history.push({