import json
from typing import Dict, Any, List, Tuple
from openai import AsyncOpenAI
import getpass
import langwatch
api_key = getpass.getpass("Enter your OpenAI API key: ")
# Initialize OpenAI and LangWatch
client = AsyncOpenAI(api_key=api_key)
langwatch.login()
# Mock database of orders
ORDERS_DB = {
"ORD123": {"status": "processing", "customer_id": "CUST456", "items": ["Product A", "Product B"]},
"ORD456": {"status": "shipped", "customer_id": "CUST789", "items": ["Product C"]},
"ORD789": {"status": "delivered", "customer_id": "CUST456", "items": ["Product D"]}
}
# Mock database of customers
CUSTOMERS_DB = {
"CUST456": {"email": "[email protected]", "name": "John Doe"},
"CUST789": {"email": "[email protected]", "name": "Jane Smith"}
}
# Tool definitions
async def find_customer_by_email(email: str) -> Dict[str, Any]:
"""Find a customer by their email address."""
for customer_id, customer in CUSTOMERS_DB.items():
if customer["email"] == email:
return {"customer_id": customer_id, **customer}
return {"error": "Customer not found"}
async def get_orders_by_customer_id(customer_id: str) -> Dict[str, Any]:
"""Get all orders for a specific customer."""
orders = []
for order_id, order in ORDERS_DB.items():
if order["customer_id"] == customer_id:
orders.append({"order_id": order_id, **order})
return {"orders": orders}
async def get_order_status(order_id: str) -> Dict[str, Any]:
"""Get the status of a specific order."""
if order_id in ORDERS_DB:
return {"order_id": order_id, "status": ORDERS_DB[order_id]["status"]}
return {"error": "Order not found"}
async def update_ticket_status(ticket_id: str, status: str) -> Dict[str, Any]:
"""Update the status of a support ticket."""
return {"ticket_id": ticket_id, "status": status, "updated": True}
async def escalate_to_human() -> Dict[str, Any]:
"""Escalate the current issue to a human agent."""
return {
"status": "escalated",
"message": "A human agent has been notified and will follow up shortly."
}
# Dictionary mapping tool names to functions
TOOL_MAP = {
"find_customer_by_email": find_customer_by_email,
"get_orders_by_customer_id": get_orders_by_customer_id,
"get_order_status": get_order_status,
"update_ticket_status": update_ticket_status,
"escalate_to_human": escalate_to_human
}
# Tool schemas for OpenAI API
TOOL_SCHEMAS = [
{
"type": "function",
"function": {
"name": "find_customer_by_email",
"description": "Find a customer by their email address.",
"parameters": {
"type": "object",
"properties": {
"email": {"type": "string", "description": "Customer email address"}
},
"required": ["email"]
}
}
},
{
"type": "function",
"function": {
"name": "get_orders_by_customer_id",
"description": "Get all orders for a specific customer.",
"parameters": {
"type": "object",
"properties": {
"customer_id": {"type": "string", "description": "Customer ID"}
},
"required": ["customer_id"]
}
}
},
{
"type": "function",
"function": {
"name": "get_order_status",
"description": "Get the status of a specific order.",
"parameters": {
"type": "object",
"properties": {
"order_id": {"type": "string", "description": "Order ID"}
},
"required": ["order_id"]
}
}
},
{
"type": "function",
"function": {
"name": "update_ticket_status",
"description": "Update the status of a support ticket.",
"parameters": {
"type": "object",
"properties": {
"ticket_id": {"type": "string", "description": "Ticket ID"},
"status": {"type": "string", "description": "New status"}
},
"required": ["ticket_id", "status"]
}
}
},
{
"type": "function",
"function": {
"name": "escalate_to_human",
"description": "Escalate the current issue to a human agent.",
"parameters": {
"type": "object",
"properties": {},
"required": []
}
}
}
]