File size: 10,494 Bytes
84983c9 a44477f 84983c9 a44477f 400e193 84983c9 a44477f 84983c9 400e193 84983c9 a44477f 84983c9 a44477f 84983c9 a44477f 84983c9 a44477f 84983c9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | """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
|