-
-
Notifications
You must be signed in to change notification settings - Fork 538
/
Copy pathSemanticKernelChatCompletionProvider.cs
108 lines (96 loc) · 4 KB
/
SemanticKernelChatCompletionProvider.cs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using BotSharp.Abstraction.Agents;
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.DependencyInjection;
using Microsoft.SemanticKernel.ChatCompletion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BotSharp.Plugin.SemanticKernel
{
/// <summary>
/// Use Semantic Kernel as chat completion provider
/// </summary>
public class SemanticKernelChatCompletionProvider : IChatCompletion
{
private Microsoft.SemanticKernel.ChatCompletion.IChatCompletionService _kernelChatCompletion;
private IServiceProvider _services;
private ITokenStatistics _tokenStatistics;
private string _model;
/// <inheritdoc/>
public string Provider => "semantic-kernel";
public string Model => _model;
/// <summary>
/// Create a new instance of <see cref="SemanticKernelChatCompletionProvider"/>
/// </summary>
/// <param name="chatCompletion"></param>
/// <param name="services"></param>
/// <param name="tokenStatistics"></param>
public SemanticKernelChatCompletionProvider(IChatCompletionService chatCompletion,
IServiceProvider services,
ITokenStatistics tokenStatistics)
{
this._kernelChatCompletion = chatCompletion;
this._services = services;
this._tokenStatistics = tokenStatistics;
}
/// <inheritdoc/>
public async Task<RoleDialogModel> GetChatCompletions(Agent agent, List<RoleDialogModel> conversations)
{
var hooks = _services.GetServices<IContentGeneratingHook>().ToList();
// Before chat completion hook
Task.WaitAll(hooks.Select(hook =>
hook.BeforeGenerating(agent, conversations)).ToArray());
var completion = this._kernelChatCompletion;
var agentService = _services.GetRequiredService<IAgentService>();
var instruction = agentService.RenderedInstruction(agent);
ChatHistory chatHistory = new ChatHistory(instruction);
foreach (var message in conversations)
{
if (message.Role == AgentRole.User)
{
chatHistory.AddUserMessage(message.Content);
}
else
{
chatHistory.AddAssistantMessage(message.Content);
}
}
var ChatMessage = await completion.GetChatMessageContentsAsync(chatHistory);
var chatMessageContent = ChatMessage?.FirstOrDefault();
var response = chatMessageContent != null ? chatMessageContent.Content :string.Empty;
var msg = new RoleDialogModel(AgentRole.Assistant, response)
{
CurrentAgentId = agent.Id,
RenderedInstruction = instruction
};
// After chat completion hook
Task.WaitAll(hooks.Select(hook =>
hook.AfterGenerated(msg, new TokenStatsModel
{
Model = _model ?? "default"
})).ToArray());
return msg;
}
/// <inheritdoc/>
public Task<bool> GetChatCompletionsAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived, Func<RoleDialogModel, Task> onFunctionExecuting)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public Task<bool> GetChatCompletionsStreamingAsync(Agent agent, List<RoleDialogModel> conversations, Func<RoleDialogModel, Task> onMessageReceived)
{
throw new NotImplementedException();
}
/// <inheritdoc/>
public void SetModelName(string model)
{
_model = model;
}
}
}