get_weather,该函数接受一个位置参数,并返回该位置的天气状况信息。ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">处理提示时,该模型可以选择将某些数据处理任务委托给您确定的函数。模型不会直接调用函数。 相反,模型会提供结构化数据输出,其中包含要调用的函数和要使用的参数值。 例如,对于提示北京的天气怎么样,模型可以将处理委托给get_weather函数,并提供位置参数值北京。ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">您可以使用模型的结构化输出来调用外部 API。例如,您可以连接到天气服务 API,提供位置,并接收有关温度、云量和风况的信息。ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 15px;margin: 1em 8px;letter-spacing: 0.1em;color: rgb(33, 37, 41);padding: 8px 12px;background: rgba(237, 242, 255, 0.8);border-radius: 8px;">然后,您可以将 API 输出返回给模型,使模型能够完成对提示的回答。 对于天气示例,模型可能会提供以下回答:ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;padding: 8px 12px;border-radius: 0px;color: rgba(0, 0, 0, 0.5);background: rgb(244, 238, 255);margin: 1em 8px;border-top-width: 1px;border-right-width: 1px;border-bottom-width: 1px;border-left: 3px solid rgb(201, 132, 255) !important;">ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 1em;letter-spacing: 0.1em;color: rgb(80, 80, 80);display: block;margin: 0px;">北京现在 18 度,多云,空气质量良好。ingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Arial, sans-serif;font-size: 20px;font-weight: bold;display: table;margin: 2em auto 1.5em;padding: 6px 0px 6px 16.75px;background: linear-gradient(135deg, rgb(113, 23, 234), rgba(113, 23, 234, 0.667), rgba(234, 96, 96, 0.533), rgba(217, 57, 205, 0.267), rgba(217, 57, 205, 0));color: rgb(255, 255, 255);border-radius: 8px;width: 318.25px;">函数调用的应用场景您可以使用函数调用执行以下任务:
使用函数调用功能,需要向模型提示添加描述编程接口的结构化查询数据(称为函数声明)。 函数声明提供 API 函数的名称,说明其用途、支持的所有参数以及这些参数的说明。 将查询中的函数声明列表传递给模型后,模型会分析函数声明和查询的其余部分,以确定如何使用声明的 API 来响应请求。
然后,模型返回一个 OpenAPI 兼容架构中的对象, 其中指定了如何调用一个或多个已声明的函数, 以便回答用户的问题。然后,您可以采用建议的函数调用参数, 调用实际 API,获取响应,并将该响应提供给用户或执行进一步操作。
创建一个能够与模型交互并使用函数调用的程序,大致过程如下:
我们一般创建一个应用来管理所有这些任务。应用可以是文本聊天机器人、语音客服、自动化工作流或任何其他程序。
我们遵循OpenAPI 架构[1]来设计一个标准的函数声明定义,来适应大多数的大模型。
在提示中实现函数调用时,先创建一个 tools 对象, 其中包含一个或多个函数声明。
使用OpenAPI 架构[2]格式定义函数。单个函数声明可以包含以下参数:
name(字符串):API 调用中函数的唯一标识符。description(字符串):全面说明函数的用途和功能。parameters(对象):定义函数所需的输入数据。type(字符串):参数的数据类型,例如string、integer、boolean。description(字符串):清晰说明参数的用途和预期格式。type(字符串):指定整体数据类型,例如object。properties(对象):列出各个参数,每个参数均包含:required(数组):列出参数名称的字符串数组 它们是函数运行所必需的。name:使用清晰、描述性强的名称,不含空格、英文句点 (.) 或短划线 (-) 字符。请改用下划线 (_) 字符 或驼峰命名法。description:提供详细、清晰且具体的功能 说明,必要时提供示例。 避免过于宽泛或不明确 信息。properties > type:使用强类型参数来减少模型幻觉。例如,如果参数值来自 设置时,请使用 enum 字段,而不是在说明中列出值 (例如,"type": "enum", "values": ["now_playing", "upcoming"])。如果 参数值始终为整数,请将类型设置为integer,而不是number。properties > description:提供具体示例和限制条件。很多模型本身支持了函数调用,比如:
我们可以基于这些模型和平台编写特定的程序来实现函数调用功能。
虽然很多平台和模型都支持原生的函数调用,为什么还要讲提示词呢:
下面讲通过提示词来实现通用的函数调用(使用 Python 代码进行格式化)。
respones_shema = {
"title":"Function Call",
"type":"object",
"properties": {
"functionCall": {
"type":"object",
"properties": {
"name": {
"type":"string",
"description":"The name of the function to call."
},
"args": {
"description":"Arguments for the function, can be any valid JSON object.",
"oneOf": [
{"type":"null"},
{"type":"boolean"},
{"type":"integer"},
{"type":"number"},
{"type":"string"},
{"type":"array"},
{"type":"object"}
]
}
},
"required": ["name","args"],
"additionalProperties": false
}
},
"required": ["functionCall"],
"additionalProperties": false
}
system_prompts =f"""
You have access to the following tools:
{json.dumps(tools)}
You can select one of the above tools or
just response user's content and respond
with only a JSON object matching the following schema:
{json.dumps(response_schema)}
"""具体的 LLM API 调用大家可以根据不同的
| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |