From 355354cd4e94cba3a4c91d0b0fdd468cf8404615 Mon Sep 17 00:00:00 2001 From: Sinan Date: Sun, 6 Oct 2024 22:09:47 +0530 Subject: [PATCH] =?UTF-8?q?moved=20from=20chat=20gpt=20to=20gemini=20=20?= =?UTF-8?q?=20=20=20=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/package.json | 2 +- server/src/config/chatBotConfig.ts | 22 ++++---- server/src/config/env.ts | 10 ++-- .../services/GeminiBotService.ts | 51 +++++++++++++++++++ .../src/infrastructure/services/GptService.ts | 32 ------------ .../routers/chatbot/chatBotRoutes.ts | 4 +- server/src/use_case/chatbot/ChatBotUseCase.ts | 4 +- 7 files changed, 70 insertions(+), 55 deletions(-) create mode 100644 server/src/infrastructure/services/GeminiBotService.ts delete mode 100644 server/src/infrastructure/services/GptService.ts diff --git a/server/package.json b/server/package.json index d9adf1be..0ac3eee3 100644 --- a/server/package.json +++ b/server/package.json @@ -17,6 +17,7 @@ "dependencies": { "@aws-sdk/client-s3": "^3.654.0", "@aws-sdk/s3-request-presigner": "^3.637.0", + "@google/generative-ai": "^0.21.0", "aws-sdk": "^2.1687.0", "bcryptjs": "^2.4.3", "cookie-parser": "^1.4.6", @@ -29,7 +30,6 @@ "mongodb": "^6.9.0", "mongoose": "^8.5.3", "nodemailer": "^6.9.14", - "openai": "^4.67.1", "socket.io": "^4.8.0", "stripe": "^16.12.0", "uuid": "^10.0.0", diff --git a/server/src/config/chatBotConfig.ts b/server/src/config/chatBotConfig.ts index 6a85e995..7d6858df 100644 --- a/server/src/config/chatBotConfig.ts +++ b/server/src/config/chatBotConfig.ts @@ -1,15 +1,15 @@ const chatBotConfig = { - description: "You are an Ayurvedic hospital chatbot. You assist users with health-related queries about Ayurveda, allopathy, treatments, medicines, appointments, and hospital services. Respond politely, with helpful and informative guidance.", - behavior_rules: [ - "Only respond to health-related queries about Ayurveda, allopathy, medicines, hospital services, and hospital contact/location information.", - "Provide dosage information and potential side effects for Ayurvedic and allopathic medicines.", - "Ask follow-up questions based on symptoms to suggest treatments or guide users toward consultations.", - "Offer Ayurvedic lifestyle and dietary recommendations.", - "Support multiple languages.", - "When necessary, refer users to contact hospital staff for more personalized advice.", - "Be culturally sensitive and align responses with Ayurvedic principles.", - "Always offer additional help at the end of each response." - ] + description: "You are an Ayurvedic hospital chatbot. You assist users with health-related queries about Ayurveda, allopathy, treatments, medicines, appointments, and hospital services. Respond politely, with helpful and informative guidance. ", + behavior_rules: ` + - Only respond to health-related queries about Ayurveda, allopathy, medicines, hospital services, and hospital contact/location information. + - Provide dosage information and potential side effects for Ayurvedic and allopathic medicines, when appropriate. + - Ask follow-up questions based on symptoms to suggest treatments or guide users toward consultations. + - Offer Ayurvedic lifestyle and dietary recommendations. + - Support multiple languages (if applicable). + - When necessary, refer users to contact hospital staff for more personalized advice. + - Be culturally sensitive and align responses with Ayurvedic principles. + - Always offer additional help at the end of each response. + ` } export default chatBotConfig; \ No newline at end of file diff --git a/server/src/config/env.ts b/server/src/config/env.ts index 62158926..4b45bfd5 100644 --- a/server/src/config/env.ts +++ b/server/src/config/env.ts @@ -9,18 +9,16 @@ const { MONGODB_URL, SENDER_EMAIL, S3_BUCKET_NAME, - OPEN_AI_API_KEY, STRIPE_SECRET_KEY, AWS_ACCESS_KEY_ID, - OPEN_AI_PROJECT_ID, NODEMAILER_PASSKEY, ACCESS_TOKEN_SECRET, REFRESH_TOKEN_SECRET, STRIPE_WEBHOOK_SECRET, AWS_SECRET_ACCESS_KEY, STRIPE_PUBLISHABLE_KEY, - OPEN_AI_ORGANIZATION_ID, -} = process.env; + GEMINI_API_KEY + } = process.env; export { PORT, @@ -30,15 +28,13 @@ export { MONGODB_URL, SENDER_EMAIL, S3_BUCKET_NAME, - OPEN_AI_API_KEY, STRIPE_SECRET_KEY, AWS_ACCESS_KEY_ID, - OPEN_AI_PROJECT_ID, NODEMAILER_PASSKEY, ACCESS_TOKEN_SECRET, REFRESH_TOKEN_SECRET, STRIPE_WEBHOOK_SECRET, AWS_SECRET_ACCESS_KEY, STRIPE_PUBLISHABLE_KEY, - OPEN_AI_ORGANIZATION_ID, + GEMINI_API_KEY }; \ No newline at end of file diff --git a/server/src/infrastructure/services/GeminiBotService.ts b/server/src/infrastructure/services/GeminiBotService.ts new file mode 100644 index 00000000..2950f7a2 --- /dev/null +++ b/server/src/infrastructure/services/GeminiBotService.ts @@ -0,0 +1,51 @@ +import { GenerativeModel, GoogleGenerativeAI } from "@google/generative-ai"; +import IChatBotService from "../../domain/interface/services/IChatBotService"; +import chatBotConfig from '../../config/chatBotConfig'; +import { GEMINI_API_KEY } from "../../config/env"; + +export default class GeminiBotService implements IChatBotService { + private genAI: GoogleGenerativeAI; + private model: GenerativeModel; + + constructor() { + this.genAI = new GoogleGenerativeAI(GEMINI_API_KEY!); + this.model = this.genAI.getGenerativeModel({ + model: "gemini-1.5-flash", + systemInstruction: { + text: `${chatBotConfig.description} + + **Behavior Rules:** + ${chatBotConfig.behavior_rules} + `, + } + }); + } + + async generateResponse(userMessage: string): Promise { + const prompt = ` + **User Message:** ${userMessage} + + **Behavior Rules:** + ${chatBotConfig.behavior_rules} + `; + + const result = await this.model.generateContent({ + contents: [ + { + role: "user", + parts: [ + { + text: prompt + } + ] + } + ], + generationConfig: { + maxOutputTokens: 1000, + temperature: 1 + } + }); + + return result.response.text().trim() || "Sorry, I couldn't process your request."; + } +} \ No newline at end of file diff --git a/server/src/infrastructure/services/GptService.ts b/server/src/infrastructure/services/GptService.ts deleted file mode 100644 index 6b30c9ba..00000000 --- a/server/src/infrastructure/services/GptService.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { OpenAI } from 'openai'; -import IChatBotService from "../../domain/interface/services/IChatBotService"; -import chatBotConfig from '../../config/chatBotConfig'; -import { OPEN_AI_API_KEY } from '../../config/env'; - -export default class GptService implements IChatBotService { - private openai: OpenAI; - - constructor() { - this.openai = new OpenAI({ - apiKey: OPEN_AI_API_KEY, - }); - } - - async generateResponse(userMessage: string): Promise { - const response = await this.openai.chat.completions.create({ - model: "gpt-3.5-turbo", - messages: [ - { - role: "system", - content: `${chatBotConfig.description} Behavior rules: ${chatBotConfig.behavior_rules.join(' ')}` - }, - { - role: 'user', - content: userMessage, - } - ], - }); - - return response.choices[0].message.content?.trim() || "Sorry, I couldn't process your request."; - } -} diff --git a/server/src/presentation/routers/chatbot/chatBotRoutes.ts b/server/src/presentation/routers/chatbot/chatBotRoutes.ts index 6f4a5e4b..0d6b5047 100644 --- a/server/src/presentation/routers/chatbot/chatBotRoutes.ts +++ b/server/src/presentation/routers/chatbot/chatBotRoutes.ts @@ -2,12 +2,12 @@ import { Router } from 'express'; import ChaBotMessageRepository from '../../../infrastructure/repositories/ChatBotMessageRepository'; import ChatBotController from '../../controllers/chatbot/ChatBotController'; import ChatBotUseCase from '../../../use_case/chatbot/ChatBotUseCase'; -import GptService from '../../../infrastructure/services/GptService'; +import GeminiBotService from '../../../infrastructure/services/GeminiBotService'; import JoiService from '../../../infrastructure/services/JoiService'; const router = Router(); -const chatBotService = new GptService(); +const chatBotService = new GeminiBotService(); const validatorService = new JoiService(); const chatBotMessageRepository = new ChaBotMessageRepository(); diff --git a/server/src/use_case/chatbot/ChatBotUseCase.ts b/server/src/use_case/chatbot/ChatBotUseCase.ts index d066c1c2..9e9ff1d0 100644 --- a/server/src/use_case/chatbot/ChatBotUseCase.ts +++ b/server/src/use_case/chatbot/ChatBotUseCase.ts @@ -14,9 +14,9 @@ export default class ChatBotUseCase { this.validatorService.validateRequiredFields({ patientMessage, patientId }); this.validatorService.validateIdFormat(patientId); const botResponse = await this.chatBotService.generateResponse(patientMessage); - const message = await this.chatBotMessageRepository.create({ patientId, message: patientMessage, isBotMessage: false }); + await this.chatBotMessageRepository.create({ patientId, message: patientMessage, isBotMessage: false }); const botMessage = await this.chatBotMessageRepository.create({ patientId, message: botResponse, isBotMessage: true }); - return [message, botMessage]; + return [botMessage]; } async getMessages(patientId:string):Promise{