forked from PowerShell/AIShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathService.cs
More file actions
187 lines (157 loc) · 6.22 KB
/
Service.cs
File metadata and controls
187 lines (157 loc) · 6.22 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System.ClientModel;
using System.ClientModel.Primitives;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Extensions.AI;
using OpenAI;
using OpenAIChatClient = OpenAI.Chat.ChatClient;
using ChatMessage = Microsoft.Extensions.AI.ChatMessage;
using AIShell.Abstraction;
namespace AIShell.OpenAI.Agent;
internal class ChatService
{
// TODO: Maybe expose this to our model registration?
private const int MaxResponseToken = 1000;
private readonly string _historyRoot;
private readonly List<ChatMessage> _chatHistory;
private readonly ChatOptions _chatOptions;
private GPT _gptToUse;
private Settings _settings;
private IChatClient _client;
internal ChatService(string historyRoot, Settings settings)
{
_chatHistory = [];
_historyRoot = historyRoot;
_settings = settings;
_chatOptions = new ChatOptions()
{
MaxOutputTokens = MaxResponseToken,
};
}
internal List<ChatMessage> ChatHistory => _chatHistory;
internal void RefreshSettings(Settings settings)
{
_settings = settings;
}
private void RefreshOpenAIClient()
{
if (ReferenceEquals(_gptToUse, _settings.Active))
{
// Active GPT was not changed.
return;
}
GPT old = _gptToUse;
_gptToUse = _settings.Active;
_chatHistory.Clear();
if (old is not null
&& old.Type == _gptToUse.Type
&& string.Equals(old.Endpoint, _gptToUse.Endpoint)
&& string.Equals(old.Deployment, _gptToUse.Deployment)
&& string.Equals(old.ModelName, _gptToUse.ModelName)
&& old.AuthType == _gptToUse.AuthType
&& (old.AuthType is AuthType.EntraID || old.Key.IsEqualTo(_gptToUse.Key)))
{
// It's the same endpoint and auth type, so we reuse the existing client.
return;
}
OpenAIChatClient client;
EndpointType type = _gptToUse.Type;
// Reasoning models do not support the temperature setting.
_chatOptions.Temperature = _gptToUse.ModelInfo.Reasoning ? null : 0.5f;
if (type is EndpointType.AzureOpenAI)
{
// Create a client that targets Azure OpenAI service or Azure API Management service.
var clientOptions = new AzureOpenAIClientOptions() { RetryPolicy = new ChatRetryPolicy() };
bool isApimEndpoint = _gptToUse.Endpoint.EndsWith(Utils.ApimGatewayDomain);
if (_gptToUse.AuthType is AuthType.ApiKey)
{
string userKey = Utils.ConvertFromSecureString(_gptToUse.Key);
if (isApimEndpoint)
{
clientOptions.AddPolicy(
ApiKeyAuthenticationPolicy.CreateHeaderApiKeyPolicy(
new ApiKeyCredential(userKey),
Utils.ApimAuthorizationHeader),
PipelinePosition.PerTry);
}
string azOpenAIApiKey = isApimEndpoint ? "placeholder-api-key" : userKey;
var aiClient = new AzureOpenAIClient(
new Uri(_gptToUse.Endpoint),
new ApiKeyCredential(azOpenAIApiKey),
clientOptions);
client = aiClient.GetChatClient(_gptToUse.Deployment);
}
else
{
var credential = new DefaultAzureCredential(
new DefaultAzureCredentialOptions
{
ExcludeInteractiveBrowserCredential = false,
ExcludeEnvironmentCredential = true,
ExcludeManagedIdentityCredential = true,
}
);
var aiClient = new AzureOpenAIClient(
new Uri(_gptToUse.Endpoint),
credential,
clientOptions);
client = aiClient.GetChatClient(_gptToUse.Deployment);
}
}
else
{
// Create a client that targets the non-Azure OpenAI service.
var clientOptions = new OpenAIClientOptions() { RetryPolicy = new ChatRetryPolicy() };
if (type is EndpointType.CompatibleThirdParty)
{
clientOptions.Endpoint = new(_gptToUse.Endpoint);
}
string userKey = Utils.ConvertFromSecureString(_gptToUse.Key);
var aiClient = new OpenAIClient(new ApiKeyCredential(userKey), clientOptions);
client = aiClient.GetChatClient(_gptToUse.ModelName);
}
_client = client.AsIChatClient()
.AsBuilder()
.UseFunctionInvocation(configure: c => c.IncludeDetailedErrors = true)
.Build();
}
private void PrepareForChat(string input, IShell shell)
{
// Refresh the client in case the active model was changed.
RefreshOpenAIClient();
if (_chatHistory.Count is 0)
{
string system = _gptToUse.SystemPrompt;
if (string.IsNullOrEmpty(system))
{
system = shell.ChannelEstablished
? Prompt.SystemPromptWithConnectedPSSession
: Prompt.SystemPrompForStandaloneApp;
}
_chatHistory.Add(new(ChatRole.System, system));
}
_chatHistory.Add(new(ChatRole.User, input));
}
public async Task<IAsyncEnumerator<ChatResponseUpdate>> GetStreamingChatResponseAsync(string input, IShell shell, CancellationToken cancellationToken)
{
try
{
PrepareForChat(input, shell);
var tools = await shell.GetAIFunctions();
if (tools is { Count: > 0 })
{
_chatOptions.Tools = [.. tools];
}
IAsyncEnumerator<ChatResponseUpdate> enumerator = _client
.GetStreamingResponseAsync(_chatHistory, _chatOptions, cancellationToken)
.GetAsyncEnumerator(cancellationToken);
return await enumerator
.MoveNextAsync()
.ConfigureAwait(continueOnCapturedContext: false) ? enumerator : null;
}
catch (OperationCanceledException)
{
return null;
}
}
}