Download agent.py from Ayush-003/ThinkOV2: direct link, hf CLI and curl.
- Browser
- Download file 18.5 kB
-
https://huggingface.co/spaces/Ayush-003/ThinkOV2/resolve/main/agent.py
- Command line
-
hf download hf://spaces/Ayush-003/ThinkOV2/agent.py
-
curl -L -o agent.py https://huggingface.co/spaces/Ayush-003/ThinkOV2/resolve/main/agent.py
18.5 kB
| """ | |
| Intelligent Agent for Grocify Chat | |
| Implements RAG, tool calling, escalation logic, and structured output generation | |
| """ | |
| import json | |
| import logging | |
| from typing import List, Dict, Any, Optional, Tuple | |
| from datetime import datetime | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder | |
| from langchain.schema import HumanMessage, AIMessage, SystemMessage | |
| from langchain.callbacks.base import BaseCallbackHandler | |
| from schemas import ( | |
| ChatContext, AgentResponse, AgentAction, Escalation, AgentBrief, | |
| SourceReference, ProductCarousel, ProductCarouselItem, OrderCard, | |
| QuickReplies, QuickReply, SentimentType, EscalationQueue, Priority, | |
| RetrievedDocument | |
| ) | |
| from config import settings, constants | |
| from tools import tool_registry | |
| from memory import session_manager | |
| logger = logging.getLogger(__name__) | |
| class AgentCallbackHandler(BaseCallbackHandler): | |
| """Custom callback handler for agent execution""" | |
| def __init__(self): | |
| self.tool_calls = [] | |
| self.errors = [] | |
| def on_tool_start(self, tool: str, input_str: str, **kwargs): | |
| logger.info(f"Tool started: {tool}") | |
| self.tool_calls.append({"tool": tool, "input": input_str}) | |
| def on_tool_end(self, output: str, **kwargs): | |
| logger.info(f"Tool completed with output length: {len(str(output))}") | |
| def on_tool_error(self, error: Exception, **kwargs): | |
| logger.error(f"Tool error: {error}") | |
| self.errors.append(str(error)) | |
| class GrocifyAgent: | |
| """Main intelligent agent for Grocify Chat""" | |
| def __init__(self): | |
| self.llm = self._initialize_llm() | |
| self.system_prompt = self._load_system_prompt() | |
| self.callback_handler = AgentCallbackHandler() | |
| def _initialize_llm(self) -> ChatGoogleGenerativeAI: | |
| """Initialize the LLM""" | |
| return ChatGoogleGenerativeAI( | |
| model=settings.LLM_MODEL, | |
| temperature=settings.LLM_TEMPERATURE, | |
| max_tokens=settings.LLM_MAX_TOKENS, | |
| timeout=settings.AGENT_TIMEOUT_SECONDS, | |
| max_retries=2, | |
| ) | |
| def _load_system_prompt(self) -> str: | |
| """Load system prompt""" | |
| # Return the comprehensive system prompt from the user request | |
| return """SYSTEM: You are "Unthinkable Assistant" — an intelligent, safe, sales-aware customer support agent for an e-commerce brand (Diwali/holiday context). Always be helpful, concise, and factual. Use the Retrieval-Augmented-Generation (RAG) results as the ground truth. Do NOT hallucinate. If you lack a relevant retrieved document or a live API response, say so and offer safe next steps. | |
| PRIMARY OBJECTIVES (in order): | |
| 1. Safely resolve user intent where possible using retrieved_docs + tools. | |
| 2. If resolved, return a concise human-friendly answer and the minimal supporting sources. | |
| 3. If not resolvable, escalate via intelligent pathways (explicit request, negative sentiment, repetitive failure, or sensitive topics) and prepare an agent-brief. | |
| 4. For sales flows (gift-finding, promotions), proactively offer relevant CTAs (product cards, add-to-cart links) when confident. | |
| 5. Always respect privacy and never ask for full card numbers, CVV, or passwords in chat. | |
| RETRIEVAL USE RULES: | |
| - Always inspect retrieved_docs first. If top doc score ≥ threshold, you may ground the response in those snippets. | |
| - When forming the final answer, include an inline "sources" array listing id or source name for each factual claim. | |
| - If retrieved_docs are absent or insufficient, call an appropriate tool (product_search, order_lookup, promotions_check, crm_query). Do NOT guess. | |
| TONE & STYLE: | |
| - Friendly, professional, helpful. | |
| - Use short sentences (≤2 lines per message). | |
| - For Indian users, use ₹ for INR and examples in local context where helpful. | |
| - When offering product lists, present 3-5 options max with price and availability. | |
| ESCALATION TRIGGERS (auto-escalate and create agent-brief JSON): | |
| - Explicit user ask: "speak to an agent", "human", "representative". | |
| - sentiment_result == 'angry' or negative with increasing intensity. | |
| - >= 3 failed attempts to resolve same issue. | |
| - Sensitive categories: account compromise, payment fraud, large refunds, legal complaints. | |
| SECURITY & PRIVACY: | |
| - NEVER request or accept full card numbers, CVV, OTPs, or passwords in chat. | |
| - For payment issues, direct user to secure billing workflow and then escalate to billing queue if needed. | |
| OUTPUT FORMAT - You MUST return valid JSON matching this schema: | |
| { | |
| "reply_text": "Your friendly response text", | |
| "confidence": 0.85, | |
| "sources": [{"id": "doc_1", "location": "vector_db"}], | |
| "actions": [], | |
| "structured_response": null, | |
| "escalation": null | |
| } | |
| IMPORTANT: | |
| - "structured_response" is OPTIONAL - only include it when you have actual data for ONE of these types: | |
| * {"type": "product_carousel", "items": [...]} - when showing products | |
| * {"type": "order_card", "order_id": "...", "status": "..."} - when showing order details | |
| * {"type": "quick_replies", "replies": [...]} - when offering quick reply buttons | |
| * {"type": "promotion_card", "coupon": "...", "discount": "...", "description": "..."} - when showing promotions | |
| * {"type": "text"} - for plain text responses | |
| - If you don't have specific data for a structured response, SET IT TO null (not an empty object) | |
| - "actions" is an array of tool calls: [{"tool_name": "product_search", "tool_input": {...}, "priority": 1}] | |
| - "escalation" should be null unless you need to escalate to human agent | |
| """ | |
| async def process_message(self, context: ChatContext) -> AgentResponse: | |
| """Main entry point for processing user messages""" | |
| try: | |
| # Step 1: Check for explicit escalation keywords | |
| if self._should_escalate_immediately(context): | |
| return await self._create_escalation_response(context, reason="explicit_request") | |
| # Step 2: Analyze sentiment if enabled | |
| sentiment = None | |
| if settings.ENABLE_SENTIMENT_ANALYSIS: | |
| sentiment = await self._analyze_sentiment(context.user_message) | |
| if sentiment.sentiment == SentimentType.ANGRY and settings.AUTO_ESCALATE_ON_NEGATIVE: | |
| return await self._create_escalation_response(context, reason="negative_sentiment", sentiment=sentiment) | |
| # Step 3: Build context with RAG and conversation history | |
| prompt_context = await self._build_prompt_context(context) | |
| # Step 4: Generate agent response | |
| agent_response = await self._generate_response(prompt_context, context) | |
| # Step 5: Execute any tool calls requested by agent | |
| if agent_response.actions and settings.ENABLE_TOOL_CALLING: | |
| agent_response = await self._execute_tools(agent_response, context) | |
| # Step 6: Check if failed attempts threshold reached | |
| if agent_response.confidence < 0.5: | |
| failed_count = await session_manager.increment_failed_attempts(context.session_id) | |
| if failed_count >= settings.MAX_FAILED_ATTEMPTS: | |
| return await self._create_escalation_response(context, reason="repeated_failure") | |
| else: | |
| await session_manager.reset_failed_attempts(context.session_id) | |
| # Step 7: Save to conversation history | |
| await session_manager.add_message( | |
| context.session_id, | |
| "user", | |
| context.user_message | |
| ) | |
| await session_manager.add_message( | |
| context.session_id, | |
| "assistant", | |
| agent_response.reply_text, | |
| metadata={"confidence": agent_response.confidence} | |
| ) | |
| return agent_response | |
| except Exception as e: | |
| logger.error(f"Agent processing error: {e}", exc_info=True) | |
| return AgentResponse( | |
| reply_text="I apologize, but I encountered an issue processing your request. Would you like me to connect you with a human agent?", | |
| confidence=0.0, | |
| sources=[], | |
| actions=[], | |
| debug={"error": str(e)} if settings.ENABLE_DEBUG_LOGS else None | |
| ) | |
| def _should_escalate_immediately(self, context: ChatContext) -> bool: | |
| """Check if message contains explicit escalation keywords""" | |
| message_lower = context.user_message.lower() | |
| return any(keyword in message_lower for keyword in constants.ESCALATION_KEYWORDS) | |
| async def _analyze_sentiment(self, text: str) -> Any: | |
| """Analyze sentiment of user message""" | |
| from tools import SentimentAnalysisInput | |
| tool = tool_registry.get_tool("sentiment_analysis") | |
| return await tool.execute(SentimentAnalysisInput(text=text)) | |
| async def _build_prompt_context(self, context: ChatContext) -> str: | |
| """Build comprehensive prompt context""" | |
| parts = [] | |
| # Add system prompt | |
| parts.append(self.system_prompt) | |
| parts.append("\n" + "="*80 + "\n") | |
| # Add context information | |
| parts.append(f"USER MESSAGE: {context.user_message}") | |
| parts.append(f"SESSION ID: {context.session_id}") | |
| parts.append(f"LOCALE: {context.locale}") | |
| parts.append(f"TIMEZONE: {context.timezone}") | |
| # Add conversation history | |
| if context.convo_history: | |
| parts.append("\nCONVERSATION HISTORY:") | |
| for turn in context.convo_history[-10:]: # Last 10 turns | |
| parts.append(f"{turn['role'].upper()}: {turn['content']}") | |
| # Add retrieved documents (RAG) | |
| if context.retrieved_docs: | |
| parts.append("\nRETRIEVED DOCUMENTS:") | |
| for i, doc in enumerate(context.retrieved_docs[:5], 1): | |
| parts.append(f"[{i}] (score: {doc.score:.2f}) {doc.text_snippet[:200]}...") | |
| parts.append(f" Source: {doc.source}, ID: {doc.id}") | |
| # Add available tools | |
| if context.available_tools: | |
| parts.append(f"\nAVAILABLE TOOLS: {', '.join(context.available_tools)}") | |
| # Add memory summary if available | |
| if context.memory_summary: | |
| parts.append(f"\nUSER PROFILE SUMMARY: {context.memory_summary}") | |
| # Add tool responses if any | |
| if context.tool_responses: | |
| parts.append("\nPREVIOUS TOOL RESULTS:") | |
| parts.append(json.dumps(context.tool_responses, indent=2)) | |
| parts.append("\n" + "="*80) | |
| parts.append("\nYour task: Analyze the above context and generate a structured JSON response following the output format specified in the system prompt.") | |
| return "\n".join(parts) | |
| async def _generate_response(self, prompt: str, context: ChatContext) -> AgentResponse: | |
| """Generate response using LLM""" | |
| try: | |
| # Call LLM | |
| response = await self.llm.ainvoke([HumanMessage(content=prompt)]) | |
| response_text = response.content | |
| # Try to parse as JSON | |
| try: | |
| # Clean markdown code blocks if present | |
| if "```json" in response_text: | |
| response_text = response_text.split("```json")[1].split("```")[0].strip() | |
| elif "```" in response_text: | |
| response_text = response_text.split("```")[1].split("```")[0].strip() | |
| response_data = json.loads(response_text) | |
| # Ensure structured_response is either None or has proper type field | |
| if "structured_response" in response_data: | |
| sr = response_data["structured_response"] | |
| if sr is not None and not isinstance(sr, dict): | |
| response_data["structured_response"] = None | |
| elif sr is not None and "type" not in sr: | |
| # Default to text type for plain responses | |
| response_data["structured_response"] = {"type": "text"} | |
| # Ensure escalation is properly formatted | |
| if "escalation" in response_data: | |
| esc = response_data["escalation"] | |
| if esc is not None and not isinstance(esc, dict): | |
| response_data["escalation"] = None | |
| # Parse into AgentResponse | |
| return AgentResponse(**response_data) | |
| except json.JSONDecodeError as je: | |
| # Fallback: treat as plain text response | |
| logger.warning(f"LLM response not in JSON format: {je}, using fallback") | |
| return AgentResponse( | |
| reply_text=response_text, | |
| confidence=0.6, | |
| sources=[], | |
| actions=[], | |
| structured_response={"type": "text"} | |
| ) | |
| except Exception as pe: | |
| # Pydantic validation error - log details and return fallback | |
| logger.error(f"Pydantic validation error: {pe}") | |
| logger.debug(f"Response data that failed validation: {json.dumps(response_data, indent=2)}") | |
| # Try to extract at least the reply_text if available | |
| reply = response_data.get("reply_text", "I apologize, I'm having trouble formatting my response. How else can I help you?") | |
| return AgentResponse( | |
| reply_text=reply, | |
| confidence=response_data.get("confidence", 0.5), | |
| sources=[], | |
| actions=[], | |
| structured_response={"type": "text"} | |
| ) | |
| except Exception as e: | |
| logger.error(f"LLM generation error: {e}") | |
| raise | |
| async def _execute_tools(self, response: AgentResponse, context: ChatContext) -> AgentResponse: | |
| """Execute tool calls requested by agent""" | |
| tool_results = {} | |
| for action in response.actions: | |
| try: | |
| logger.info(f"Executing tool: {action.tool_name}") | |
| result = await tool_registry.execute_tool(action.tool_name, action.tool_input) | |
| # Convert Pydantic models to dicts for JSON serialization | |
| tool_results[action.tool_name] = self._serialize_result(result) | |
| except Exception as e: | |
| logger.error(f"Tool execution failed for {action.tool_name}: {e}") | |
| tool_results[action.tool_name] = {"error": str(e)} | |
| # If we have tool results, regenerate response with tool outputs | |
| if tool_results: | |
| context.tool_responses = tool_results | |
| prompt_context = await self._build_prompt_context(context) | |
| response = await self._generate_response(prompt_context, context) | |
| return response | |
| def _serialize_result(self, result: Any) -> Any: | |
| """Serialize Pydantic models and lists to JSON-compatible format""" | |
| if isinstance(result, list): | |
| # Handle list of Pydantic models | |
| return [self._serialize_result(item) for item in result] | |
| elif hasattr(result, 'model_dump'): | |
| # Pydantic v2 | |
| return result.model_dump() | |
| elif hasattr(result, 'dict'): | |
| # Pydantic v1 | |
| return result.dict() | |
| elif isinstance(result, dict): | |
| # Already a dict, but check nested values | |
| return {k: self._serialize_result(v) for k, v in result.items()} | |
| else: | |
| # Primitive type or already serializable | |
| return result | |
| async def _create_escalation_response( | |
| self, | |
| context: ChatContext, | |
| reason: str, | |
| sentiment: Optional[Any] = None | |
| ) -> AgentResponse: | |
| """Create escalation response with agent brief""" | |
| # Increment escalation counter | |
| await session_manager.increment_escalation_count(context.session_id) | |
| # Determine queue and priority | |
| queue = EscalationQueue.SUPPORT | |
| priority = Priority.NORMAL | |
| if reason == "negative_sentiment" or (sentiment and sentiment.sentiment == SentimentType.ANGRY): | |
| priority = Priority.HIGH | |
| if any(word in context.user_message.lower() for word in constants.SENSITIVE_TOPICS): | |
| queue = EscalationQueue.FRAUD | |
| priority = Priority.HIGH | |
| # Build agent brief | |
| agent_brief = AgentBrief( | |
| summary=f"Escalation triggered: {reason}. User message: {context.user_message[:100]}", | |
| user_identifiers={ | |
| "user_id": context.user_id or "unknown", | |
| "session_id": context.session_id, | |
| "locale": context.locale | |
| }, | |
| conversation_snippets=[ | |
| f"{turn['role']}: {turn['content'][:100]}" | |
| for turn in context.convo_history[-5:] | |
| ], | |
| retrieved_docs=context.retrieved_docs[:3], | |
| tools_used=[], | |
| suggested_actions=[ | |
| "Review conversation history", | |
| "Verify user identity", | |
| "Provide personalized assistance" | |
| ] | |
| ) | |
| escalation = Escalation( | |
| queue=queue, | |
| priority=priority, | |
| agent_brief=agent_brief | |
| ) | |
| return AgentResponse( | |
| reply_text="I understand you'd like to speak with someone. Let me connect you with one of our team members who can help you better. They'll be with you shortly.", | |
| confidence=0.9, | |
| sources=[], | |
| actions=[], | |
| escalation=escalation | |
| ) | |
| # Global agent instance | |
| agent = GrocifyAgent() | |