2024-10-29 03:28:09 +00:00
|
|
|
using BotSharp.Abstraction.Agents.Enums;
|
|
|
|
|
using BotSharp.Abstraction.Agents.Models;
|
|
|
|
|
using BotSharp.Abstraction.Conversations;
|
|
|
|
|
using BotSharp.Abstraction.Conversations.Models;
|
|
|
|
|
using BotSharp.Abstraction.Loggers;
|
|
|
|
|
using BotSharp.Abstraction.MLTasks;
|
|
|
|
|
using Microsoft.Extensions.AI;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.MicrosoftExtensionsAI;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides an implementation of <see cref="ITextCompletion"/> for Microsoft.Extensions.AI.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class MicrosoftExtensionsAITextCompletionProvider : ITextCompletion
|
|
|
|
|
{
|
|
|
|
|
private readonly IChatClient _chatClient;
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly ITokenStatistics _tokenStatistics;
|
|
|
|
|
private string? _model = null;
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public string Provider => "microsoft-extensions-ai";
|
2025-03-05 23:22:46 +00:00
|
|
|
public string Model => _model;
|
2024-10-29 03:28:09 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates an instance of the <see cref="MicrosoftExtensionsAITextCompletionProvider"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public MicrosoftExtensionsAITextCompletionProvider(
|
|
|
|
|
IChatClient chatClient,
|
|
|
|
|
IServiceProvider services,
|
|
|
|
|
ITokenStatistics tokenStatistics)
|
|
|
|
|
{
|
|
|
|
|
_chatClient = chatClient;
|
|
|
|
|
_services = services;
|
|
|
|
|
_tokenStatistics = tokenStatistics;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public async Task<string> GetCompletion(string text, string agentId, string messageId)
|
|
|
|
|
{
|
|
|
|
|
var hooks = _services.GetServices<IContentGeneratingHook>().ToArray();
|
|
|
|
|
|
|
|
|
|
// Before chat completion hook
|
|
|
|
|
Agent agent = new() { Id = agentId };
|
|
|
|
|
RoleDialogModel userMessage = new(AgentRole.User, text) { MessageId = messageId };
|
|
|
|
|
await Task.WhenAll(hooks.Select(hook => hook.BeforeGenerating(agent, [userMessage])));
|
|
|
|
|
|
|
|
|
|
_tokenStatistics.StartTimer();
|
2025-02-17 05:38:25 +00:00
|
|
|
var completion = await _chatClient.GetResponseAsync(text);
|
2024-10-29 03:28:09 +00:00
|
|
|
var result = string.Concat(completion.Message.Contents.OfType<TextContent>());
|
|
|
|
|
_tokenStatistics.StopTimer();
|
|
|
|
|
|
|
|
|
|
// After chat completion hook
|
|
|
|
|
await Task.WhenAll(hooks.Select(hook =>
|
|
|
|
|
hook.AfterGenerated(new(AgentRole.Assistant, result), new() { Model = _model ?? "default" })));
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public void SetModelName(string model)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(model))
|
|
|
|
|
{
|
|
|
|
|
_model = model;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|