Define IAgent interface and implement CoreAgent (main conversational agent) and LibrarianAgent (knowledge retrieval agent). Includes DI service collection extensions for agent registration.
55 lines
1.8 KiB
C#
55 lines
1.8 KiB
C#
using System.Text.Json;
|
|
using Luna.Agents.Abstractions;
|
|
using Luna.Configuration;
|
|
using Luna.Providers.Abstractions;
|
|
using Microsoft.Extensions.AI;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Luna.Agents.Core;
|
|
|
|
public class CoreAgent(
|
|
[FromKeyedServices("Mistral")] IProvider provider,
|
|
IOptionsMonitor<AgentOptions> options,
|
|
ILogger<CoreAgent> logger)
|
|
: IAgent
|
|
{
|
|
private readonly IChatClient chatClient = provider.GetChatClient(options.Get(Name).ModelId);
|
|
|
|
public static string Name => "Core";
|
|
|
|
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 = default)
|
|
{
|
|
var messagesJson = JsonSerializer.Serialize(messages);
|
|
logger.LogInformation("Processing messages: {MessagesJson}", messagesJson);
|
|
|
|
var chatOptions = new ChatOptions
|
|
{
|
|
Instructions = Options.Instructions
|
|
};
|
|
|
|
var response = await chatClient.GetResponseAsync(messages, chatOptions, ct);
|
|
|
|
logger.LogInformation("Response: {ResponseJson}", JsonSerializer.Serialize(response));
|
|
return response.Messages;
|
|
}
|
|
|
|
public IAsyncEnumerable<ChatResponseUpdate> ProcessStreamingAsync(
|
|
IReadOnlyList<ChatMessage> messages,
|
|
CancellationToken ct = default)
|
|
{
|
|
var chatOptions = new ChatOptions
|
|
{
|
|
Instructions = Options.Instructions
|
|
};
|
|
|
|
return chatClient.GetStreamingResponseAsync(messages, chatOptions, ct);
|
|
}
|
|
} |