Files
Luna/Luna.Agents/Librarian/LibrarianAgent.cs
T
darman 29eb3851c9 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.
2026-04-04 04:10:51 +02:00

60 lines
1.9 KiB
C#

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;
}
}
}
}