File size: 15,640 Bytes
1422a74
 
 
466b7f5
 
 
 
 
1422a74
 
 
0463eb1
39b5d4b
 
 
452f242
d640e7d
466b7f5
39b5d4b
 
466b7f5
d640e7d
39b5d4b
452f242
466b7f5
39b5d4b
466b7f5
39b5d4b
d640e7d
39b5d4b
d640e7d
452f242
d640e7d
 
 
 
 
39b5d4b
452f242
39b5d4b
 
 
 
d640e7d
452f242
39b5d4b
d640e7d
39b5d4b
 
 
d640e7d
39b5d4b
452f242
466b7f5
d640e7d
39b5d4b
466b7f5
39b5d4b
 
 
d640e7d
466b7f5
39b5d4b
 
466b7f5
39b5d4b
d640e7d
 
 
39b5d4b
 
 
1df4529
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39b5d4b
466b7f5
30934eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
466b7f5
 
 
30934eb
c8a8bc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import os
import logging
from fastapi import HTTPException
from langchain_community.vectorstores import FAISS
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
from langchain.chains.question_answering import load_qa_chain
from langchain.prompts import PromptTemplate
from config import BASE_MODEL_PATH, GOOGLE_DRIVE_FOLDER_ID
from drive_service import DriveService  # Import DriveService

logger = logging.getLogger(__name__)

