Skip to content

Commit

Permalink
moved from chat gpt to gemini 🚀
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanptm committed Oct 6, 2024
1 parent faa8581 commit 355354c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 55 deletions.
2 changes: 1 addition & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
22 changes: 11 additions & 11 deletions server/src/config/chatBotConfig.ts
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 3 additions & 7 deletions server/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
};
51 changes: 51 additions & 0 deletions server/src/infrastructure/services/GeminiBotService.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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.";
}
}
32 changes: 0 additions & 32 deletions server/src/infrastructure/services/GptService.ts

This file was deleted.

4 changes: 2 additions & 2 deletions server/src/presentation/routers/chatbot/chatBotRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions server/src/use_case/chatbot/ChatBotUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IChatBotMessage[]>{
Expand Down

0 comments on commit 355354c

Please sign in to comment.