File size: 5,287 Bytes
a52bae4 | 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 | # tools.py
# Three tiny tools the agent can call. Fake weather data so no extra API key is needed.
FAKE_WEATHER = {
"mumbai": "32 C, sunny, humid",
"london": "14 C, cloudy, light rain",
"tokyo": "21 C, clear skies",
"new york": "18 C, partly cloudy",
"paris": "16 C, overcast",
}
def add(a: float, b: float) -> str:
return f"{a + b}"
def multiply(a: float, b: float) -> str:
return f"{a * b}"
def get_weather(city: str) -> str:
return FAKE_WEATHER.get(
city.lower(),
f"Weather for {city}: 25 C, partly cloudy (demo data)",
)
# ----------------------------------------------------------------
# ML example tools — wrap the helpers from examples.py so the agent
# can search the paper catalog, look up a paper, or list all papers.
# ----------------------------------------------------------------
from examples import search_examples, get_paper_info, list_papers
def search_ml_examples(query: str) -> str:
"""Search the ML paper sentence catalog by keyword."""
matches = search_examples(query)
if not matches:
return f"No sentences matching '{query}'."
lines = [f"Found {len(matches)} match(es):"]
for m in matches[:5]:
lines.append(
f"- [{m['label']}] \"{m['sentence']}\" "
f"({m['paper_title']}, {m['year']})"
)
return "\n".join(lines)
def ml_paper_info(paper_id: str) -> str:
"""Look up metadata for a specific paper by its id."""
info = get_paper_info(paper_id)
if not info:
return f"No paper with id '{paper_id}'."
return (
f"{info['title']} ({info['year']}) — "
f"id: {info['paper_id']}, sentences in catalog: {info['sentence_count']}"
)
def list_ml_papers() -> str:
"""List every paper in the catalog."""
papers = list_papers()
lines = [f"{len(papers)} papers in catalog:"]
for p in papers:
lines.append(
f"- {p['paper_id']}: {p['title']} ({p['year']}) "
f"— {p['sentence_count']} sentences"
)
return "\n".join(lines)
TOOL_FUNCTIONS = {
"add": add,
"multiply": multiply,
"get_weather": get_weather,
"search_ml_examples": search_ml_examples,
"ml_paper_info": ml_paper_info,
"list_ml_papers": list_ml_papers,
}
TOOL_SCHEMAS = [
{
"type": "function",
"function": {
"name": "add",
"description": "Add two numbers and return the result.",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "First number"},
"b": {"type": "number", "description": "Second number"},
},
"required": ["a", "b"],
},
},
},
{
"type": "function",
"function": {
"name": "multiply",
"description": "Multiply two numbers and return the result.",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "First number"},
"b": {"type": "number", "description": "Second number"},
},
"required": ["a", "b"],
},
},
},
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a given city.",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "City name"},
},
"required": ["city"],
},
},
},
{
"type": "function",
"function": {
"name": "search_ml_examples",
"description": "Search the built-in ML paper sentence catalog. Returns sentences matching the query along with their paper title, year, and label.",
"parameters": {
"type": "object",
"properties": {
"query": {"type": "string", "description": "Keyword or phrase to search for"},
},
"required": ["query"],
},
},
},
{
"type": "function",
"function": {
"name": "ml_paper_info",
"description": "Look up metadata (title, year, sentence count) for a specific ML paper by its id like 'vaswani-2017-attention'.",
"parameters": {
"type": "object",
"properties": {
"paper_id": {"type": "string", "description": "Paper id slug"},
},
"required": ["paper_id"],
},
},
},
{
"type": "function",
"function": {
"name": "list_ml_papers",
"description": "List every ML paper in the built-in catalog with its id, title, year, and sentence count.",
"parameters": {
"type": "object",
"properties": {},
},
},
},
]
|