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

推荐订阅源

SecWiki News
SecWiki News
V
V2EX
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
博客园 - 叶小钗
C
Check Point Blog
Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
D
DataBreaches.Net
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
IT之家
IT之家
The Hacker News
The Hacker News
The Cloudflare Blog
T
Threatpost
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
P
Proofpoint News Feed
I
InfoQ
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Troy Hunt's Blog
罗磊的独立博客
P
Privacy International News Feed
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security @ Cisco Blogs
Latest news
Latest news
D
Docker
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
Help Net Security
Help Net Security
云风的 BLOG
云风的 BLOG
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
腾讯CDC
L
LINUX DO - 最新话题
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
W
WeLiveSecurity
Forbes - Security
Forbes - Security
T
Threat Research - Cisco Blogs
美团技术团队
L
LINUX DO - 热门话题
Know Your Adversary
Know Your Adversary
O
OpenAI News

博客园 - 「圣杰」

.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 Calling 基础(3) .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 Caling 实操(4)
「圣杰」 · 2025-11-22 · via 博客园 - 「圣杰」

.NET+AI | MEAI | Function Caling 实操

TL;DR

  • ✅ 注册你的方法为工具(Tool)
  • ✅ 启用中间件 UseFunctionInvocation()
  • ✅ 设置 ChatOptions.ToolMode = Auto
  • ✅ 发起对话,MEAI 自动完成:请求 → 调用 → 回填 → 作答

🏢 场景与价值

  • 💬 智能助理需要可控访问后端:天气/库存/知识库/工单
  • 📦 MEAI 用统一“工具”抽象,屏蔽模型/厂商差异
  • 🛡️ 可控、可观测、可扩展(日志/缓存/限流易接入)

🌏 核心概念速记

  • 📦 ToolCollection/AITool:把你的方法包装成“可被调用的工具”
  • 🎚️ ChatOptions.ToolMode:控制模型是否/如何调用工具(None/Auto/Require)
  • 💬 FunctionCallContent / FunctionResultContent:调用意图与函数结果的消息载体
  • 📦 FunctionInvokingChatClient:自动完成调用循环的中间件

🚀 快速上手(4 步)

1)获取 ChatClient

// 方式 A:课程辅助类(推荐)
var chatClient = AIClientHelper.GetDefaultChatClient();

// 方式 B:自行创建(示意)
// var chatClient = new OpenAIChatClient(apiKey: "...", model: "gpt-4o-...");

2)注册工具(Tool)

using Microsoft.Extensions.AI;

// 最小示例:无入参,返回字符串
string GetCurrentWeather() => Random.Shared.NextDouble() > 0.5 ? "It's sunny" : "It's raining";
var tools = AIFunctionFactory.Create(GetCurrentWeather, name: "GetCurrentWeather", description: "查询当前天气");

稍复杂(带描述/类型)

using System.ComponentModel;

public record WeatherReport(string City, int TemperatureCelsius, bool WillRain);

public class TravelToolset
{
    [Description("查询指定城市的实时天气")]
    public WeatherReport QueryWeather(string city)
        => new(city, 25, willRain: false);
}

var travelTools = AIFunctionFactory.CreateFromMethods(new TravelToolset());

3)启用函数调用中间件

var client = chatClient.AsBuilder()
    .UseFunctionInvocation() // 🔧 关键:启用自动函数调用
    .Build();

4)配置并对话

var messages = new List<ChatMessage>
{
    new(ChatRole.System, "你是出行助手,善于调用工具给出穿搭建议。"),
    new(ChatRole.User,   "帮我查看今天北京的天气,要不要带伞?")
};

var options = new ChatOptions
{
    ToolMode = ChatToolMode.Auto,
    Tools = [ tools ] // 或 travelTools
};

var response = await client.GetResponseAsync(messages, options);
Console.WriteLine(response.Text);

💻 可运行最小示例

依赖:Microsoft.Extensions.AI(以及选用的提供方实现,如 Microsoft.Extensions.AI.OpenAI 或 Azure.AI.OpenAI)

using System;
using System.Collections.Generic;
using Microsoft.Extensions.AI;

class Program
{
    static async System.Threading.Tasks.Task Main()
    {
        // 1) 获取 ChatClient
        var chatClient = AIClientHelper.GetDefaultChatClient();

        // 2) 注册工具
        string GetCurrentWeather() => Random.Shared.NextDouble() > 0.5 ? "It's sunny" : "It's raining";
        var tools = AIFunctionFactory.Create(GetCurrentWeather, name: "GetCurrentWeather", description: "查询当前天气");

        // 3) 启用函数调用
        var client = chatClient.AsBuilder().UseFunctionInvocation().Build();

        // 4) 配置与对话
        var messages = new List<ChatMessage>
        {
            new(ChatRole.System, "你是出行助手,善于调用工具给出穿搭建议。"),
            new(ChatRole.User,   "帮我查看今天北京的天气,要不要带伞?")
        };

        var options = new ChatOptions { ToolMode = ChatToolMode.Auto, Tools = [ tools ] };
        var result = await client.GetResponseAsync(messages, options);
        Console.WriteLine(result.Text);
    }
}

🔄 执行流程图(Mermaid)

模式 含义 适用场景
None 禁用工具调用 只对话,不走工具
Auto 模型自行决定是否调用 通用推荐,灵活强
RequireAny 必须调用任意一个工具 强制走工具流程
RequireSpecific("name") 必须调用指定工具 固定关键步骤

❓ 常见问题与解决

  • 模型没调用工具?
    • 🔍 检查 ToolMode 是否为 Auto/Require*
    • 🔍 工具名称/描述/参数是否清晰、可推断
  • 传参不匹配/解析失败?
    • 📝 明确参数类型与描述,避免模糊命名
    • 🛡️ 约束输入(枚举/范围),必要时抛出可读异常
  • 多次工具调用链过长?
    • 🎯 收敛任务目标,提供清晰系统提示与结果格式
    • 🎚️ 需要一步到位时,用 RequireSpecific 强制关键工具

✅ 最佳实践

  • ✍️ 工具命名与描述精炼,面向“模型读者”
  • 🧩 工具只做一件事;返回结构化结果(record/class)更稳
  • 📦 基于 UseFunctionInvocation 叠加中间件:日志/缓存/限流
  • ♻️ 通用能力注册到全局 AdditionalTools,场景内复用
  • 🧯 设置兜底回答与失败重试,提升健壮性

✨ 总结

MEAI 的函数调用把“模型 + 你的业务能力”无缝拼起来:声明工具、打开开关、开始对话,剩下的交给中间件自动编排。