Spaces:
Sleeping
Sleeping
File size: 3,424 Bytes
6bedc74 | 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 | from typing import Any, Callable, Dict
from src.tools.calculator import calculator
from src.tools.database import search_inventory
from src.tools.external_api import lookup_country
ToolFunction = Callable[..., Dict[str, Any]]
TOOL_REGISTRY: Dict[str, Dict[str, Any]] = {
"calculator": {
"function": calculator,
"description": (
"Safely evaluate a mathematical expression. "
"Use this tool for arithmetic and calculations."
),
"parameters": {
"type": "object",
"properties": {
"expression": {
"type": "string",
"description": (
"A mathematical expression such as "
"'125 * 4' or '(100 / 4) + 7'."
),
}
},
"required": ["expression"],
"additionalProperties": False,
},
},
"search_inventory": {
"function": search_inventory,
"description": (
"Search the local inventory database by item name, "
"category, or both."
),
"parameters": {
"type": "object",
"properties": {
"item_name": {
"type": "string",
"description": (
"Optional item name or partial item name."
),
},
"category": {
"type": "string",
"description": (
"Optional exact inventory category such as "
"'Electronics', 'Furniture', or "
"'Office Supplies'."
),
},
},
"additionalProperties": False,
},
},
"lookup_country": {
"function": lookup_country,
"description": (
"Look up structured information about a country "
"using the World Bank Countries API."
),
"parameters": {
"type": "object",
"properties": {
"country_name": {
"type": "string",
"description": (
"The country name to look up, such as "
"'Japan' or 'Canada'."
),
}
},
"required": ["country_name"],
"additionalProperties": False,
},
},
}
def get_tool(name: str) -> ToolFunction:
"""Return an approved tool function by name."""
if name not in TOOL_REGISTRY:
raise ValueError(
f"Tool '{name}' is not registered."
)
return TOOL_REGISTRY[name]["function"]
def get_tool_definitions() -> list[Dict[str, Any]]:
"""
Return tool definitions in OpenAI-compatible function-call format.
"""
definitions = []
for name, config in TOOL_REGISTRY.items():
definitions.append(
{
"type": "function",
"function": {
"name": name,
"description": config["description"],
"parameters": config["parameters"],
},
}
)
return definitions
def list_tools() -> list[str]:
"""Return the names of all approved tools."""
return sorted(TOOL_REGISTRY.keys()) |