-
-
Notifications
You must be signed in to change notification settings - Fork 139
[refactor]: migrate to cog-based command architecture #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8855701
6fa867c
2892957
6c2fcd4
22536a8
a7d4122
ebb0154
897b5fb
2952ac8
7460279
af5e0bc
ecdc688
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -34,43 +34,66 @@ async def _create_llm_response(state: AgentState, task_result: Dict[str, Any], l | |||||||||||||||||||||||||
| elif state.context.get("original_message"): | ||||||||||||||||||||||||||
| latest_message = state.context["original_message"] | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| conversation_summary = state.conversation_summary or "This is the beginning of our conversation." | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| recent_messages_count = min(10, len(state.messages)) | ||||||||||||||||||||||||||
| conversation_history_str = "\n".join([ | ||||||||||||||||||||||||||
| f"{msg.get('type', 'unknown')}: {msg.get('content', '')}" | ||||||||||||||||||||||||||
| for msg in state.conversation_history[-5:] | ||||||||||||||||||||||||||
| f"{msg.get('role', 'user')}: {msg.get('content', '')}" | ||||||||||||||||||||||||||
| for msg in state.messages[-recent_messages_count:] | ||||||||||||||||||||||||||
| ]) | ||||||||||||||||||||||||||
|
Comment on lines
+39
to
43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Fallback to legacy Older - f"{msg.get('role', 'user')}: {msg.get('content', '')}"
+ f"{msg.get('role', msg.get('type', 'user'))}: {msg.get('content', '')}"📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||
| current_context_str = str(state.context) | ||||||||||||||||||||||||||
| task_type_str = str(task_result.get("type", "N/A")) | ||||||||||||||||||||||||||
| task_details_str = str(task_result) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| total_messages = len(state.messages) | ||||||||||||||||||||||||||
| if total_messages > recent_messages_count: | ||||||||||||||||||||||||||
| conversation_history_str = f"[Showing last {recent_messages_count} of {total_messages} messages]\n" + \ | ||||||||||||||||||||||||||
| conversation_history_str | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| context_parts = [ | ||||||||||||||||||||||||||
| f"Platform: {state.platform}", | ||||||||||||||||||||||||||
| f"Total interactions: {state.interaction_count}", | ||||||||||||||||||||||||||
| f"Session duration: {(state.last_interaction_time - state.session_start_time).total_seconds() / 60:.1f} minutes" | ||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if state.key_topics: | ||||||||||||||||||||||||||
| context_parts.append(f"Key topics discussed: {', '.join(state.key_topics)}") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if state.user_profile: | ||||||||||||||||||||||||||
| context_parts.append(f"User profile: {state.user_profile}") | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| current_context_str = "\n".join(context_parts) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||
| prompt = GENERAL_LLM_RESPONSE_PROMPT.format( | ||||||||||||||||||||||||||
| conversation_summary=conversation_summary, | ||||||||||||||||||||||||||
| latest_message=latest_message, | ||||||||||||||||||||||||||
| conversation_history=conversation_history_str, | ||||||||||||||||||||||||||
| current_context=current_context_str, | ||||||||||||||||||||||||||
| task_type=task_type_str, | ||||||||||||||||||||||||||
| task_details=task_details_str | ||||||||||||||||||||||||||
| task_type=task_result.get("type", "general"), | ||||||||||||||||||||||||||
| task_details=str(task_result) | ||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| logger.info(f"Prompt includes summary: {len(conversation_summary)} chars, " | ||||||||||||||||||||||||||
| f"recent history: {recent_messages_count} messages, " | ||||||||||||||||||||||||||
| f"total history: {total_messages} messages") | ||||||||||||||||||||||||||
| except KeyError as e: | ||||||||||||||||||||||||||
| logger.error(f"Missing key in GENERAL_LLM_RESPONSE_PROMPT: {e}") | ||||||||||||||||||||||||||
| return "Error: Response template formatting error." | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| response = await llm.ainvoke([HumanMessage(content=prompt)]) | ||||||||||||||||||||||||||
| return response.content.strip() | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| async def generate_response_node(state: AgentState, llm) -> AgentState: | ||||||||||||||||||||||||||
| async def generate_response_node(state: AgentState, llm) -> dict: | ||||||||||||||||||||||||||
| """Generate final response to user""" | ||||||||||||||||||||||||||
| logger.info(f"Generating response for session {state.session_id}") | ||||||||||||||||||||||||||
| task_result = state.task_result or {} | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if task_result.get("type") == "faq": | ||||||||||||||||||||||||||
| state.final_response = task_result.get("response", "I don't have a specific answer for that question.") | ||||||||||||||||||||||||||
| final_response = task_result.get("response", "I don't have a specific answer for that question.") | ||||||||||||||||||||||||||
| elif task_result.get("type") == "web_search": | ||||||||||||||||||||||||||
| response = await _create_search_response(task_result) | ||||||||||||||||||||||||||
| state.final_response = response | ||||||||||||||||||||||||||
| final_response = await _create_search_response(task_result) | ||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||
| # Pass the llm instance to _create_llm_response | ||||||||||||||||||||||||||
| response = await _create_llm_response(state, task_result, llm) | ||||||||||||||||||||||||||
| state.final_response = response | ||||||||||||||||||||||||||
| final_response = await _create_llm_response(state, task_result, llm) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| state.current_task = "response_generated" | ||||||||||||||||||||||||||
| return state | ||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||
| "final_response": final_response, | ||||||||||||||||||||||||||
| "current_task": "response_generated" | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger = logging.getLogger(__name__) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def handle_faq_node(state: AgentState, faq_tool) -> AgentState: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def handle_faq_node(state: AgentState, faq_tool) -> dict: | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Handle FAQ requests""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.info(f"Handling FAQ for session {state.session_id}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -16,11 +16,11 @@ async def handle_faq_node(state: AgentState, faq_tool) -> AgentState: | |||||||||||||||||||||||||||||||||||||||||||||||||||
| # faq_tool will be passed from the agent, similar to llm for classify_intent | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| faq_response = await faq_tool.get_response(latest_message) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| state.task_result = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "type": "faq", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "response": faq_response, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "source": "faq_database" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "task_result": { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "type": "faq", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "response": faq_response, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "source": "faq_database" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "current_task": "faq_handled" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+19
to
26
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling around external FAQ call An exception inside - faq_response = await faq_tool.get_response(latest_message)
+ try:
+ faq_response = await faq_tool.get_response(latest_message)
+ except Exception as e:
+ logger.exception("FAQ tool failed")
+ return {
+ "errors": [str(e)],
+ "current_task": "faq_failed"
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| state.current_task = "faq_handled" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return state | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.