Spaces:
Running
Running
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,198 +1,219 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pickle
|
| 4 |
-
from transformers import AutoTokenizer, AutoModel
|
| 5 |
-
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
-
import logging
|
| 7 |
-
import config
|
| 8 |
-
|
| 9 |
-
import
|
| 10 |
-
from
|
| 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 |
-
# Load
|
| 59 |
-
logger.info("Loading
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
return
|
| 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 |
-
logger.
|
| 103 |
-
return
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pickle
|
| 4 |
+
from transformers import AutoTokenizer, AutoModel
|
| 5 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 6 |
+
import logging
|
| 7 |
+
import config
|
| 8 |
+
import os
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
from langsmith.run_helpers import traceable
|
| 11 |
+
|
| 12 |
+
# Load environment variables from .env file
|
| 13 |
+
load_dotenv()
|
| 14 |
+
|
| 15 |
+
logger = logging.getLogger("swayam-chatbot")
|
| 16 |
+
|
| 17 |
+
# Initialize Together client with proper error handling and version compatibility
|
| 18 |
+
try:
|
| 19 |
+
# Try different import patterns for different versions of together library
|
| 20 |
+
try:
|
| 21 |
+
from together import Together
|
| 22 |
+
except ImportError:
|
| 23 |
+
try:
|
| 24 |
+
from together.client import Together
|
| 25 |
+
except ImportError:
|
| 26 |
+
import together
|
| 27 |
+
Together = together.Together
|
| 28 |
+
|
| 29 |
+
# Try to get API key from environment directly as a fallback
|
| 30 |
+
api_key = config.TOGETHER_API_KEY or os.environ.get("TOGETHER_API_KEY")
|
| 31 |
+
if not api_key:
|
| 32 |
+
logger.warning("No Together API key found. LLM functionality will not work.")
|
| 33 |
+
client = None
|
| 34 |
+
else:
|
| 35 |
+
client = Together(api_key=api_key)
|
| 36 |
+
logger.info("Together client initialized successfully")
|
| 37 |
+
except Exception as e:
|
| 38 |
+
logger.error(f"Failed to initialize Together client: {e}")
|
| 39 |
+
client = None
|
| 40 |
+
|
| 41 |
+
# Function for mean pooling to get sentence embeddings
|
| 42 |
+
def mean_pooling(model_output, attention_mask):
|
| 43 |
+
token_embeddings = model_output[0]
|
| 44 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
|
| 45 |
+
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
|
| 46 |
+
|
| 47 |
+
# Load embeddings and model once at startup
|
| 48 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 49 |
+
tokenizer = None
|
| 50 |
+
model = None
|
| 51 |
+
chunks = None
|
| 52 |
+
embeddings = None
|
| 53 |
+
|
| 54 |
+
def load_resources():
|
| 55 |
+
"""Load the embedding model and pre-computed embeddings"""
|
| 56 |
+
global tokenizer, model, chunks, embeddings
|
| 57 |
+
|
| 58 |
+
# Load model and tokenizer
|
| 59 |
+
logger.info("Loading embedding model...")
|
| 60 |
+
tokenizer = AutoTokenizer.from_pretrained(config.EMBEDDING_MODEL)
|
| 61 |
+
model = AutoModel.from_pretrained(config.EMBEDDING_MODEL)
|
| 62 |
+
model.to(device)
|
| 63 |
+
|
| 64 |
+
# Create embeddings directory if it doesn't exist
|
| 65 |
+
os.makedirs(os.path.dirname(config.CHUNK_PATH), exist_ok=True)
|
| 66 |
+
|
| 67 |
+
# Load stored chunks and embeddings
|
| 68 |
+
logger.info("Loading pre-computed embeddings...")
|
| 69 |
+
try:
|
| 70 |
+
with open(config.CHUNK_PATH, "rb") as f:
|
| 71 |
+
chunks = pickle.load(f)
|
| 72 |
+
|
| 73 |
+
with open(config.EMBEDDING_PATH, "rb") as f:
|
| 74 |
+
embeddings = pickle.load(f)
|
| 75 |
+
|
| 76 |
+
logger.info(f"Loaded {len(chunks)} chunks and embeddings of shape {embeddings.shape}")
|
| 77 |
+
return True
|
| 78 |
+
except FileNotFoundError as e:
|
| 79 |
+
logger.error(f"Error loading embeddings: {e}")
|
| 80 |
+
# Try downloading from cloud storage if available
|
| 81 |
+
if config.EMBEDDINGS_CLOUD_URL:
|
| 82 |
+
logger.info(f"Attempting to download embeddings from cloud storage...")
|
| 83 |
+
success = download_embeddings_from_cloud()
|
| 84 |
+
if success:
|
| 85 |
+
return load_resources() # Try loading again after download
|
| 86 |
+
return False
|
| 87 |
+
|
| 88 |
+
def download_embeddings_from_cloud():
|
| 89 |
+
"""Download embeddings from cloud storage"""
|
| 90 |
+
try:
|
| 91 |
+
import requests
|
| 92 |
+
|
| 93 |
+
# Download chunks file
|
| 94 |
+
logger.info(f"Downloading chunks from {config.CHUNKS_CLOUD_URL}")
|
| 95 |
+
response = requests.get(config.CHUNKS_CLOUD_URL)
|
| 96 |
+
if response.status_code == 200:
|
| 97 |
+
os.makedirs(os.path.dirname(config.CHUNK_PATH), exist_ok=True)
|
| 98 |
+
with open(config.CHUNK_PATH, "wb") as f:
|
| 99 |
+
f.write(response.content)
|
| 100 |
+
logger.info("Successfully downloaded chunks file")
|
| 101 |
+
else:
|
| 102 |
+
logger.error(f"Failed to download chunks: {response.status_code}")
|
| 103 |
+
return False
|
| 104 |
+
|
| 105 |
+
# Download embeddings file
|
| 106 |
+
logger.info(f"Downloading embeddings from {config.EMBEDDINGS_CLOUD_URL}")
|
| 107 |
+
response = requests.get(config.EMBEDDINGS_CLOUD_URL)
|
| 108 |
+
if response.status_code == 200:
|
| 109 |
+
with open(config.EMBEDDING_PATH, "wb") as f:
|
| 110 |
+
f.write(response.content)
|
| 111 |
+
logger.info("Successfully downloaded embeddings file")
|
| 112 |
+
return True
|
| 113 |
+
else:
|
| 114 |
+
logger.error(f"Failed to download embeddings: {response.status_code}")
|
| 115 |
+
return False
|
| 116 |
+
except Exception as e:
|
| 117 |
+
logger.error(f"Error downloading embeddings: {e}")
|
| 118 |
+
return False
|
| 119 |
+
|
| 120 |
+
def is_personal_query(query):
|
| 121 |
+
"""Determine if a query is about Swayam or general knowledge"""
|
| 122 |
+
query_lower = query.lower()
|
| 123 |
+
|
| 124 |
+
# Check if query contains personal keywords
|
| 125 |
+
for keyword in config.PERSONAL_KEYWORDS:
|
| 126 |
+
if keyword.lower() in query_lower:
|
| 127 |
+
logger.info(f"Query classified as PERSONAL due to keyword: {keyword}")
|
| 128 |
+
return True
|
| 129 |
+
|
| 130 |
+
logger.info("Query classified as GENERAL")
|
| 131 |
+
return False
|
| 132 |
+
|
| 133 |
+
@traceable(run_type="retriever", name="E5 Vector Retriever")
|
| 134 |
+
def get_relevant_context(query, top_k=3):
|
| 135 |
+
"""Retrieve relevant context from embeddings for a given query"""
|
| 136 |
+
if tokenizer is None or model is None:
|
| 137 |
+
logger.error("Embedding model not loaded. Call load_resources() first.")
|
| 138 |
+
return ""
|
| 139 |
+
|
| 140 |
+
# Process query with e5 model - use "query: " prefix for better retrieval
|
| 141 |
+
inputs = tokenizer(f"query: {query}", padding=True, truncation=True,
|
| 142 |
+
return_tensors="pt").to(device)
|
| 143 |
+
|
| 144 |
+
with torch.no_grad():
|
| 145 |
+
outputs = model(**inputs)
|
| 146 |
+
|
| 147 |
+
# Get query embedding
|
| 148 |
+
query_embedding = mean_pooling(outputs, inputs["attention_mask"]).cpu().numpy()
|
| 149 |
+
|
| 150 |
+
# Calculate similarity with all chunk embeddings
|
| 151 |
+
similarities = cosine_similarity(query_embedding, embeddings)[0]
|
| 152 |
+
|
| 153 |
+
# Get top k most similar chunks
|
| 154 |
+
top_indices = np.argsort(similarities)[::-1][:top_k]
|
| 155 |
+
|
| 156 |
+
# Combine the text from the top chunks
|
| 157 |
+
context_parts = []
|
| 158 |
+
for idx in top_indices:
|
| 159 |
+
_, chunk_text = chunks[idx]
|
| 160 |
+
similarity = similarities[idx]
|
| 161 |
+
if similarity > 0.2: # Only include reasonably similar chunks
|
| 162 |
+
context_parts.append(chunk_text)
|
| 163 |
+
logger.info(f"Including chunk with similarity: {similarity:.4f}")
|
| 164 |
+
|
| 165 |
+
return "\n\n".join(context_parts)
|
| 166 |
+
|
| 167 |
+
@traceable(run_type="llm", name="Together AI LLM")
|
| 168 |
+
def get_llm_response(messages):
|
| 169 |
+
"""Get response from LLM using Together API"""
|
| 170 |
+
if client is None:
|
| 171 |
+
logger.error("Together client not initialized. Cannot get LLM response.")
|
| 172 |
+
return "Sorry, I cannot access the language model at the moment. Please ensure the API key is set correctly."
|
| 173 |
+
|
| 174 |
+
try:
|
| 175 |
+
response = client.chat.completions.create(
|
| 176 |
+
model=config.MODEL_NAME,
|
| 177 |
+
messages=messages
|
| 178 |
+
)
|
| 179 |
+
return response.choices[0].message.content
|
| 180 |
+
except AttributeError:
|
| 181 |
+
# Handle older version of together library
|
| 182 |
+
try:
|
| 183 |
+
response = client.completions.create(
|
| 184 |
+
model=config.MODEL_NAME,
|
| 185 |
+
prompt=messages[-1]["content"],
|
| 186 |
+
max_tokens=1000
|
| 187 |
+
)
|
| 188 |
+
return response.choices[0].text
|
| 189 |
+
except Exception as e:
|
| 190 |
+
logger.error(f"Error with fallback API call: {e}")
|
| 191 |
+
return "Sorry, I encountered an error while processing your request."
|
| 192 |
+
except Exception as e:
|
| 193 |
+
logger.error(f"Error calling LLM API: {e}")
|
| 194 |
+
return "Sorry, I encountered an error while processing your request."
|
| 195 |
+
|
| 196 |
+
@traceable(run_type="chain", name="Response Generator")
|
| 197 |
+
def generate_response(query):
|
| 198 |
+
"""Generate a response based on the query type"""
|
| 199 |
+
if is_personal_query(query):
|
| 200 |
+
# Personal query - use RAG approach
|
| 201 |
+
context = get_relevant_context(query)
|
| 202 |
+
logger.info(f"Retrieved context: {context[:200]}...")
|
| 203 |
+
|
| 204 |
+
messages = [
|
| 205 |
+
{"role": "system", "content": config.PERSONAL_SYSTEM_PROMPT},
|
| 206 |
+
{"role": "user", "content": f"Context about Swayam:\n{context}\n\nQuestion: {query}"}
|
| 207 |
+
]
|
| 208 |
+
|
| 209 |
+
response = get_llm_response(messages)
|
| 210 |
+
return {"response": response, "type": "personal"}
|
| 211 |
+
else:
|
| 212 |
+
# General query - use LLM directly
|
| 213 |
+
messages = [
|
| 214 |
+
{"role": "system", "content": config.GENERAL_SYSTEM_PROMPT},
|
| 215 |
+
{"role": "user", "content": query}
|
| 216 |
+
]
|
| 217 |
+
|
| 218 |
+
response = get_llm_response(messages)
|
| 219 |
+
return {"response": response, "type": "general"}
|