1. 首页>
  2. 技术文章>
  3. MAF第2个例子-使用工具

MAF第2个例子-使用工具

8/25/26 3:59:36 PM 浏览 814 评论 0

MAF

MAF使用工具的例子:

using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;
using OpenAI.Responses;
using System;
using System.ClientModel;
using System.ComponentModel;
[Description("获取天气")]
static string GetWeather([Description("要获取天气的城市")] string location)
    => $"你成功的调用获取天气工具,城市:{location},空气温度36,下着暴雨";
var options = new OpenAIClientOptions
{
    Endpoint = new Uri("http://localhost:11434/v1"), // DeepSeek 的 OpenAI 兼容端点
};
OpenAIClient client = new(new ApiKeyCredential("1222"), options);
#pragma warning disable OPENAI001 // GetResponsesClient 是评估用 API,可能在未来变更
AIAgent agent = client.GetResponsesClient().AsAIAgent(
    model: "gemma4:cloud",
    instructions: "你是一个叫猫猫的AI助手",
    name: "MyFristAgent",
    tools: [AIFunctionFactory.Create(GetWeather)]);
#pragma warning restore OPENAI001
AgentSession session = await agent.CreateSessionAsync();
Console.WriteLine("开始与猫猫对话,输入 exit 退出:");
while (true)
{
    Console.Write("你:");
    string? input = Console.ReadLine();
    if (string.IsNullOrWhiteSpace(input))
    {
        continue;
    }
    if (input.Equals("exit", StringComparison.OrdinalIgnoreCase))
    {
        break;
    }
    Console.Write("猫猫:");
    await foreach (var update in agent.RunStreamingAsync(input, session))
    {
        if (!string.IsNullOrEmpty(update.Text))
        {
            Console.Write(update.Text);
        }
    }
    Console.WriteLine();
    Console.WriteLine();
}


网友讨论