"""The sample billing tools as LangChain tools (the same 9 as ../catalog.json), with canned results.""" import json from langchain.tools import tool CUSTOMERS = {"globex": {"customer_id": "cus_8812", "name": "Globex", "plan": "growth"}, "initech": {"customer_id": "cus_1207", "name": "Initech", "plan": "starter"}} @tool def find_customer(query: str) -> str: """Find a customer by name, email or domain. Returns the customer id.""" for key, customer in CUSTOMERS.items(): if key in query.lower(): return json.dumps(customer) return json.dumps({"error": "no customer matches " + query}) @tool def get_customer_invoices(customer_id: str, status: str = "any") -> str: """List a customer's invoices, newest first. Returns invoice ids, amounts and statuses.""" return json.dumps([{"invoice_id": "INV-1042", "amount": 490, "status": "paid", "customer_id": customer_id}, {"invoice_id": "INV-0977", "amount": 490, "status": "paid", "customer_id": customer_id}]) @tool def refund_invoice(invoice_id: str, amount: float | None = None) -> str: """Refund a paid invoice fully or partially.""" return json.dumps({"refund_id": "re_301", "invoice_id": invoice_id, "amount": amount or 490}) @tool def create_invoice(customer_id: str, amount: float, description: str) -> str: """Create and send a one-off invoice to a customer.""" return json.dumps({"invoice_id": "INV-1043", "customer_id": customer_id, "amount": amount}) @tool def change_plan(customer_id: str, plan: str) -> str: """Move a customer's subscription to another plan (starter, growth, scale).""" return json.dumps({"customer_id": customer_id, "plan": plan, "effective": "now"}) @tool def cancel_subscription(customer_id: str, at_period_end: bool = True) -> str: """Cancel a customer's subscription, now or at the end of the billing period.""" return json.dumps({"customer_id": customer_id, "cancelled": True, "at_period_end": at_period_end}) @tool def export_report(report: str, format: str = "csv") -> str: """Export a revenue, churn or invoices report as CSV or PDF.""" return json.dumps({"url": f"https://example.com/reports/{report}.{format}"}) @tool def update_tax_settings(country: str, vat_rate: float) -> str: """Change the account's tax settings, such as the VAT rate per country. Admins only.""" return json.dumps({"country": country, "vat_rate": vat_rate}) @tool def ask_user(question: str) -> str: """Ask the user a clarifying question when something needed is missing or ambiguous.""" return "(the question is shown to the user)" TOOLS = [find_customer, get_customer_invoices, refund_invoice, create_invoice, change_plan, cancel_subscription, export_report, update_tax_settings, ask_user] # catalog fields LangChain tools do not carry: which ids a tool returns, roles, always-on helpers, user phrasings EXTRAS = { "find_customer": {"produces": ["customer_id"]}, "get_customer_invoices": {"produces": ["invoice_id"]}, "refund_invoice": {"examples": ["give them their money back", "refund INV-1042"]}, "update_tax_settings": {"requiredRoles": ["admin"]}, "ask_user": {"core": True}, }