Add agent system with Core and Librarian agent implementations
Define IAgent interface and implement CoreAgent (main conversational agent) and LibrarianAgent (knowledge retrieval agent). Includes DI service collection extensions for agent registration.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using Luna.Agents.Abstractions;
|
||||
using Luna.Configuration;
|
||||
using Luna.Providers.Abstractions;
|
||||
using Microsoft.Extensions.AI;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Luna.Agents.Librarian;
|
||||
|
||||
public class LibrarianAgent(
|
||||
[FromKeyedServices("Mistral")] IProvider provider,
|
||||
IOptionsMonitor<AgentOptions> options)
|
||||
: IAgent
|
||||
{
|
||||
private readonly IChatClient chatClient = provider.GetChatClient(options.Get(Name).ModelId);
|
||||
|
||||
public static string Name => "Librarian";
|
||||
|
||||
public string DisplayName => Options.DisplayName ?? Name;
|
||||
public string Description => Options.Description ?? string.Empty;
|
||||
public AgentOptions Options { get; init; } = options.Get(Name);
|
||||
|
||||
public async Task<IEnumerable<ChatMessage>> ProcessAsync(
|
||||
IReadOnlyList<ChatMessage> messages,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var chatOptions = new ChatOptions
|
||||
{
|
||||
Instructions = Options.Instructions
|
||||
};
|
||||
|
||||
var messagesWithoutSystemPrompt = messages
|
||||
.Where(m => m.Role != ChatRole.System)
|
||||
.ToList();
|
||||
|
||||
var response = await chatClient.GetResponseAsync(messagesWithoutSystemPrompt, chatOptions, ct);
|
||||
return response.Messages
|
||||
.Where(m => m.Role != ChatRole.System);
|
||||
}
|
||||
|
||||
public async IAsyncEnumerable<ChatResponseUpdate> ProcessStreamingAsync(IReadOnlyList<ChatMessage> messages, CancellationToken ct = default)
|
||||
{
|
||||
var chatOptions = new ChatOptions
|
||||
{
|
||||
Instructions = Options.Instructions
|
||||
};
|
||||
|
||||
var messagesWithoutSystemPrompt = messages
|
||||
.Where(m => m.Role != ChatRole.System)
|
||||
.ToList();
|
||||
|
||||
await foreach (var update in chatClient.GetStreamingResponseAsync(messagesWithoutSystemPrompt, chatOptions, ct))
|
||||
{
|
||||
if (update.Role != ChatRole.System)
|
||||
{
|
||||
yield return update;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user