工具#
学习如何将 LLM 与外部工具连接起来。
介绍#
通过 tools
功能,您可以让您的模型使用外部工具。
就像 OpenAI 的 Function calling API 一样,你可以定义带有参数的函数,并让模型动态选择要调用哪个函数以及传递给它什么参数。
这是调用函数的一般过程:
您提交一个查询,详细说明函数、它们的参数和描述。
LLM 决定是否启动功能。如果选择不启动,它会用日常语言回复,要么基于其内在理解提供解决方案,要么询问有关查询和工具使用的进一步细节。在决定使用工具时,它会推荐适合的 API 和 JSON 格式的使用说明。
接下来,你在应用程序中实现 API 调用,并将返回的响应发送回 LLM 进行结果分析,并继续执行下一步操作。
目前没有为 tools
功能实现专用的 API 端点。它必须与 Chat API 结合使用。
支持的模型列表#
Xinference 支持以下模型使用 tools
功能:
快速入门#
Chat API 中的可选参数 tools
可以用于提供函数规范。其目的是使模型能够生成符合所提供规范的函数参数。
使用 OpenAI 客户端的示例#
import openai
client = openai.Client(
api_key="cannot be empty",
base_url="http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1"
)
client.chat.completions.create(
model="<MODEL_UID>",
messages=[{
"role": "user",
"content": "Call me an Uber ride type 'Plus' in Berkeley at zipcode 94704 in 10 minutes"
}],
tools=[
{
"type": "function",
"function": {
"name": "uber_ride",
"description": "Find suitable ride for customers given the location, "
"type of ride, and the amount of time the customer is "
"willing to wait as parameters",
"parameters": {
"type": "object",
"properties": {
"loc": {
"type": "int",
"description": "Location of the starting place of the Uber ride",
},
"type": {
"type": "string",
"enum": ["plus", "comfort", "black"],
"description": "Types of Uber ride user is ordering",
},
"time": {
"type": "int",
"description": "The amount of time in minutes the customer is willing to wait",
},
},
},
},
}
],
)
print(response.choices[0].message)
输出结果是:
{
"role": "assistant",
"content": null,
"tool_calls": [
"id": "call_ad2f383f-31c7-47d9-87b7-3abe928e629c",
"type": "function",
"function": {
"name": "uber_ride",
"arguments": "{\"loc\": 94704, \"type\": \"plus\", \"time\": 10}"
}
],
}
备注
如果 LLM 使用了工具调用,完成原因将是 tool_calls
。否则,它将是默认的完成原因。
备注
API 本身不会执行任何函数调用。开发者需要使用模型输出来执行函数调用。
你可以在教程笔记本中找到更多关于 tools
能力的示例。