|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| from langchain.tools import tool
|
| from backend import Rag
|
| import json
|
| import os
|
| import uuid
|
| from datetime import datetime
|
|
|
| TICKETS_FILE = "tickets.json"
|
|
|
| @tool
|
| def search_knowledge_base(query:str)->str:
|
| """
|
| Searches the company knowledge base for answers to customer questions.
|
| Always use this tool FIRST when a customer asks about policies, products, returns, or shipping.
|
| """
|
|
|
| retriever=Rag.get_retriever()
|
| results = retriever.invoke(query)
|
|
|
| combined_context = ""
|
| if not results:
|
|
|
| return "No results found in the knowledge base for that query."
|
|
|
|
|
| combined_context = ""
|
| for i, doc in enumerate(results):
|
| combined_context += f"--- Relevant Chunk {i+1} ---\n{doc.page_content}\n\n"
|
|
|
| return combined_context.strip()
|
|
|
|
|
|
|
|
|
| @tool
|
| def create_support_ticket(issue: str, customer_name: str) -> str:
|
| """
|
| Creates a support ticket for the customer.
|
| Use this tool ONLY if the knowledge base does not have the answer, or if the user explicitly asks to open a ticket.
|
| """
|
|
|
| ticket_id = f"TKT-{str(uuid.uuid4())[:5].upper()}"
|
|
|
| new_ticket = {
|
| "ticket_id": ticket_id,
|
| "customer_name": customer_name,
|
| "issue": issue,
|
| "status": "Open",
|
| "created_at": datetime.now().isoformat()
|
| }
|
|
|
|
|
| tickets = []
|
| if os.path.exists(TICKETS_FILE):
|
| try:
|
| with open(TICKETS_FILE, "r") as f:
|
| tickets = json.load(f)
|
| except json.JSONDecodeError:
|
| pass
|
|
|
|
|
| tickets.append(new_ticket)
|
|
|
|
|
| with open(TICKETS_FILE, "w") as f:
|
| json.dump(tickets, f, indent=4)
|
|
|
| return f"Success! Ticket {ticket_id} has been created for {customer_name}. A support agent will review it shortly."
|
|
|
|
|
|
|
|
|
|
|
| @tool
|
| def escalate_to_human(reason: str) -> str:
|
| """
|
| Escalates the conversation to a human agent.
|
| Use this tool if the customer is angry, explicitly asks to speak to a human, or if you cannot help them after searching the knowledge base.
|
| """
|
|
|
| return f"ESCALATION TRIGGERED: The system has notified a human agent to take over because: {reason}. Please tell the customer a human will be with them shortly." |