-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
53 lines (41 loc) · 1.83 KB
/
Copy pathProgram.cs
File metadata and controls
53 lines (41 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using A2A;
using A2A.AspNetCore;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OllamaSharp;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<IChatClient>(_ =>
new OllamaApiClient(
new Uri(Environment.GetEnvironmentVariable("OLLAMA_ENDPOINT") ?? "http://localhost:11434"),
Environment.GetEnvironmentVariable("OLLAMA_MODEL") ?? "phi4-mini"));
builder.Services.AddSingleton<AIAgent>(sp =>
new ChatClientAgent(
sp.GetRequiredService<IChatClient>(),
instructions: "You are an A2A research specialist. Answer concisely with cited claims.",
name: "ResearchSpecialist"));
var agentCard = new AgentCard
{
Name = "ResearchSpecialist",
Description = "An A2A research specialist that answers concisely with cited claims.",
Version = "1.0.0",
DefaultInputModes = ["text"],
DefaultOutputModes = ["text"],
};
builder.Services.AddA2AAgent<AIAgentHandler>(agentCard);
var app = builder.Build();
app.MapA2A("/a2a");
app.MapWellKnownAgentCard(agentCard, "/a2a");
app.Run();
internal sealed class AIAgentHandler(AIAgent agent) : IAgentHandler
{
public async Task ExecuteAsync(RequestContext context, AgentEventQueue eventQueue, CancellationToken cancellationToken)
{
var input = string.Concat(context.Message.Parts.Select(p => p.Text ?? string.Empty));
AgentSession session = await agent.CreateSessionAsync(cancellationToken);
AgentResponse response = await agent.RunAsync(input, session, cancellationToken: cancellationToken);
var responder = new MessageResponder(eventQueue, context.ContextId);
await responder.ReplyAsync(response.ToString(), cancellationToken);
}
public Task CancelAsync(RequestContext context, AgentEventQueue eventQueue, CancellationToken cancellationToken)
=> Task.CompletedTask;
}