The model picks a tool, the program runs it and adds the result to the history, and the model decides again, until it can answer.
AI · intermediate · 9 min · 38 steps · free
def search(query):
return {'population of chennai': '7 million'}.get(query, 'no result')
def calculator(expression):
a, op, b = expression.split()
return int(a) * int(b) if op == '*' else int(a) + int(b)
TOOLS = {'search': search, 'calculator': calculator}
# Stands in for the language model: it picks the next action from what it has seen so far.
def model(history):
if len(history) == 1:
return {'tool': 'search', 'input': 'population of chennai'}
if len(history) == 3:
return {'tool': 'calculator', 'input': '7 * 2'}
return {'answer': 'About 14 million people live in two cities the size of Chennai.'}
history = [{'role': 'user', 'content': 'How many people live in two cities the size of Chennai?'}]
for turn in range(5):
action = model(history)
if 'answer' in action:
print(action['answer'])
break
result = TOOLS[action['tool']](action['input'])
history.append({'role': 'assistant', 'content': action})
history.append({'role': 'tool', 'content': result})search(). Its body does not run yet: it runs each time the function is called.calculator(). Its body does not run yet: it runs each time the function is called.TOOLS is created and points to a new dict: {search: function search(), calculator: function calculator()}.model decides the next action from the history so far.turn is created with the value 0.model(), with history = [a dict]. It gets its own names.len(history) == 1 is true, so the block under it runs.{'tool': 'search', 'input': 'population of chennai'}.'answer' in action is false, so the block under it is skipped.search(), with query = 'population of chennai'. It gets its own names.{'population of chennai': '7 million'}.get(query, 'no result').history points to.turn changes from 0 to 1.model(), with history = [a dict, a dict, a dict]. It gets its own names.len(history) == 1 is false, so the block under it is skipped.len(history) == 3 is true, so the block under it runs.{'tool': 'calculator', 'input': '7 * 2'}.'answer' in action is false, so the block under it is skipped.calculator(), with expression = '7 * 2'. It gets its own names.a is created with the value '7'. op is created with the value '*'. b is created with the value '2'.int(a) * int(b) if op == '*' else int(a) + int(b).calculator() has finished, so its names are gone. The value it returned, 14, is stored in result.history points to.history points to.turn changes from 1 to 2.model(), with history = [a dict, a dict, a dict, a dict, a dict]. It gets its own names.len(history) == 1 is false, so the block under it is skipped.len(history) == 3 is false, so the block under it is skipped.{'answer': 'About 14 million people live in two cities the size of Chennai.'}.model() has finished, so its names are gone. The value it returned, {answer: 'About 14 million people live in two cities the size of Chennai.'}, is stored in action.About 14 million people live in two cities the size of Chennai..break.Answer: The model only knows what is sent to it
Answer: An answer, or the turn limit
← Tools, Schemas and Validation · Agent Memory →