PR-AGENT / server /agent.py
Seth
Update
400e193
"""OpenAI tool-calling agent — catalog as external memory (no full DB in prompts)."""
from __future__ import annotations
import json
import os
import sqlite3
from typing import Any
from openai import OpenAI
from server.catalog import connect, get_commodity, search_catalog, summarize_row
def _parse_json_response(text: str) -> dict[str, Any] | None:
t = (text or "").strip()
if t.startswith("```"):
lines = t.splitlines()
if lines and lines[0].lstrip().startswith("```"):
lines = lines[1:]
if lines and lines[-1].strip() == "```":
lines = lines[:-1]
t = "\n".join(lines).strip()
try:
out = json.loads(t)
return out if isinstance(out, dict) else None
except json.JSONDecodeError:
return None
def _assistant_message_dict(msg: Any) -> dict[str, Any]:
d: dict[str, Any] = {"role": "assistant", "content": msg.content}
if getattr(msg, "tool_calls", None):
d["tool_calls"] = [
{
"id": tc.id,
"type": "function",
"function": {
"name": tc.function.name,
"arguments": tc.function.arguments or "{}",
},
}
for tc in msg.tool_calls
]
return d
SYSTEM = """You are ProcureMind, an AI procurement assistant with access to the procurement catalogue through tools only.
Rules:
- Never invent catalogue codes. Every code must come from tool results.
- Call `search_catalog` with **short procurement phrases** (about 2–6 keywords), not a pasted paragraph. Example: "consumer research surveys" or "market analysis studies" rather than the user's full sentence.
- **Retry before giving up:** If matches are empty or a poor fit, run additional `search_catalog` calls with broader or alternate terms (e.g. consumer research, marketing studies, survey services, statistical analysis, opinion polls). UNSPSC uses formal titles — paraphrase the user's intent into catalogue-style wording.
- If results are ambiguous but plausible, return status "choose" with 2–6 distinct candidates rather than "not_found".
- Only return status "not_found" after **at least two** distinct searches still yield nothing usable; then suggest narrower catalogue-style keywords.
- When you have a single best commodity, return status "found" with that commodity_code (integer).
- Always include human-readable code references: segment / family / class / commodity integers exactly as returned by tools.
Final reply MUST be a single JSON object (no markdown fences) with this shape:
{
"status": "found" | "choose" | "not_found",
"summary": "short natural language for the user",
"commodity_code": null or integer,
"candidates": [
{"commodity_code": 80141501, "path": "Segment > ... > Commodity", "reason": "why it matches"}
],
"analysis_rows": [
{"icon": "inventory_2", "left": "...", "right": "FOUND", "right_style": "success"},
{"icon": "verified_user", "left": "...", "right": "CDW, B&H", "right_style": "muted"}
],
"form_intro": "One paragraph telling the user to confirm line items for the selected catalogue item."
}
Use right_style: "success" (green), "muted" (grey), or "neutral".
Icons must be Material symbol names: inventory_2, verified_user, travel_explore, psychology, etc.
"""
TOOLS: list[dict[str, Any]] = [
{
"type": "function",
"function": {
"name": "search_catalog",
"description": "Keyword search in the SQLite procurement catalogue (FTS). Pass 2–6 focused procurement keywords (e.g. market research, survey services). Call again with broader alternate phrases if the first query returns few or zero rows.",
"parameters": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Focused search phrase, e.g. 'market research laptop depot repair'",
},
"limit": {
"type": "integer",
"description": "Max rows to return (default 20)",
},
},
"required": ["query"],
},
},
},
{
"type": "function",
"function": {
"name": "get_commodity_details",
"description": "Fetch one commodity by exact 8-digit catalogue commodity code.",
"parameters": {
"type": "object",
"properties": {
"commodity_code": {
"type": "integer",
"description": "Catalogue commodity code, e.g. 80141501",
}
},
"required": ["commodity_code"],
},
},
},
]
def _dispatch(
conn: sqlite3.Connection, name: str, args: dict[str, Any]
) -> Any:
if name == "search_catalog":
q = str(args.get("query") or "")
limit = int(args.get("limit") or 20)
rows = search_catalog(conn, q, limit=limit)
return {"matches": [summarize_row(r) for r in rows]}
if name == "get_commodity_details":
code = int(args.get("commodity_code"))
row = get_commodity(conn, code)
if not row:
return {"error": "not_found", "commodity_code": code}
return {"commodity": summarize_row(row)}
return {"error": "unknown_tool", "name": name}
def run_agent(
user_message: str,
*,
conn: sqlite3.Connection | None = None,
selected_code: int | None = None,
) -> dict[str, Any]:
"""Return parsed JSON object from model after tool loop."""
own_conn = conn is None
if conn is None:
conn = connect()
try:
if selected_code is not None:
row = get_commodity(conn, int(selected_code))
if not row:
return {
"status": "not_found",
"summary": "That commodity code is not in the local catalog.",
"commodity_code": None,
"candidates": [],
"analysis_rows": [],
"form_intro": "",
}
s = summarize_row(row)
return {
"status": "found",
"summary": f"Selected catalogue item {s['commodity_code']}: {s['path']}",
"commodity_code": s["commodity_code"],
"candidates": [],
"analysis_rows": [
{
"icon": "inventory_2",
"left": f"Catalog match: {s['path']}",
"right": "FOUND",
"right_style": "success",
},
{
"icon": "verified_user",
"left": "Cross-referencing approved vendors (configure in procurement policy)",
"right": "—",
"right_style": "muted",
},
],
"form_intro": (
"Review the specification fields for this catalogue item. "
"Confirm selections before generating the requisition."
),
"selected_details": s,
}
api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
return {
"status": "not_found",
"summary": "Server misconfiguration: OPENAI_API_KEY is not set.",
"commodity_code": None,
"candidates": [],
"analysis_rows": [],
"form_intro": "",
"error": "missing_openai_key",
}
client = OpenAI(api_key=api_key)
messages: list[dict[str, Any]] = [
{"role": "system", "content": SYSTEM},
{"role": "user", "content": user_message},
]
for _ in range(10):
resp = client.chat.completions.create(
model=os.environ.get("OPENAI_MODEL", "gpt-4o-mini"),
messages=messages,
tools=TOOLS,
tool_choice="auto",
temperature=0.2,
)
msg = resp.choices[0].message
if msg.tool_calls:
messages.append(_assistant_message_dict(msg))
for tc in msg.tool_calls:
name = tc.function.name
try:
args = json.loads(tc.function.arguments or "{}")
except json.JSONDecodeError:
args = {}
result = _dispatch(conn, name, args)
messages.append(
{
"role": "tool",
"tool_call_id": tc.id,
"content": json.dumps(result, ensure_ascii=False),
}
)
continue
text = (msg.content or "").strip()
parsed = _parse_json_response(text)
if parsed:
return parsed
return {
"status": "not_found",
"summary": text or "Unable to parse agent response.",
"commodity_code": None,
"candidates": [],
"analysis_rows": [],
"form_intro": "",
"raw": text,
}
return {
"status": "not_found",
"summary": "Agent stopped after too many tool rounds.",
"commodity_code": None,
"candidates": [],
"analysis_rows": [],
"form_intro": "",
}
finally:
if own_conn:
conn.close()
def coerce_payload(payload: dict[str, Any]) -> dict[str, Any]:
"""Ensure required keys exist."""
base = {
"status": payload.get("status") or "not_found",
"summary": payload.get("summary") or "",
"commodity_code": payload.get("commodity_code"),
"candidates": payload.get("candidates") or [],
"analysis_rows": payload.get("analysis_rows") or [],
"form_intro": payload.get("form_intro") or "",
}
if "selected_details" in payload:
base["selected_details"] = payload["selected_details"]
if "error" in payload:
base["error"] = payload["error"]
return base