1. 首页>
  2. 技术文章>
  3. MAF第1个入门例子

MAF第1个入门例子

8/23/26 10:11:58 PM 浏览 345 评论 0

Microsoft agent framework

引入包:

package Microsoft.Agents.AI.OpenAI

详细代码:

using Microsoft.Agents.AI;
using OpenAI;
using OpenAI.Responses;
using System;
using System.ClientModel;
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");
#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();
}


网友讨论