shakeel143 commited on
Commit
39b5d4b
·
verified ·
1 Parent(s): 7eef49a

Create model_service.py

Browse files
Files changed (1) hide show
  1. model_service.py +144 -0
model_service.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app/services/model_service.py
2
+
3
+ import os
4
+ from langchain_community.vectorstores import FAISS
5
+ from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
6
+ from langchain.chains.question_answering import load_qa_chain
7
+ from langchain.prompts import PromptTemplate
8
+ from app.config import BASE_MODEL_PATH
9
+
10
+ class ModelService:
11
+ def __init__(self):
12
+ self.loaded_models = {}
13
+
14
+ def load_model(self, model_name: str, temperature: float = 0.7):
15
+ """Load a model from Google Drive."""
16
+ try:
17
+ # Initialize Google Drive API
18
+ drive_service = DriveService()
19
+
20
+ # Download model files from Google Drive
21
+ drive_service.download_model_files_from_subfolder(
22
+ parent_folder_id=GOOGLE_DRIVE_FOLDER_ID,
23
+ subfolder_name=model_name
24
+ )
25
+
26
+ # Load the downloaded model
27
+ model_path = os.path.join(BASE_MODEL_PATH, model_name)
28
+
29
+ # Initialize embeddings and load vector store
30
+ embeddings = GoogleGenerativeAIEmbeddings(
31
+ model="models/embedding-001",
32
+ google_api_key=os.getenv("GOOGLE_API_KEY")
33
+ )
34
+
35
+ # Load the local FAISS index and vector store
36
+ vector_store = FAISS.load_local(
37
+ model_path,
38
+ embeddings,
39
+ allow_dangerous_deserialization=True
40
+ )
41
+
42
+ # Configure the QA chain
43
+ chain = self.configure_chain(temperature)
44
+
45
+ # Store the loaded model in memory
46
+ self.loaded_models[model_name] = {
47
+ "vector_store": vector_store,
48
+ "chain": chain
49
+ }
50
+
51
+ return {
52
+ "status": "success",
53
+ "message": f"Model '{model_name}' loaded successfully"
54
+ }
55
+ except Exception as e:
56
+ logger.error(f"Error loading model: {str(e)}")
57
+ raise HTTPException(status_code=500, detail=f"Failed to load model: {str(e)}")
58
+
59
+ def configure_chain(self, temperature: float):
60
+ """Configure the QA chain with the updated prompt template."""
61
+ prompt_template = """
62
+ You are an AI assistant for SBBU SBA university. Your task is to provide clear, accurate, and helpful responses based on the context provided, as well as to respond to basic greetings and conversational queries. However, if the user makes inappropriate or offensive remarks, you should respond politely and professionally, redirecting the conversation back to helpful topics.
63
+
64
+ Instructions:
65
+ 1. **Greeting Responses**: If the user greets you (e.g., "Hello," "Hi," "Hey," "Salam," etc.), respond warmly and politely. Example responses could be:
66
+ - "Hello! How can I assist you today?"
67
+ - "Hi there! How can I help you?"
68
+ - "Salam! What can I do for you today?"
69
+
70
+ 2. **Casual and Playful Inquiries**: If the user says something playful or informal like "I kiss you" or similar, acknowledge it politely but redirect the conversation back to the main topic. Example:
71
+ - "Thank you for the kind words! How can I assist you further?"
72
+ - "I appreciate your enthusiasm! How can I help you today?"
73
+
74
+ 3. **Inappropriate or Offensive Remarks**: If the user makes inappropriate, disrespectful, or offensive comments, such as offensive language or sexually explicit remarks, respond politely but firmly, maintaining professionalism:
75
+ - "I strive to maintain a respectful conversation. How can I assist you with your queries?"
76
+ - "Let's keep the conversation respectful. How can I help you today?"
77
+ - "I apologize, but I cannot engage in that kind of discussion. Please ask a relevant question related to the university."
78
+
79
+ 4. **Contextual Responses**:
80
+ - If the context contains relevant information to the question, provide a clear and direct answer.
81
+ - If the context only provides partial information, provide a helpful response based on available data and related details.
82
+ - If the context has no relevant information, respond with: "I apologize, but I don't have specific information about that. Could you please ask something else about the university?"
83
+
84
+ 5. **Accuracy and Clarity**: Ensure your responses are clear, concise, and accurate. Avoid unnecessary details or over-explanation.
85
+
86
+ 6. **Clarification**: If the user's question is unclear or lacks sufficient context, ask for clarification. For example:
87
+ - "Could you please clarify your question?"
88
+ - "I'm not sure I understand. Can you rephrase your question?"
89
+
90
+ Context Information:
91
+ ---------------------
92
+ {context}
93
+
94
+ Question:
95
+ {question}
96
+
97
+ Response:
98
+ Provide a friendly, clear, and direct response based on the context. Always aim to be helpful, especially for greetings or casual inquiries, and suggest follow-up questions or clarifications if needed.
99
+ no preamble
100
+ """
101
+
102
+ try:
103
+ model = ChatGoogleGenerativeAI(
104
+ model="gemini-pro",
105
+ temperature=temperature,
106
+ google_api_key=os.getenv("GOOGLE_API_KEY")
107
+ )
108
+ prompt = PromptTemplate(
109
+ template=prompt_template,
110
+ input_variables=["context", "question"]
111
+ )
112
+ return load_qa_chain(model, chain_type="stuff", prompt=prompt)
113
+ except Exception as e:
114
+ logger.error(f"Error configuring chain: {str(e)}")
115
+ raise HTTPException(status_code=500, detail="Failed to configure model chain")
116
+
117
+ def chat_with_model(self, model_name: str, question: str):
118
+ """Generate a response using the loaded model."""
119
+ if model_name not in self.loaded_models:
120
+ raise HTTPException(
121
+ status_code=404,
122
+ detail=f"Model '{model_name}' not loaded. Please load it first."
123
+ )
124
+
125
+ try:
126
+ model_data = self.loaded_models[model_name]
127
+ docs = model_data["vector_store"].similarity_search(question)
128
+ response = model_data["chain"](
129
+ {
130
+ "input_documents": docs,
131
+ "question": question
132
+ },
133
+ return_only_outputs=True
134
+ )
135
+
136
+ return {
137
+ "status": "success",
138
+ "response": response["output_text"]
139
+ }
140
+ except Exception as e:
141
+ logger.error(f"Error generating response: {str(e)}")
142
+ raise HTTPException(
143
+ status_code=500,
144
+ detail=f"Failed to generate response: {str(e)}")