|
|
|
|
|
from llm_router import LLMRouter |
|
|
|
|
|
class ChainOfThoughtIntentRecognizer: |
|
|
def __init__(self, llm_router: LLMRouter): |
|
|
self.llm_router = llm_router |
|
|
self.cot_templates = self._load_cot_templates() |
|
|
|
|
|
async def recognize_intent(self, user_input: str, context: dict) -> dict: |
|
|
""" |
|
|
Multi-step reasoning for intent recognition |
|
|
""" |
|
|
|
|
|
initial_analysis = await self._step1_initial_classification(user_input) |
|
|
|
|
|
|
|
|
refined_analysis = await self._step2_contextual_refinement( |
|
|
user_input, initial_analysis, context |
|
|
) |
|
|
|
|
|
|
|
|
final_intent = await self._step3_confidence_calibration(refined_analysis) |
|
|
|
|
|
return self._format_intent_output(final_intent) |
|
|
|
|
|
async def _step1_initial_classification(self, user_input: str) -> dict: |
|
|
cot_prompt = f""" |
|
|
Let's think step by step about the user's intent: |
|
|
|
|
|
User input: "{user_input}" |
|
|
|
|
|
Step 1: Identify key entities and actions mentioned |
|
|
Step 2: Map to common intent categories |
|
|
Step 3: Estimate confidence for each category |
|
|
|
|
|
Categories: [information_request, task_execution, creative_generation, |
|
|
analysis_research, casual_conversation, troubleshooting] |
|
|
""" |
|
|
|
|
|
return await self.llm_router.route_inference( |
|
|
"intent_classification", cot_prompt |
|
|
) |
|
|
|
|
|
async def _step2_contextual_refinement(self, user_input: str, initial_analysis: dict, context: dict) -> dict: |
|
|
""" |
|
|
Refine intent classification based on conversation context |
|
|
""" |
|
|
|
|
|
return initial_analysis |
|
|
|
|
|
async def _step3_confidence_calibration(self, analysis: dict) -> dict: |
|
|
""" |
|
|
Calibrate confidence scores for final intent decision |
|
|
""" |
|
|
|
|
|
return analysis |
|
|
|
|
|
def _format_intent_output(self, intent_data: dict) -> dict: |
|
|
""" |
|
|
Format intent recognition output with confidence scores |
|
|
""" |
|
|
|
|
|
return { |
|
|
"intent": intent_data.get("intent", "unknown"), |
|
|
"confidence": intent_data.get("confidence", 0.0), |
|
|
"reasoning_steps": intent_data.get("reasoning_steps", []) |
|
|
} |
|
|
|
|
|
def _load_cot_templates(self) -> dict: |
|
|
""" |
|
|
Load Chain of Thought templates for different intent types |
|
|
""" |
|
|
return { |
|
|
"information_request": """Let's analyze: {user_input} |
|
|
Step 1: What information is the user seeking? |
|
|
Step 2: Is it factual, procedural, or explanatory? |
|
|
Step 3: What level of detail is appropriate?""", |
|
|
|
|
|
"task_execution": """Let's analyze: {user_input} |
|
|
Step 1: What action does the user want to perform? |
|
|
Step 2: What are the required parameters? |
|
|
Step 3: Are there any constraints or preferences?""", |
|
|
|
|
|
"creative_generation": """Let's analyze: {user_input} |
|
|
Step 1: What type of creative content is needed? |
|
|
Step 2: What style, tone, and format? |
|
|
Step 3: What constraints or guidelines apply?""" |
|
|
} |
|
|
|
|
|
|