Send the Result Back چالش 4 از جلسهی 69، متوسط
یک چرخهی کامل: جواب اول مدل یک درخواست ابزار است. ابزار را اجرا کن، پیام مدل را با tool_calls و بعد یک پیام با نقش tool، همان tool_call_id و نتیجه به عنوان content به messages اضافه کن و مدل را دوباره صدا بزن. متن جواب نهایی را چاپ کن. خروجی:The weather in Tabriz is sunny, 18 degrees.
import json
WEATHER = {"Tabriz": "sunny, 18 degrees", "Rasht": "rainy, 12 degrees"}
def get_weather(city):
return WEATHER.get(city, "unknown")
def fake_model(messages):
# A pretend model: asks for the weather tool, then answers from its result.
last = messages[-1]
if last["role"] == "tool":
return {"role": "assistant", "content": "The weather in Tabriz is " + last["content"] + ".", "tool_calls": None}
return {"role": "assistant", "content": None, "tool_calls": [
{"id": "call_1", "function": {"name": "get_weather", "arguments": '{"city": "Tabriz"}'}}]}
messages = [{"role": "user", "content": "What is the weather in Tabriz today?"}]
reply = fake_model(messages)
call = reply["tool_calls"][0]
# run the tool, add two messages, ask again
print(reply["content"])
راهنما و جواب این چالش · برگشت به درس جلسهی 69: ابزار دادن به مدل · چالش بعدی: Several Tools in One Reply
بیشتر بخوان: تابع در پایتون: آموزش def، پارامتر و return با مثال