> For the complete documentation index, see [llms.txt](https://doc.afarensis.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.afarensis.com/gong-ju/function-calling.md).

# Function calling

Afarensis 助手API支持函数调用，类似于聊天完成API，这一功能允许您向助手描述函数，并智能地返回需要调用的函数及其参数。当助手在运行过程中调用函数时，执行将暂停，直到您提供函数调用的结果，从而继续运行的执行。

定义函数 在创建助手时，首先定义您想要助手调用的函数：

assistant = client.beta.assistants.create( instructions="You are a weather bot. Use the provided functions to answer questions.", model="gpt-4-turbo-preview", tools=\[{ "type": "function", "function": { "name": "getCurrentWeather", "description": "获取指定地点的天气", "parameters": { "type": "object", "properties": { "location": {"type": "string", "description": "城市和州，例如 San Francisco, CA"}, "unit": {"type": "string", "enum": \["c", "f"]} }, "required": \["location"] } } }, { "type": "function", "function": { "name": "getNickname", "description": "获取城市的昵称", "parameters": { "type": "object", "properties": { "location": {"type": "string", "description": "城市和州，例如 San Francisco, CA"}, }, "required": \["location"] } } }] ) 触发和读取函数调用 当用户消息触发函数调用时，运行将进入“pending”状态，并在处理后进入“requires\_action”状态。您可以通过检索运行状态来验证此状态，并查看需要调用的函数及其参数。

提交函数输出 完成函数调用后，您需要提交函数的输出，以便继续运行的执行：

run = client.beta.threads.runs.submit\_tool\_outputs( thread\_id=thread.id, run\_id=run.id, tool\_outputs=\[ { "tool\_call\_id": call\_ids\[0], "output": "22C", }, { "tool\_call\_id": call\_ids\[1], "output": "LA", }, ] ) 在提交了函数输出后，运行将进入“queued”状态，并继续执行直至完成。
