Spaces:
Sleeping
Sleeping
Update llm_handler.py
Browse files- 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 |
-
#
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 156 |
-
""
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
# Add chat history
|
| 162 |
-
chat_history = get_chat_history(session_id)
|
| 163 |
for msg in chat_history:
|
| 164 |
messages.append(msg)
|
| 165 |
|
| 166 |
-
# Add current user query
|
| 167 |
messages.append({"role": "user", "content": query})
|
| 168 |
|
| 169 |
-
print(f"π Debug -
|
| 170 |
-
for i, msg in enumerate(messages):
|
| 171 |
-
print(f" {i}: {msg['role']}: {msg['content'][:
|
| 172 |
|
| 173 |
try:
|
| 174 |
completion = openrouter_client.chat.completions.create(
|
| 175 |
model="x-ai/grok-4-fast",
|
| 176 |
messages=messages,
|
| 177 |
-
max_tokens=500,
|
| 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 |
|