Function Calling 是 Azure OpenAI 和 OpenAI 服务中一项强大的功能,它允许开发人员在大型语言模型 (LLM) 与外部系统之间创建更结构化的交互。本文档解释了 Function Calling 的概念、其好处以及如何在生成式 AI 应用程序中实现它。
有关不使用 Function Calling 的文本生成应用程序的信息,请参阅 文本生成应用程序。有关构建聊天应用程序的信息,请参阅 聊天应用程序。
Function Calling 使您的应用程序能够:
该系统允许 LLM 识别何时需要外部数据或工具来满足用户请求,识别要调用的适当函数,并正确格式化必要的参数。
来源:11-integrating-with-function-calling/README.md13-24 11-integrating-with-function-calling/translations/ja-jp/README.md11-24
在引入 Function Calling 之前,开发人员在尝试以编程方式使用 LLM 输出时遇到了重大挑战
当要求 LLM 从文本中提取信息时,即使提示相同,响应格式也可能在每次调用之间有所不同。这种不一致性给下游处理带来了困难。
考虑以下示例,两个几乎相同的学生描述产生了不同的输出格式
Response 1:
{
"name": "Emily Johnson",
"major": "Computer Science",
"school": "Duke University",
"grades": "3.7",
"club": "Chess Club"
}
Response 2:
{
"name": "Michael Lee",
"major": "Computer Science",
"school": "Stanford University",
"grades": "3.8 GPA",
"club": "Robotics Club"
}
请注意 grades 字段的格式不同:“3.7” vs “3.8 GPA”。这些不一致性在处理大规模数据时会导致问题。
LLM 在某个截止日期之前的数据进行了训练,并且无法访问:
Function Calling 通过允许 LLM 请求外部数据的特定数据并将其用于响应来解决这些限制。
来源:11-integrating-with-function-calling/README.md37-178 11-integrating-with-function-calling/translations/ja-jp/README.md38-177
重要提示:LLM 实际上不会执行函数。它会识别应该调用哪个函数并提供结构化的参数。您的应用程序负责实际的函数执行。
来源:11-integrating-with-function-calling/README.md179-204 11-integrating-with-function-calling/translations/ja-jp/README.md178-204
实现 Function Calling 涉及以下关键步骤:
首先创建一个用户消息,其中包含您的应用程序要处理的查询
该 role 可以是:
system(用于设置规则/上下文)assistant(用于模型响应)user(用于最终用户查询)接下来,通过提供详细的规范来定义 LLM 可以“调用”的函数
每个函数定义包括:
name:函数标识符description:对函数功能的清晰说明parameters:参数的模式,包含:type:数据类型(object、string、number 等)properties:每个参数的详细定义required:必需参数的数组将消息和函数定义发送到 Azure OpenAI 服务
设置 function_call="auto" 允许模型根据用户查询决定是否调用函数。
从响应中提取函数调用信息
示例响应
来源:11-integrating-with-function-calling/README.md205-297 11-integrating-with-function-calling/translations/ja-jp/README.md205-297
为了将 Function Calling 完全集成到您的应用程序中,请遵循以下步骤:
创建当 LLM 建议调用时将执行的 Python 函数
将 LLM 响应中的函数名称映射到您的实际 Python 函数
检查 LLM 是否建议了函数调用,执行它,并将结果添加到对话中
将更新后的消息(包括函数结果)发送回 LLM 以生成自然语言响应
示例结果
{
"role": "assistant",
"content": "I found several good courses for beginner students learning Azure:\n\n
1. [Describe concepts of cryptography](https://learn.microsoft.com/training/modules/describe-concepts-of-cryptography/?WT.mc_id=api_CatalogApi)\n
2. [Introduction to audio classification with TensorFlow](https://learn.microsoft.com/training/modules/intro-audio-classification-tensorflow/?WT.mc_id=api_CatalogApi)\n
3. [Design a data model with Azure Data Studio for Azure SQL Database](https://learn.microsoft.com/training/modules/design-a-data-model-with-ads/?WT.mc_id=api_CatalogApi)\n
4. [Microsoft Cloud Adoption Framework for Azure](https://learn.microsoft.com/training/modules/cloud-adoption-framework-getting-started/?WT.mc_id=api_CatalogApi)\n
5. [Set up your Rust development environment](https://learn.microsoft.com/training/modules/rust-set-up-environment/?WT.mc_id=api_CatalogApi)\n\n
You can click on the links to access the courses."
}
来源:11-integrating-with-function-calling/README.md309-470 11-integrating-with-function-calling/translations/ja-jp/README.md309-470
Function Calling 支持各种强大的应用程序场景
聊天机器人可以使用 Function Calling 根据用户请求执行特定操作。例如,学生可能会要求聊天机器人“给我的教授发邮件,说我在这门课上需要更多帮助”,这可能会调用一个 send_email(to: string, body: string) 函数。
用户可以使用自然语言搜索信息,这些信息会被转换为格式化的查询或 API 请求。例如,老师询问“哪些学生完成了上次作业?”,可能会触发一个 get_completed(student_name: string, assignment: int, current_status: string) 函数。
用户可以检索文本或 CSV 格式的数据,并使用 LLM 提取重要信息。例如,学生可以使用 get_important_facts(agreement_name: string, date_signed: string, parties_involved: list) 函数将关于和平协议的维基百科文章转换为 AI 抽认卡。
来源:11-integrating-with-function-calling/README.md189-196 11-integrating-with-function-calling/translations/ja-jp/README.md188-195
在应用程序中实现 Function Calling 时,请考虑以下最佳实践:
Function Calling 弥合了自然语言交互与编程功能之间的差距,使您的生成式 AI 应用程序能够:
通过遵循本文档中概述的实现步骤,您可以创建更强大、更灵活、更有用的生成式 AI 应用程序。
来源:11-integrating-with-function-calling/README.md472-480 11-integrating-with-function-calling/translations/ja-jp/README.md472-480
刷新此 Wiki
最后索引时间2025 年 4 月 18 日(b88b09)