44 lines
1.5 KiB
C#
44 lines
1.5 KiB
C#
using MAF1.Tools;
|
|||
|
|
using MAF1.Utils;
|
||
|
|
using Microsoft.Agents.AI;
|
||
|
|
using Microsoft.Extensions.AI;
|
||
|
|
|
||
|
|
namespace MAF1.Agents.Weather;
|
||
|
|
|
||
|
|
public static class WeatherAgent
|
||
|
|
{
|
||
|
|
public static AIAgent Create(AgentFactory factory, WeatherTools weatherTools)
|
||
|
|
{
|
||
|
|
return factory.CreateAgent(
|
||
|
|
name: "WeatherAgent",
|
||
|
|
instructions:
|
||
|
|
"""
|
||
|
|
你负责查询天气。用户给出的每个城市都必须调用一次 GetWeather,不要编造天气。
|
||
|
|
用中文汇总所有城市的天气。
|
||
|
|
""",
|
||
|
|
tools: [AIFunctionFactory.Create(weatherTools.GetWeather)]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static async Task<AgentStepResult> RunAsync(
|
||
|
|
AIAgent agent,
|
||
|
|
IReadOnlyDictionary<string, object?> inputs,
|
||
|
|
CancellationToken cancellationToken = default)
|
||
|
|
{
|
||
|
|
List<string> cities = AgentInputs.ReadStringList(inputs, "cities");
|
||
|
|
if (cities.Count == 0)
|
||
|
|
{
|
||
|
|
throw new InvalidOperationException("输入 cities 为空。请把上一节点的 cities 连到本节点的 cities。");
|
||
|
|
}
|
||
|
|
|
||
|
|
AgentResponse response = await agent.RunAsync(
|
||
|
|
$"请查询这些城市的天气:{string.Join("、", cities)}",
|
||
|
|
cancellationToken: cancellationToken);
|
||
|
|
return new AgentStepResult
|
||
|
|
{
|
||
|
|
Message = response.Text,
|
||
|
|
Inputs = new Dictionary<string, object?> { ["cities"] = cities },
|
||
|
|
Outputs = new Dictionary<string, object?> { ["summary"] = response.Text },
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|