Spaces:
Sleeping
Sleeping
Sohan Kshirsagar commited on
Commit ·
ebc9c2d
1
Parent(s): 00c68fb
Revelance based persona ranking and response order
Browse files
multi_llm_chatbot_backend/app/core/improved_orchestrator.py
CHANGED
|
@@ -4,6 +4,9 @@ from app.core.session_manager import ConversationContext, get_session_manager
|
|
| 4 |
from app.core.context_manager import get_context_manager
|
| 5 |
from app.core.rag_manager import get_rag_manager
|
| 6 |
from app.llm.llm_client import LLMClient
|
|
|
|
|
|
|
|
|
|
| 7 |
import logging
|
| 8 |
import re
|
| 9 |
|
|
@@ -404,4 +407,72 @@ class ImprovedChatOrchestrator:
|
|
| 404 |
|
| 405 |
except Exception as e:
|
| 406 |
logger.error(f"Error retrieving documents for {persona_id}: {str(e)}")
|
| 407 |
-
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from app.core.context_manager import get_context_manager
|
| 5 |
from app.core.rag_manager import get_rag_manager
|
| 6 |
from app.llm.llm_client import LLMClient
|
| 7 |
+
from app.models.default_personas import is_valid_persona_id
|
| 8 |
+
|
| 9 |
+
import json
|
| 10 |
import logging
|
| 11 |
import re
|
| 12 |
|
|
|
|
| 407 |
|
| 408 |
except Exception as e:
|
| 409 |
logger.error(f"Error retrieving documents for {persona_id}: {str(e)}")
|
| 410 |
+
return ""
|
| 411 |
+
|
| 412 |
+
|
| 413 |
+
async def get_top_personas(self, session_id: str, k: int = 3) -> List[str]:
|
| 414 |
+
"""
|
| 415 |
+
Use the LLM to rank personas based on current session context.
|
| 416 |
+
Falls back to default persona order if LLM fails or returns invalid data.
|
| 417 |
+
"""
|
| 418 |
+
try:
|
| 419 |
+
session = self.session_manager.get_session(session_id)
|
| 420 |
+
|
| 421 |
+
if not self.personas:
|
| 422 |
+
logger.warning("No personas registered.")
|
| 423 |
+
return []
|
| 424 |
+
|
| 425 |
+
# Use the LLM from one of the existing persona objects
|
| 426 |
+
llm = next(iter(self.personas.values())).llm
|
| 427 |
+
|
| 428 |
+
# Use recent conversation context (last 5 messages)
|
| 429 |
+
recent_context = "\n".join(
|
| 430 |
+
msg['content'] for msg in session.get_recent_messages(5)
|
| 431 |
+
)
|
| 432 |
+
|
| 433 |
+
# Format available persona descriptions
|
| 434 |
+
persona_descriptions = "\n".join([
|
| 435 |
+
f"- ID: {p.id}\n Name: {p.name}\n Prompt: {p.system_prompt.strip()}"
|
| 436 |
+
for p in self.personas.values()
|
| 437 |
+
])
|
| 438 |
+
|
| 439 |
+
prompt = f"""
|
| 440 |
+
The user is seeking PhD advice. Based on the conversation below, choose the top {k} most relevant advisors.
|
| 441 |
+
|
| 442 |
+
Respond ONLY with a JSON list of exactly {k} advisor IDs in order of relevance.
|
| 443 |
+
Example response: ["methodist", "pragmatist", "theorist"]
|
| 444 |
+
|
| 445 |
+
--- Conversation ---
|
| 446 |
+
{recent_context}
|
| 447 |
+
|
| 448 |
+
--- Available Advisors ---
|
| 449 |
+
{persona_descriptions}
|
| 450 |
+
""".strip()
|
| 451 |
+
|
| 452 |
+
llm_response = await llm.generate(
|
| 453 |
+
system_prompt="You are an assistant that selects the best advisors for a PhD student.",
|
| 454 |
+
context=[{"role": "user", "content": prompt}],
|
| 455 |
+
temperature=0.4,
|
| 456 |
+
max_tokens=150
|
| 457 |
+
)
|
| 458 |
+
|
| 459 |
+
# Step 1: Try direct JSON load
|
| 460 |
+
try:
|
| 461 |
+
top_ids = json.loads(llm_response.strip())
|
| 462 |
+
except json.JSONDecodeError:
|
| 463 |
+
# Step 2: Fallback: try extracting list of quoted strings
|
| 464 |
+
top_ids = re.findall(r'"(.*?)"', llm_response)
|
| 465 |
+
logger.warning(f"Fallback JSON extraction used: {top_ids}")
|
| 466 |
+
|
| 467 |
+
# Step 3: Filter valid persona IDs
|
| 468 |
+
valid_ids = [pid for pid in top_ids if pid in self.personas]
|
| 469 |
+
|
| 470 |
+
if len(valid_ids) < k:
|
| 471 |
+
logger.warning(f"LLM returned insufficient or invalid IDs. Got: {valid_ids}")
|
| 472 |
+
return list(self.personas.keys())[:k]
|
| 473 |
+
|
| 474 |
+
return valid_ids[:k]
|
| 475 |
+
|
| 476 |
+
except Exception as e:
|
| 477 |
+
logger.error(f"Error selecting top personas: {e}")
|
| 478 |
+
return list(self.personas.keys())[:k]
|