Pulastya0 commited on
Commit
508fe7d
Β·
1 Parent(s): 28616a1

Update llm_handler.py

Browse files
Files changed (1) hide show
  1. llm_handler.py +35 -28
llm_handler.py CHANGED
@@ -125,20 +125,29 @@ def get_rag_response(query: str, session_id: str = None) -> tuple[str, str]:
125
  else:
126
  print(f"βœ… Found existing session with {len(chat_sessions[session_id])} messages")
127
 
128
- # 1. Retrieve relevant documents from ChromaDB
129
- query_embedding = encoder.encode([query])[0].tolist()
130
- results = chroma_collection.query(
131
- query_embeddings=[query_embedding],
132
- n_results=3,
133
- )
134
-
135
- # Get the 'metadatas' which contain the full internship details
136
- retrieved_docs = results.get('metadatas', [[]])[0]
137
- context = "\n".join([str(doc) for doc in retrieved_docs])
 
 
 
 
 
 
138
 
139
- # 2. Prepare the conversation with chat history
140
- system_prompt = """
141
- You are a helpful and friendly assistant for the PM Internship Scheme.
 
 
 
142
  Your role is to guide users about internship opportunities, skills required, and preparation tips.
143
 
144
  Rules:
@@ -147,34 +156,32 @@ Rules:
147
  - Keep answers clear, natural, and helpful β€” aim for short but complete responses (3–6 sentences).
148
  - Use a friendly, encouraging tone while staying professional.
149
  - IMPORTANT: Remember the conversation history and provide contextual responses based on what was discussed earlier.
150
- - When user says "the first one", "that internship", "it", etc., refer back to what was mentioned in the conversation history.
151
-
152
- Available internship context for this query:
153
- """ + context + """
154
 
155
- If the context doesn't have the answer, use your own general knowledge to provide a helpful response, even then if you are unable to answer the question, say: "I don't have that information, please check the official PM Internship portal.".
156
- """
157
 
158
- # Build the conversation messages
159
- messages = [{"role": "system", "content": system_prompt}]
 
 
160
 
161
- # Add chat history (this is the crucial part!)
162
- chat_history = get_chat_history(session_id)
163
  for msg in chat_history:
164
  messages.append(msg)
165
 
166
- # Add current user query (without the context prefix this time)
167
  messages.append({"role": "user", "content": query})
168
 
169
- print(f"πŸ” Debug - Messages being sent to LLM:")
170
- for i, msg in enumerate(messages):
171
- print(f" {i}: {msg['role']}: {msg['content'][:100]}...")
172
 
173
  try:
174
  completion = openrouter_client.chat.completions.create(
175
  model="x-ai/grok-4-fast",
176
  messages=messages,
177
- max_tokens=500, # Limit response length
178
  temperature=0.7,
179
  )
180
 
 
125
  else:
126
  print(f"βœ… Found existing session with {len(chat_sessions[session_id])} messages")
127
 
128
+ # Get chat history
129
+ chat_history = get_chat_history(session_id)
130
+ is_first_message = len(chat_history) == 0
131
+
132
+ # Only retrieve context for the first message or when explicitly needed
133
+ context = ""
134
+ if is_first_message or any(word in query.lower() for word in ['internship', 'job', 'opportunity', 'skill', 'apply', 'stipend', 'duration']):
135
+ # Retrieve relevant documents from ChromaDB
136
+ query_embedding = encoder.encode([query])[0].tolist()
137
+ results = chroma_collection.query(
138
+ query_embeddings=[query_embedding],
139
+ n_results=3,
140
+ )
141
+ retrieved_docs = results.get('metadatas', [[]])[0]
142
+ context = "\n".join([str(doc) for doc in retrieved_docs])
143
+ print(f"πŸ” Retrieved context for query (length: {len(context)})")
144
 
145
+ # Build the conversation messages
146
+ messages = []
147
+
148
+ # Add system prompt only for first message or when context is needed
149
+ if is_first_message or context:
150
+ system_content = """You are a helpful and friendly assistant for the PM Internship Scheme.
151
  Your role is to guide users about internship opportunities, skills required, and preparation tips.
152
 
153
  Rules:
 
156
  - Keep answers clear, natural, and helpful β€” aim for short but complete responses (3–6 sentences).
157
  - Use a friendly, encouraging tone while staying professional.
158
  - IMPORTANT: Remember the conversation history and provide contextual responses based on what was discussed earlier.
159
+ - When user says "the first one", "that internship", "it", etc., refer back to what was mentioned in the conversation history."""
 
 
 
160
 
161
+ if context:
162
+ system_content += f"\n\nAvailable internship context for this query:\n{context}"
163
 
164
+ system_content += "\n\nIf the context doesn't have the answer, use your own general knowledge to provide a helpful response, even then if you are unable to answer the question, say: 'I don't have that information, please check the official PM Internship portal.'."
165
+
166
+ messages.append({"role": "system", "content": system_content})
167
+ print(f"πŸ“ Added system prompt (with context: {bool(context)})")
168
 
169
+ # Add chat history
 
170
  for msg in chat_history:
171
  messages.append(msg)
172
 
173
+ # Add current user query
174
  messages.append({"role": "user", "content": query})
175
 
176
+ print(f"πŸ” Debug - Sending {len(messages)} messages to LLM (reduced from full context each time)")
177
+ for i, msg in enumerate(messages[-3:], len(messages)-3): # Show only last 3 messages in debug
178
+ print(f" {i}: {msg['role']}: {msg['content'][:80]}...")
179
 
180
  try:
181
  completion = openrouter_client.chat.completions.create(
182
  model="x-ai/grok-4-fast",
183
  messages=messages,
184
+ max_tokens=500,
185
  temperature=0.7,
186
  )
187