A stand-in model that chooses tools
An agent needs a model to choose a tool. Instead of a paid one, write a stand-in: a function that reads the message and offered tools and picks a call.
import re
def choose_tool(message, model_tools):
"""Decide which tool to call, and with what, from the message alone."""
offered = {tool["function"]["name"] for tool in model_tools}
order = re.search(r"\b[A-Z]\d+\b", message)
text = message.lower()
if order and "refund" in text and "refund_order" in offered:
return {"name": "refund_order", "arguments": {"order_id": order.group(), "reason": message}}
if order and "lookup_order" in offered:
return {"name": "lookup_order", "arguments": {"order_id": order.group()}}
if "search_help" in offered:
return {"name": "search_help", "arguments": {"query": text.split()[-1].strip("?.!")}}
return NoneIt returns what a model returns when it calls a tool: a name and arguments, or None for no tool. re.search(r"\b[A-Z]\d+\b", ...) finds an order id like A17: a capital letter followed by digits, as a whole word.
It only calls tools that were offered, as a real model can only call tools in its request. Take refund_order out of the list and it falls back to a lookup. That rule is what makes the offered tool list matter.
offered = [{"type": "function", "function": {"name": name}} for name in ("lookup_order", "search_help", "refund_order")]
for message in [
"Where is my order B42?",
"Please refund order A17, it arrived broken",
"How do I reset my password?",
]:
print(choose_tool(message, offered))only_lookup = [{"type": "function", "function": {"name": "lookup_order"}}]
print(choose_tool("Please refund order A17, it arrived broken", only_lookup))
print(choose_tool("How do I reset my password?", only_lookup))With only lookup_order offered, the refund request becomes a lookup and the password question gets no tool. A real model makes the same kind of choice from the descriptions, with far better judgement and far less predictability, which is why the next lessons check what it does.
- Add a rule: a message containing
"article"callssearch_helpwith the next word. - Send
"order a17"in lower case. Why is no order found, and should the regex change? - Return
Nonefor any message over 200 characters.
Every expert started right here.