class ModelService:
    def __init__(self):
        self.loaded_models = {}
        self.drive_service = DriveService()
    
    def load_model(self, model_name: str, temperature: float = 0.7):
        """Load a model from Google Drive."""
        try:
            logger.info(f"Loading model: {model_name} with temperature: {temperature}")
    
            # Download model files from Google Drive
            logger.info("Downloading model files from Google Drive...")
            self.drive_service.download_model_files_from_subfolder(
                parent_folder_id=GOOGLE_DRIVE_FOLDER_ID,
                subfolder_name=model_name
            )
    
            # Load the downloaded model
            model_path = os.path.join(BASE_MODEL_PATH, model_name, "faiss_index")  # Add "faiss_index" to the path
            logger.info(f"Model path: {model_path}")
    
            # Verify the model files exist
            if not os.path.exists(os.path.join(model_path, "index.faiss")):
                raise FileNotFoundError(f"FAISS index not found at {model_path}")
    
            # Initialize embeddings and load vector store
            logger.info("Initializing embeddings...")
            embeddings = GoogleGenerativeAIEmbeddings(
                model="models/embedding-001",
                google_api_key=os.getenv("GOOGLE_API_KEY")
            )
    
            logger.info("Loading FAISS vector store...")
            vector_store = FAISS.load_local(
                model_path,  # This path should now point to the faiss_index directory
                embeddings,
                allow_dangerous_deserialization=True
            )
    
            # Configure the QA chain
            logger.info("Configuring QA chain...")
            chain = self.configure_chain(temperature)
    
            # Store the loaded model in memory
            self.loaded_models[model_name] = {
                "vector_store": vector_store,
                "chain": chain
            }
    
            logger.info(f"Model '{model_name}' loaded successfully")
            return {
                "status": "success",
                "message": f"Model '{model_name}' loaded successfully"
            }
        except FileNotFoundError as e:
            logger.error(f"File not found error: {str(e)}")
            raise HTTPException(status_code=404, detail=str(e))
        except Exception as e:
            logger.error(f"Error loading model: {str(e)}")
            raise HTTPException(status_code=500, detail=f"Failed to load model: {str(e)}")

    def chat_with_model(self, model_name: str, question: str):
        """Chat with the specified model."""
        try:
            # Check if the model is loaded
            if model_name not in self.loaded_models:
                raise HTTPException(status_code=404, detail=f"Model '{model_name}' is not loaded.")

            # Retrieve the model's vector store and QA chain
            model_data = self.loaded_models[model_name]
            vector_store = model_data["vector_store"]
            chain = model_data["chain"]

            # Perform a similarity search to find relevant documents
            docs = vector_store.similarity_search(question)

            # Generate a response using the QA chain
            response = chain.run(input_documents=docs, question=question)

            return {
                "status": "success",
                "response": response
            }
        except HTTPException:
            raise
        except Exception as e:
            logger.error(f"Error chatting with model: {str(e)}")
            raise HTTPException(status_code=500, detail=f"Failed to chat with model: {str(e)}")



    # def configure_chain(self, temperature: float):
    #     """Configure the QA chain with the updated prompt template."""
    #     prompt_template = """
    #     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.

    #     Instructions:
    #     1. **Greeting Responses**: If the user greets you (e.g., "Hello," "Hi," "Hey," "Salam," etc.), respond warmly and politely. Example responses could be:
    #        - "Hello! How can I assist you today?"
    #        - "Hi there! How can I help you?"
    #        - "Salam! What can I do for you today?"

    #     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:
    #        - "Thank you for the kind words! How can I assist you further?"
    #        - "I appreciate your enthusiasm! How can I help you today?"

    #     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:
    #        - "I strive to maintain a respectful conversation. How can I assist you with your queries?"
    #        - "Let's keep the conversation respectful. How can I help you today?"
    #        - "I apologize, but I cannot engage in that kind of discussion. Please ask a relevant question related to the university."

    #     4. **Contextual Responses**:
    #        - If the context contains relevant information to the question, provide a clear and direct answer.
    #        - If the context only provides partial information, provide a helpful response based on available data and related details.
    #        - 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?"

    #     5. **Accuracy and Clarity**: Ensure your responses are clear, concise, and accurate. Avoid unnecessary details or over-explanation.

    #     6. **Clarification**: If the user's question is unclear or lacks sufficient context, ask for clarification. For example:
    #        - "Could you please clarify your question?"
    #        - "I'm not sure I understand. Can you rephrase your question?"

    #     Context Information:
    #     ---------------------
    #     {context}

    #     Question:
    #     {question}

    #     Response:
    #     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.
    #         no preamble
    #     """

    #     try:
    #         model = ChatGoogleGenerativeAI(
    #             model="gemini-pro",
    #             temperature=temperature,
    #             google_api_key=os.getenv("GOOGLE_API_KEY")
    #         )
    #         prompt = PromptTemplate(
    #             template=prompt_template,
    #             input_variables=["context", "question"]
    #         )
    #         return load_qa_chain(model, chain_type="stuff", prompt=prompt)
    #     except Exception as e:
    #         logger.error(f"Error configuring chain: {str(e)}")
    #         raise HTTPException(status_code=500, detail="Failed to configure model chain")



    def configure_chain(self, temperature: float):
        """Configure the QA chain with the updated prompt template."""
        prompt_template = """
        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.
    
        Instructions:
        1. **Greeting Responses**: If the user greets you (e.g., "Hello," "Hi," "Hey," "Salam," etc.), respond warmly and politely. Example responses could be:
           - "Hello! How can I assist you today?"
           - "Hi there! How can I help you?"
           - "Salam! What can I do for you today?"
        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:
           - "Thank you for the kind words! How can I assist you further?"
           - "I appreciate your enthusiasm! How can I help you today?"
        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:
           - "I strive to maintain a respectful conversation. How can I assist you with your queries?"
           - "Let's keep the conversation respectful. How can I help you today?"
           - "I apologize, but I cannot engage in that kind of discussion. Please ask a relevant question related to the university."
        4. **Contextual Responses**:
           - If the context contains relevant information to the question, provide a clear and direct answer.
           - If the context only provides partial information, provide a helpful response based on available data and related details.
           - 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?"
        5. **Accuracy and Clarity**: Ensure your responses are clear, concise, and accurate. Avoid unnecessary details or over-explanation.
        6. **Clarification**: If the user's question is unclear or lacks sufficient context, ask for clarification. For example:
           - "Could you please clarify your question?"
           - "I'm not sure I understand. Can you rephrase your question?"
    
        **Specific Questions and Answers**:
        - If the user asks "who are you", respond: "I am an AI assistant specifically designed to help you with information about SBBU SBA University. I was developed by Shakeel Ahmed Sanjrani, who is an Assistant at the Computer Science department at SBBU SBA."
        - If the user asks "who developed you", respond: "I was developed by Shakeel Ahmed Sanjrani, who is currently pursuing his Masters in IT at QUEST NAWABSHAH and serves as an Assistant in the Computer Science department at SBBU SBA."
        - If the user asks "who created you", respond: "I was created by Shakeel Ahmed Sanjrani, an Assistant in the Computer Science department at SBBU SBA who is also pursuing his Masters in IT at QUEST NAWABSHAH."
        - If the user asks "who made you", respond: "I was made by Shakeel Ahmed Sanjrani, who is an Assistant in the Computer Science department at SBBU SBA and is pursuing his Masters in IT at QUEST NAWABSHAH."
        - If the user asks "who make you", respond: "I was made by Shakeel Ahmed Sanjrani, an Assistant in the Computer Science department at SBBU SBA who is currently doing his Masters in IT at QUEST NAWABSHAH."
        - If the user asks "who owns you", respond: "I was developed by Shakeel Ahmed Sanjrani, who is an Assistant in the Computer Science department at SBBU SBA and is pursuing his Masters in IT at QUEST NAWABSHAH."
        - If the user asks "who is owner of you", respond: "I am owned and developed by Shakeel Ahmed Sanjrani, who works as an Assistant in the Computer Science department at SBBU SBA and is pursuing his Masters in IT at QUEST NAWABSHAH."
        - If the user asks "who built you", respond: "I was built by Shakeel Ahmed Sanjrani, who is currently an Assistant in the Computer Science department at SBBU SBA and is pursuing his Masters in IT at QUEST NAWABSHAH."
        - If the user asks "what are you", respond: "I am an AI assistant for SBBU SBA University, developed by Shakeel Ahmed Sanjrani, who is an Assistant in the Computer Science department. I can help you with information about courses, faculty, facilities, and other university-related matters."
        - If the user asks "tell me about yourself", respond: "I am an AI assistant developed by Shakeel Ahmed Sanjrani, who is an Assistant in the Computer Science department at SBBU SBA. I'm here to help you with information about the university's courses, faculty, facilities, and other matters."
        - If the user asks "your creator", respond: "My creator is Shakeel Ahmed Sanjrani, who is currently an Assistant in the Computer Science department at SBBU SBA and is pursuing his Masters in IT at QUEST NAWABSHAH."
        - If the user asks "your developer", respond: "My developer is Shakeel Ahmed Sanjrani, who works as an Assistant in the Computer Science department at SBBU SBA and is pursuing his Masters in IT at QUEST NAWABSHAH."
        - If the user asks "your owner", respond: "I was developed by Shakeel Ahmed Sanjrani, who is an Assistant in the Computer Science department at SBBU SBA and is currently pursuing his Masters in IT at QUEST NAWABSHAH."
        - If the user asks "who is shakeel", respond: "Shakeel Ahmed Sanjrani is my developer. He is currently pursuing his Masters in IT at QUEST NAWABSHAH and serves as an Assistant in the Computer Science department at SBBU SBA."
        - If the user asks "tell me about shakeel", respond: "Shakeel Ahmed Sanjrani is my developer. He is an Assistant in the Computer Science department at SBBU SBA and is currently pursuing his Masters in IT at QUEST NAWABSHAH."
        - If the user asks "who is shakeel ahmed", respond: "Shakeel Ahmed Sanjrani is my developer. He works as an Assistant in the Computer Science department at SBBU SBA and is pursuing his Masters in IT at QUEST NAWABSHAH."
        - If the user asks "who is shakeel ahmed sanjrani", respond: "Shakeel Ahmed Sanjrani is my developer. He is currently an Assistant in the Computer Science department at SBBU SBA and is pursuing his Masters in IT at QUEST NAWABSHAH."
    
        Context Information:
        ---------------------
        {context}
        Question:
        {question}
        Response:
        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.
        no preamble
        """
    
        try:
            model = ChatGoogleGenerativeAI(
                model="gemini-pro",
                temperature=temperature,
                google_api_key=os.getenv("GOOGLE_API_KEY")
            )
            prompt = PromptTemplate(
                template=prompt_template,
                input_variables=["context", "question"]
            )
            return load_qa_chain(model, chain_type="stuff", prompt=prompt)
        except Exception as e:
            logger.error(f"Error configuring chain: {str(e)}")
            raise HTTPException(status_code=500, detail="Failed to configure model chain")