sbburaflaskapi / model_service.py
shakeel143's picture
Update model_service.py
c8a8bc4 verified
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")