diff --git a/appbuilder/core/console/appbuilder_client/appbuilder_client.py b/appbuilder/core/console/appbuilder_client/appbuilder_client.py
index 1f1f290e..7d09d40e 100644
--- a/appbuilder/core/console/appbuilder_client/appbuilder_client.py
+++ b/appbuilder/core/console/appbuilder_client/appbuilder_client.py
@@ -390,82 +390,3 @@ def _transform(
tool_calls=ev.tool_calls,
)
out.events.append(event)
-
-
-
-
-builder = AppBuilderClient("b2a972c5-e082-46e5-b313-acbf51792422")
-conversation_id = builder.create_conversation()
-
-def get_current_weather(location: str, unit: str) -> str:
- """
- 查询指定中国城市的当前天气。
-
- 参数:
- location (str): 城市名称,例如:"北京"
- unit (str): 温度单位,可选 "celsius" 或 "fahrenheit"
-
- 返回:
- str: 天气情况描述
-
- 抛出:
- ValueError: 如果传入的城市不支持或单位不正确
- """
- return "北京今天25度"
-def get_current_water_temperature() -> str:
- """
- 获取当前水温。
-
- 返回:
- str: 当前水温描述
- """
- return "当前水温25度"
-tools = [
- {
- "type": "function",
- "function": {
- "name": "get_current_weather",
- "description": "仅支持中国城市的天气查询,参数location为中国城市名称,其他国家城市不支持天气查询",
- "parameters": {
- "type": "object",
- "properties": {
- "location": {
- "type": "string",
- "description": "城市名,举例:北京",
- },
- "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
- },
- "required": ["location", "unit"],
- },
- },
- }
- ]
-functions = [get_current_water_temperature]
-msg = builder.run(
- conversation_id=conversation_id,
- query="当前水温多少度?你知道吗",
- functions=functions,
-)
-print(msg.model_dump_json(indent=4))
-
-# 获取最后的事件和工具调用信息
-event = msg.content.events[-1]
-tool_call = event.tool_calls[-1]
-
-# 获取函数名称和参数
-name = tool_call.function.name
-args = tool_call.function.arguments
-
-# 将函数名称映射到具体的函数并执行
-function_map = {f.__name__: f for f in functions}
-raw_result = function_map[name](**args)
-
-# 传递工具的输出
-msg_2 = builder.run(
- conversation_id=conversation_id,
- tool_outputs=[{
- "tool_call_id": tool_call.id,
- "output": str(raw_result)
- }],
-)
-print(msg_2.model_dump_json(indent=4))
\ No newline at end of file
diff --git a/cookbooks/end2end_application/agent/tool_call.ipynb b/cookbooks/end2end_application/agent/tool_call.ipynb
index 0946ed83..b98bb64e 100644
--- a/cookbooks/end2end_application/agent/tool_call.ipynb
+++ b/cookbooks/end2end_application/agent/tool_call.ipynb
@@ -115,6 +115,36 @@
"- 角色定义描述(Prompt):定义Agent的角色\n",
"- 能力描述(Prompt):定义Agent可以干什么\n",
"- 工具描述(JsonSchema/Str):将工具的输入和输出,按照规范,定义为一段字符串,作为最终大模型Prompt的一部分\n",
+ "- **函数定义描述(functions)**:允许直接传入本地定义的函数对象列表,替代工具描述中的`JsonSchema`,在某些场景下可以简化调用流程,避免繁琐的JSON格式定义。\n",
+ "\n",
+ "##### functions字段的引入与兼容性说明\n",
+ "为了提供更高的灵活性,Agent现在支持`functions`字段,允许传入本地函数对象的列表。相比于`tools`字段中的`JsonSchema`格式定义,`functions`字段可以更加直观地直接传递函数对象,适合无需复杂参数定义的场景。\n",
+ "\n",
+ "**兼容性说明**:当`tools`和`functions`字段同时启用时,`tools`字段优先被调用。`functions`字段将在`tools`未定义时被调用,以保持兼容旧有代码的功能。\n",
+ "##### 使用functions字段的注意事项\n",
+ "\n",
+ "当使用`functions`字段时,函数的注释(docstring)和参数的类型注解将自动被转换为JSON格式,以供Agent调用。因此,在定义函数时,请确保以下几点:\n",
+ "\n",
+ "1. **注释(docstring)**:函数的文档字符串非常重要,它将作为大模型调用时的描述。如果缺少注释,函数的描述可能不完整或不准确,影响调用结果。\n",
+ "2. **参数类型注解**:所有函数参数都应提供明确的类型注解。中间件会根据这些类型注解将参数转换为JSON Schema中对应的类型(例如,`str`对应`string`,`int`对应`integer`等)。如果没有注解,默认会将参数类型设置为`string`。\n",
+ "3. **函数签名**:函数的签名(参数名称和类型)会被自动解析为JSON中的参数定义,确保函数参数使用合理的名称并提供类型提示。\n",
+ "\n",
+ "例如,以下是一个符合要求的函数定义:\n",
+ "\n",
+ "```python\n",
+ "def get_current_weather(location: str, unit: str) -> str:\n",
+ " \"\"\"\n",
+ " 查询指定中国城市的当前天气。\n",
+ "\n",
+ " 参数:\n",
+ " location (str): 城市名称,例如:\"北京\"\n",
+ " unit (str): 温度单位,可选 \"celsius\" 或 \"fahrenheit\"\n",
+ "\n",
+ " 返回:\n",
+ " str: 天气情况描述\n",
+ " \"\"\"\n",
+ " return \"北京今天25度\"\n",
+ "```\n",
"\n",
"#### Agent的输入信息包含以下几个部分\n",
"- 用户输入(Query/Prompt):用户输入的文本\n",
@@ -141,7 +171,18 @@
"\n",
"### 3.2、开发者如何命令Agent调用本地Tool\n",
"\n",
- "我们以AppBuilder-SDK中的AppBuilder-Client的基础代码为例,介绍开发者应该如何使用ToolCall功能\n",
+ "在AppBuilder-SDK中,开发者可以通过`ToolCall`功能注册Agent调用本地定义的工具或函数。为了提高灵活性,`ToolCall`现在支持两种方式来定义和调用工具:\n",
+ "\n",
+ "1. **使用 `tools` 字段**:通过`JsonSchema`格式定义工具的输入和输出,适用于较为复杂的工具描述和调用。\n",
+ "2. **使用 `functions` 字段**:直接传递本地定义的函数对象,适用于简单的函数调用,避免繁琐的`JsonSchema`定义。\n",
+ "\n",
+ "#### tools 和 functions 字段的优先级\n",
+ "\n",
+ "当`tools`和`functions`字段同时定义时,系统将优先调用`tools`字段中的工具描述。如果`tools`未定义,则使用`functions`中的本地函数。\n",
+ "\n",
+ "#### 示例:如何通过ToolCall命令Agent调用本地工具\n",
+ "\n",
+ "以下代码示例分别展示了如何使用tools字段和functions字段来调用本地定义的函数。tools字段适用于通过JsonSchema定义复杂工具,而functions字段则简化了调用流程,直接传递本地函数对象。\n",
"\n",
"\n",
"```python\n",
@@ -182,6 +223,7 @@
" file_ids: list = [],\n",
" stream: bool = False,\n",
" tools: list[data_class.Tool] = None,\n",
+ " functions: list[Callable] = None, # 新增 functions 字段\n",
" tool_outputs: list[data_class.ToolOutput] = None,\n",
" **kwargs\n",
" ) -> Message:\n",
@@ -192,7 +234,8 @@
" file_ids(list[str], 可选):\n",
" stream (bool, 可选): 为True时,流式返回,需要将message.content.answer拼接起来才是完整的回答;为False时,对应非流式返回\n",
" tools(list[data_class.Tools], 可选): 一个Tools组成的列表,其中每个Tools对应一个工具的配置, 默认为None\n",
- " tool_outputs(list[data_class.ToolOutput], 可选): 工具输出列表,格式为list[ToolOutput], ToolOutputd内容为本地的工具执行结果,以自然语言/json dump str描述,默认为None\n",
+ " functions(list[Callable], 可选): 传递本地定义的函数对象列表,简化函数调用,默认为None # 新增字段\n",
+ " tool_outputs(list[data_class.ToolOutput], 可选): 工具输出列表,格式为list[ToolOutput], ToolOutput内容为本地的工具执行结果,以自然语言/json dump str描述,默认为None\n",
" 返回: message (obj: `Message`): 对话结果.\n",
" \"\"\"\n",
" pass\n",
@@ -206,6 +249,7 @@
"| file_ids | list[String] | 否 | 对话可引用的文档ID | |\n",
"| stream | Bool | 否 | 为true时则流式返回,为false时则一次性返回所有内容, 推荐设为true,降低首token时延 | False |\n",
"| tools | List[Tool] | 否 | 一个列表,其中每个字典对应一个工具的配置 | |\n",
+ "| functions | List[Callable] | 否 | 传入本地函数对象列表,以直接调用函数 | |\n",
"| tools[0] | Tool | 否 | 工具配置 | |\n",
"| +type | String | 否 | 枚举:
**file_retrieval**: 知识库检索工具能够理解文档内容,支持用户针对文档内容的问答。
**code_interpreter**: 代码解释器, 代码解释器能够生成并执行代码,从而协助用户解决复杂问题,涵盖科学计算(包括普通数学计算题)、数据可视化、文件编辑处理(图片、PDF文档、视频、音频等)、文件格式转换(如WAV、MP3、text、SRT、PNG、jpg、MP4、GIF、MP3等)、数据分析&清洗&处理(文件以excel、csv格式为主)、机器学习&深度学习建模&自然语言处理等多个领域。
**function**: 支持fucntion call模式调用工具 | |\n",
"| +function | Function | 否 | Function工具描述
仅当**type为**`**function**` 时需要且必须填写 | |\n",
@@ -268,12 +312,8 @@
},
{
"cell_type": "code",
- "execution_count": null,
- "metadata": {
- "vscode": {
- "languageId": "plaintext"
- }
- },
+ "execution_count": 12,
+ "metadata": {},
"outputs": [],
"source": [
"import os\n",
@@ -299,13 +339,17 @@
},
{
"cell_type": "code",
- "execution_count": null,
- "metadata": {
- "vscode": {
- "languageId": "plaintext"
+ "execution_count": 13,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Agent第一次回答: 很抱歉,我无法直接获取您提到的本公司张三同学的生日信息。建议您通过公司内部系统或联系相关部门负责人来获取准确信息。如果您有其他问题或需要帮助,请随时告诉我。\n"
+ ]
}
- },
- "outputs": [],
+ ],
"source": [
"message_1 = app_client.run(\n",
" conversation_id=conversation_id,\n",
@@ -345,20 +389,16 @@
"综上所述,要确定张三的生日,最直接且尊重隐私的方法是直接询问张三本人,或者通过公司正式渠道(如人力资源部门)在遵守隐私政策的前提下进行查询。\n",
"```\n",
"\n",
- "\n",
+ "#### 1. 使用 `tools` 字段进行 function call 的示例:\n",
"##### 赋予应用一个本地查询组件能力\n",
"\n",
- "- 这里我们使用info_dict模拟一个数据库查询的返回结果"
+ "- 这里我们使用`info_dict`模拟一个数据库查询的返回结果。"
]
},
{
"cell_type": "code",
- "execution_count": null,
- "metadata": {
- "vscode": {
- "languageId": "plaintext"
- }
- },
+ "execution_count": 9,
+ "metadata": {},
"outputs": [],
"source": [
"def get_person_infomation(name: str):\n",
@@ -392,7 +432,10 @@
" },\n",
"},\n",
" }\n",
- "]"
+ "]\n",
+ "#定义函数列表\n",
+ "functions = [get_person_infomation]\n",
+ "function_map = {f.__name__: f for f in functions}"
]
},
{
@@ -404,13 +447,59 @@
},
{
"cell_type": "code",
- "execution_count": null,
- "metadata": {
- "vscode": {
- "languageId": "plaintext"
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Agent的中间思考过程:\n",
+ "{\n",
+ " \"code\": 0,\n",
+ " \"message\": \"\",\n",
+ " \"status\": \"interrupt\",\n",
+ " \"event_type\": \"Interrupt\",\n",
+ " \"content_type\": \"contexts\",\n",
+ " \"detail\": {\n",
+ " \"text\": {\n",
+ " \"function_call\": {\n",
+ " \"thought\": \"\",\n",
+ " \"name\": \"get_person_infomation\",\n",
+ " \"arguments\": {\n",
+ " \"name\": \"张三\"\n",
+ " },\n",
+ " \"usage\": {\n",
+ " \"prompt_tokens\": 641,\n",
+ " \"completion_tokens\": 39,\n",
+ " \"total_tokens\": 680,\n",
+ " \"name\": \"ERNIE-4.0-Turbo-8K\",\n",
+ " \"type\": \"plan\"\n",
+ " },\n",
+ " \"tool_call_id\": \"07c403f3-502d-41ec-9815-a4498452feb1\"\n",
+ " },\n",
+ " \"used_tool\": []\n",
+ " }\n",
+ " },\n",
+ " \"usage\": null,\n",
+ " \"tool_calls\": [\n",
+ " {\n",
+ " \"id\": \"07c403f3-502d-41ec-9815-a4498452feb1\",\n",
+ " \"type\": \"function\",\n",
+ " \"function\": {\n",
+ " \"name\": \"get_person_infomation\",\n",
+ " \"arguments\": {\n",
+ " \"name\": \"张三\"\n",
+ " }\n",
+ " }\n",
+ " }\n",
+ " ]\n",
+ "}\n",
+ "Agent思考结束,等待我们上传本地结果\n",
+ "\n"
+ ]
}
- },
- "outputs": [],
+ ],
"source": [
"message_2 = app_client.run(\n",
" conversation_id=conversation_id,\n",
@@ -465,18 +554,24 @@
},
{
"cell_type": "code",
- "execution_count": null,
- "metadata": {
- "vscode": {
- "languageId": "plaintext"
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "local_func_result: 您要查找的张三的生日是:1980年1月1日\n",
+ "\n"
+ ]
}
- },
- "outputs": [],
+ ],
"source": [
"tool_call = message_2.content.events[-1].tool_calls[-1]\n",
"tool_call_id = tool_call.id\n",
+ "tool_call_name = tool_call.function.name\n",
"tool_call_argument = tool_call.function.arguments\n",
- "local_func_result = get_person_infomation(**tool_call_argument)\n",
+ "local_func_result = function_map[name](**args)\n",
"print(\"local_func_result: {}\\n\".format(local_func_result))"
]
},
@@ -495,11 +590,7 @@
{
"cell_type": "code",
"execution_count": null,
- "metadata": {
- "vscode": {
- "languageId": "plaintext"
- }
- },
+ "metadata": {},
"outputs": [],
"source": [
"message_3 = app_client.run(\n",
@@ -582,6 +673,115 @@
"```"
]
},
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#### 2. 使用 `functions` 字段进行 function call 的示例:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "#定义函数\n",
+ "def get_person_infomation(name: str):\n",
+ " info_dict = {\n",
+ " \"张三\": \"1980年1月1日\",\n",
+ " \"李四\": \"1975年12月31日\",\n",
+ " \"刘伟\": \"1990年12月30日\"\n",
+ " }\n",
+ "\n",
+ " if name in info_dict:\n",
+ " return f\"您要查找的{name}的生日是:{info_dict[name]}\"\n",
+ " else:\n",
+ " return f\"您要查找的{name}的信息我们暂未收录,请联系管理员添加。\"\n",
+ "#定义函数列表\n",
+ "functions = [get_person_infomation]\n",
+ "function_map = {f.__name__: f for f in functions}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Agent的中间思考过程:\n",
+ "{\n",
+ " \"code\": 0,\n",
+ " \"message\": \"\",\n",
+ " \"status\": \"success\",\n",
+ " \"event_type\": \"ChatAgent\",\n",
+ " \"content_type\": \"status\",\n",
+ " \"detail\": {},\n",
+ " \"usage\": null,\n",
+ " \"tool_calls\": null\n",
+ "}\n",
+ "Agent思考结束,等待我们上传本地结果\n",
+ "\n"
+ ]
+ }
+ ],
+ "source": [
+ "message_2 = app_client.run(\n",
+ " conversation_id=conversation_id,\n",
+ " query=\"请问本公司的张三同学的生日是哪天?\",\n",
+ " functions=functions\n",
+ " #tools=tools\n",
+ ")\n",
+ "print(\"Agent的中间思考过程:\")\n",
+ "print(message_2.content.events[-1].model_dump_json(indent=4))\n",
+ "print(\"Agent思考结束,等待我们上传本地结果\\n\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {},
+ "outputs": [
+ {
+ "ename": "TypeError",
+ "evalue": "'NoneType' object is not subscriptable",
+ "output_type": "error",
+ "traceback": [
+ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
+ "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
+ "\u001b[1;32m/Users/daijun04/app-builder/cookbooks/end2end_application/agent/tool_call.ipynb Cell 22\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> 1\u001b[0m tool_call \u001b[39m=\u001b[39m message_2\u001b[39m.\u001b[39;49mcontent\u001b[39m.\u001b[39;49mevents[\u001b[39m-\u001b[39;49m\u001b[39m1\u001b[39;49m]\u001b[39m.\u001b[39;49mtool_calls[\u001b[39m-\u001b[39;49m\u001b[39m1\u001b[39;49m]\n\u001b[1;32m 2\u001b[0m tool_call_id \u001b[39m=\u001b[39m tool_call\u001b[39m.\u001b[39mid\n\u001b[1;32m 3\u001b[0m tool_call_name \u001b[39m=\u001b[39m tool_call\u001b[39m.\u001b[39mfunction\u001b[39m.\u001b[39mname\n",
+ "\u001b[0;31mTypeError\u001b[0m: 'NoneType' object is not subscriptable"
+ ]
+ }
+ ],
+ "source": [
+ "tool_call = message_2.content.events[-1].tool_calls[-1]\n",
+ "tool_call_id = tool_call.id\n",
+ "tool_call_name = tool_call.function.name\n",
+ "tool_call_argument = tool_call.function.arguments\n",
+ "local_func_result = function_map[name](**args)\n",
+ "print(\"local_func_result: {}\\n\".format(local_func_result))"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "message_3 = app_client.run(\n",
+ " conversation_id=conversation_id,\n",
+ " tool_outputs=[{\n",
+ " \"tool_call_id\": tool_call_id,\n",
+ " \"output\": local_func_result\n",
+ " }]\n",
+ ")\n",
+ "print(\"Agent 拥有了本地函数调用能力后,回答是: {}\".format(message_3.content.answer))"
+ ]
+ },
{
"cell_type": "markdown",
"metadata": {},
@@ -595,7 +795,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@@ -621,7 +821,7 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
@@ -672,9 +872,36 @@
},
{
"cell_type": "code",
- "execution_count": null,
+ "execution_count": 3,
"metadata": {},
- "outputs": [],
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\u001b[1;32m -> Agent 中间思考: \u001b[0m\n",
+ "\u001b[1;34m -> 本地ToolCall结果: \n",
+ " \u001b[34mapp_builder_resources\u001b[m\u001b[m\n",
+ "appbuilder_client.ipynb\n",
+ "tmp.log\n",
+ "tool_call.ipynb\n",
+ "tool_choice.ipynb\n",
+ " \u001b[0m\n",
+ "\n",
+ "\n",
+ "\u001b[1;31m -> Agent 非流式回答: \n",
+ " 当前文件夹下的文件有:\n",
+ "\n",
+ "- app_builder_resources\n",
+ "- appbuilder_client.ipynb\n",
+ "- tmp.log\n",
+ "- tool_call.ipynb\n",
+ "- tool_choice.ipynb\n",
+ "\n",
+ "如果没有 `test.txt` 文件,我已经为您新建了一个 `test.txt` 文件,内容为:Hello World! \u001b[0m\n"
+ ]
+ }
+ ],
"source": [
"tools = [\n",
" {\n",
@@ -773,8 +1000,22 @@
}
],
"metadata": {
+ "kernelspec": {
+ "display_name": "ab",
+ "language": "python",
+ "name": "python3"
+ },
"language_info": {
- "name": "python"
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.9.18"
}
},
"nbformat": 4,
diff --git a/docs/basic_module/appbuilder_client.md b/docs/basic_module/appbuilder_client.md
index c1a7e6aa..b9f1cc06 100644
--- a/docs/basic_module/appbuilder_client.md
+++ b/docs/basic_module/appbuilder_client.md
@@ -244,6 +244,23 @@ app_id = "..." # 已发布AppBuilder应用的ID
client = appbuilder.AppBuilderClient(app_id)
# 创建会话
conversation_id = client.create_conversation()
+#定义本地函数
+def get_current_weather(location: str, unit: str) -> str:
+ """
+ 查询指定中国城市的当前天气。
+
+ 参数:
+ location (str): 城市名称,例如:"北京"
+ unit (str): 温度单位,可选 "celsius" 或 "fahrenheit"
+
+ 返回:
+ str: 天气情况描述
+
+ 抛出:
+ ValueError: 如果传入的城市不支持或单位不正确
+ """
+ return "北京今天25度"
+#--------------------------使用tools字段进行ToolCall的调用---------------------------
tools = [
{
"type": "function",
@@ -264,19 +281,108 @@ tools = [
},
}
]
+#创建函数对象列表
+functions = [get_current_weather]
+#调用大模型
msg = client.run(
conversation_id=conversation_id, query="今天北京天气怎么样?", tools=tools
)
print(msg.model_dump_json(indent=4))
+# 获取最后的事件和工具调用信息
event = msg.content.events[-1]
+tool_call = event.tool_calls[-1]
+
+# 获取函数名称和参数
+name = tool_call.function.name
+args = tool_call.function.arguments
+# 将函数名称映射到具体的函数并执行
+function_map = {f.__name__: f for f in functions}
+raw_result = function_map[name](**args)
+
+# 传递工具的输出
msg_2 = client.run(
conversation_id=conversation_id,
- tool_outputs=[{"tool_call_id": event.tool_calls[-1].id, "output": "北京今天35度"}],
+ tool_outputs=[{
+ "tool_call_id": tool_call.id,
+ "output": str(raw_result)
+ }],
)
print(msg_2.model_dump_json(indent=4))
+
+#---------------------------使用functions字段进行ToolCall的调用------------------------
+#为了区分再定义一个新的函数,注意:要使用此方法要为函数写好注释。
+def get_current_weather2(location: str, unit: str) -> str:
+ """
+ 查询指定中国城市的当前天气。
+
+ 参数:
+ location (str): 城市名称,例如:"北京"
+ unit (str): 温度单位,可选 "celsius" 或 "fahrenheit"
+
+ 返回:
+ str: 天气情况描述
+
+ 抛出:
+ ValueError: 如果传入的城市不支持或单位不正确
+ """
+ return "北京今天35度"
+#定义函数列表
+functions = [get_current_weather2]
+function_map = {f.__name__: f for f in functions}
+#调用大模型
+msg = client.run(
+ conversation_id=conversation_id,
+ query="今天北京的天气怎么样?",
+ functions=functions,
+ #tools = tools
+ )
+print(msg.model_dump_json(indent=4))
+# 获取最后的事件和工具调用信息
+event = msg.content.events[-1]
+tool_call = event.tool_calls[-1]
+
+# 获取函数名称和参数
+name = tool_call.function.name
+args = tool_call.function.arguments
+
+# 将函数名称映射到具体的函数并执行
+raw_result = function_map[name](**args)
+
+# 传递工具的输出
+msg_2 = client.run(
+ conversation_id=conversation_id,
+ tool_outputs=[{
+ "tool_call_id": tool_call.id,
+ "output": str(raw_result)
+ }],
+)
+print(msg_2.model_dump_json(indent=4))
+
+#--------------同时使用functions字段和tools字段,tools字段的优先级更高---------------
+
+#创建函数对象列表
+functions = [get_current_weather,get_current_weather2]
+
+msg = client.run(
+ conversation_id=conversation_id,
+ query="今天北京的天气怎么样?",
+ functions=[get_current_weather2],
+ tools = tools
+ )
+print(msg.model_dump_json(indent=4))
+# 获取最后的事件和工具调用信息
+event = msg.content.events[-1]
+tool_call = event.tool_calls[-1]
+
+# 获取函数名称和参数
+name = tool_call.function.name
+args = tool_call.function.arguments
+
+#打印大模型返回函数名称
+print(name)
```
#### Run方法带ToolChoice使用示例:
@@ -349,6 +455,7 @@ answer = app_builder_client.run(
| query | String | 是 | query内容 | "汽车性能参数怎么样" |
| conversationId | String | 是 | 对话id,可以通过createConversation()获取 | |
| stream | boolean | 是 | 为true时则流式返回,为false时则一次性返回所有内容, 推荐设为true,降低首token时延 | |
+| functions | List[Callable] | 否 | 传入本地函数对象列表,以直接调用函数 | |
| tools | List[Tool] | 否 | 一个列表,其中每个字典对应一个工具的配置 | |
| tools[0] | Tool | 否 | 工具配置 | |
| +type | String | 否 | 枚举:
**file_retrieval**: 知识库检索工具能够理解文档内容,支持用户针对文档内容的问答。
**code_interpreter**: 代码解释器, 代码解释器能够生成并执行代码,从而协助用户解决复杂问题,涵盖科学计算(包括普通数学计算题)、数据可视化、文件编辑处理(图片、PDF文档、视频、音频等)、文件格式转换(如WAV、MP3、text、SRT、PNG、jpg、MP4、GIF、MP3等)、数据分析&清洗&处理(文件以excel、csv格式为主)、机器学习&深度学习建模&自然语言处理等多个领域。
**function**: 支持fucntion call模式调用工具 | |