惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
阮一峰的网络日志
阮一峰的网络日志
V
V2EX
Y
Y Combinator Blog
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
Help Net Security
Help Net Security
Security Latest
Security Latest
Recorded Future
Recorded Future
S
Secure Thoughts
P
Privacy International News Feed
L
Lohrmann on Cybersecurity
Vercel News
Vercel News
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Google DeepMind News
Google DeepMind News
L
LINUX DO - 热门话题
T
The Blog of Author Tim Ferriss
T
Threatpost
宝玉的分享
宝玉的分享
PCI Perspectives
PCI Perspectives
V
Vulnerabilities – Threatpost
WordPress大学
WordPress大学
C
CERT Recently Published Vulnerability Notes
GbyAI
GbyAI
S
Schneier on Security
S
Security @ Cisco Blogs
S
Securelist
SecWiki News
SecWiki News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Jina AI
Jina AI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
Google Developers Blog
aimingoo的专栏
aimingoo的专栏
博客园 - 聂微东
H
Heimdal Security Blog
D
DataBreaches.Net
M
MIT News - Artificial intelligence
Microsoft Security Blog
Microsoft Security Blog
A
Arctic Wolf
C
Cybersecurity and Infrastructure Security Agency CISA
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Schneier on Security
Schneier on Security
C
Check Point Blog
D
Docker

博客园 - 「圣杰」

.NET+AI | Harness | MAF 1.4 发布,Harness Engineering 如约而至,智能体工程化更进一步 Microsoft Agent Framework 1.0 正式发布:Agent Skills 补齐后,.NET AI Agent 开发真正进入工程化时代 .NET+AI | Workflow | 核心概念速通(1) .NET+AI | 基于 Microsoft Agent Framework 一步步集成 Agent Skills,让你的 AI Agent 更智能 未来已来 | 写给 .NET 开发者的 2025 年度总结 从 MCP 到 Agent Skills,AI Ready 的 .NET 10 正当时 .NET+AI | Agent | 人机协作(9) .NET+AI | MEAI | 自定义中间件(8) .NET+AI | MEAI | 上下文压缩(7) .NET+AI | MEAI | 会话缓存(6) .NET+AI | MEAI | ChatOptions 详解(5) AI 时代,.NET 开发者是向左还是向右? .NET+AI | MEAI | Function Caling 实操(4) .NET+AI | MEAI | 提示工程基础(2) .NET+AI | MEAI | .NET 平台的 AI 底座 (1) .NET 搞 AI 不行? ​微软 AI Agent三剑客:AutoGen、Semantic Kernel与MEAI的协同演进 .NET+AI | eShopSupport 知多少 .NET + AI | Semantic Kernel vs Microsoft.Extensions.AI 极客时间上新 .NET + AI 体系课
.NET+AI | MEAI | Function Calling 基础(3)
「圣杰」 · 2025-11-21 · via 博客园 - 「圣杰」

.NET+AI | MEAI | Function Calling 基础

一句话概括

Function Calling 是让大模型能够识别用户意图并返回结构化函数调用指令的能力,而不是模型主动调用函数。

为什么需要Function Calling?

大模型虽然强大,但它只能基于训练数据生成文本回复,无法:

  • 📊 获取实时数据(天气、股票等)
  • 🔧 执行具体操作(发邮件、查数据库)
  • 🌐 访问外部系统(API调用)

Function Calling 打破了这个限制,让 LLM 与外部工具建立连接,极大拓展了应用边界

💡 一句话总结
Function Calling = 大模型的"插件系统",让 AI 从"能说会道"变成"能说会做"。

核心工作原理

完整生命周期(5步)

用户提问 → 模型识别意图 → 返回函数调用参数 → 应用执行函数 → 模型生成最终回复

实战案例:查询天气

场景: 用户问"深圳今天天气怎样?"

步骤1️⃣: 发送请求(含函数定义)

{
    "messages": [
        {
            "role": "user",
            "content": "深圳今天的天气怎样"
        }
    ],
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "get_current_weather",
                "description": "获取指定城市的天气",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "city": {
                            "type": "string",
                            "description": "城市名"
                        }
                    },
                    "required": ["city"]
                }
            }
        }
    ]
}

关键点:

  • tools: 告诉模型可用的函数列表
  • description: 帮助模型理解函数用途(越清晰越好)
  • parameters: 定义函数所需参数的 JSON Schema

步骤2️⃣: 模型返回函数调用信息

{
    "choices": [{
        "message": {
            "role": "assistant",
            "tool_calls": [{
                "function": {
                    "name": "get_current_weather",
                    "arguments": "{\"city\": \"深圳\"}"
                }
            }]
        },
        "finish_reason": "tool_calls"
    }]
}

重点: 模型识别出要调用 get_current_weather("深圳")

步骤3️⃣: 应用执行函数

// 解析模型返回,调用本地函数
string result = get_current_weather("深圳");
// 返回: "36℃,小雨"

步骤4️⃣: 回传结果给模型

{
    "messages": [
        // ...前面的对话历史
        {
            "role": "function",
            "name": "get_current_weather",
            "content": "36℃,小雨"
        }
    ]
}

步骤5️⃣: 模型生成友好回复

{
    "choices": [{
        "message": {
            "role": "assistant",
            "content": "深圳今天小雨,气温36摄氏度。"
        }
    }]
}

关键要点总结

误区 真相
❌ 模型会自动调用函数 ✅ 模型只返回调用指令,需开发者执行
❌ Function Calling 是新技术 ✅ 本质是语义识别+结构化输出
❌ 只有 OpenAI 支持 ✅ 国内外主流模型均已支持

实际应用场景

数据查询: 天气、股票、新闻
系统操作: 发邮件、创建日程
业务集成: CRM、ERP系统调用
多步骤任务: 自动化工作流

开发注意事项

  1. 函数描述要精准: 模型依赖 description 判断是否调用
  2. 参数定义要完整: 使用标准 JSON Schema 格式
  3. 错误处理要健壮: 函数执行失败时的降级策略
  4. 上下文要保留: 多轮对话需维护完整消息历史

下一步学习

Function Calling 的定义和调用需要手动处理较多细节, 下一节介绍如何基于MEAI 完成函数调用,具体实现敬请期待。


📚 参考资料