Wire the order-lookup tool into the support assistant

Python · LLM apps · advanced · greenfield

Wires the support console's order assistant to the database: the model gets the `lookup_orders` tool declaration, and when it asks for a call the endpoint parses the produced arguments, runs the lookup and feeds the result back per the tool-call contract — assistant message first, each result with its matching `tool_call_id` — looping until a final answer or the 5-round cap. An unknown tool name already comes back as a tool error, and provider outages as a clean 502. Exercised email lookups and status-filtered lookups against a seeded orders table and the assistant composed the answers from the returned rows.

Internal assistant on the support console: support agents sign in through the company SSO and are permitted to look up any customer's orders, so the tool needs no per-customer authorization. The model decides when to call the tool and with what arguments. `Order` rows carry `order_id` (int), `customer_email` (str), `status` (one of the four enum values), `total_cents` (int) and `created_at` (non-null timezone-aware `datetime`).

Requirements

Files touched

--- app/order_tools.py
+"""Order-lookup tool: the schema declaration and the DB-backed implementation."""
+
+from __future__ import annotations
+
+from typing import Any
+
+from sqlalchemy import select
+from sqlalchemy.ext.asyncio import AsyncSession
+
+from app.models import Order
+
+ORDER_STATUSES = ("placed", "shipped", "delivered", "cancelled")
+
+# Tool declaration sent with every model call. Per the endpoint contract this
+# schema is also the validation contract for model-produced arguments.
+LOOKUP_ORDERS_TOOL: dict[str, Any] = {
+    "type": "function",
+    "function": {
+        "name": "lookup_orders",
+        "description": (
+            "Look up a customer's orders by exact email address, optionally "
+            "filtered by status."
+        ),
+        "parameters": {
+            "type": "object",
+            "properties": {
+                "customer_email": {
+                    "type": "string",
+                    "minLength": 1,

Review this PR

Python practice