-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathAzureOpenAITextGenerator.cs
More file actions
191 lines (162 loc) · 7.11 KB
/
AzureOpenAITextGenerator.cs
File metadata and controls
191 lines (162 loc) · 7.11 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
188
189
190
191
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Azure;
using Azure.AI.OpenAI;
using Azure.Core.Pipeline;
using Azure.Identity;
using Helpers;
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory.AI.AzureOpenAI.Internals;
using Microsoft.KernelMemory.AI.OpenAI;
using Microsoft.KernelMemory.Diagnostics;
namespace Microsoft.KernelMemory.AI.AzureOpenAI;
[Experimental("KMEXP01")]
public sealed class AzureOpenAITextGenerator : ITextGenerator
{
private readonly ITextTokenizer _textTokenizer;
private readonly OpenAIClient _client;
private readonly ILogger<AzureOpenAITextGenerator> _log;
private readonly bool _useTextCompletionProtocol;
private readonly string _deployment;
public AzureOpenAITextGenerator(
AzureOpenAIConfig config,
ITextTokenizer? textTokenizer = null,
ILoggerFactory? loggerFactory = null,
HttpClient? httpClient = null)
{
this._log = (loggerFactory ?? DefaultLogger.Factory).CreateLogger<AzureOpenAITextGenerator>();
if (textTokenizer == null)
{
this._log.LogWarning(
"Tokenizer not specified, will use {0}. The token count might be incorrect, causing unexpected errors",
nameof(GPT4Tokenizer));
textTokenizer = new GPT4Tokenizer();
}
this._textTokenizer = textTokenizer;
if (string.IsNullOrEmpty(config.Endpoint))
{
throw new ConfigurationException($"Azure OpenAI: {config.Endpoint} is empty");
}
if (string.IsNullOrEmpty(config.Deployment))
{
throw new ConfigurationException($"Azure OpenAI: {config.Deployment} is empty");
}
this._useTextCompletionProtocol = config.APIType == AzureOpenAIConfig.APITypes.TextCompletion;
this._deployment = config.Deployment;
this.MaxTokenTotal = config.MaxTokenTotal;
OpenAIClientOptions options = new()
{
RetryPolicy = new RetryPolicy(maxRetries: Math.Max(0, config.MaxRetries), new SequentialDelayStrategy()),
Diagnostics =
{
IsTelemetryEnabled = Telemetry.IsTelemetryEnabled,
ApplicationId = Telemetry.HttpUserAgent,
}
};
if (httpClient is not null)
{
options.Transport = new HttpClientTransport(httpClient);
}
switch (config.Auth)
{
case AzureOpenAIConfig.AuthTypes.AzureIdentity:
this._client = new OpenAIClient(new Uri(config.Endpoint), azure_credential_utils.GetAzureCredential(config.APP_ENV), options);
break;
case AzureOpenAIConfig.AuthTypes.ManualTokenCredential:
this._client = new OpenAIClient(new Uri(config.Endpoint), config.GetTokenCredential(), options);
break;
case AzureOpenAIConfig.AuthTypes.APIKey:
if (string.IsNullOrEmpty(config.APIKey))
{
throw new ConfigurationException($"Azure OpenAI: {config.APIKey} is empty");
}
this._client = new OpenAIClient(new Uri(config.Endpoint), new AzureKeyCredential(config.APIKey), options);
break;
default:
throw new ConfigurationException($"Azure OpenAI: authentication type '{config.Auth:G}' is not supported");
}
}
/// <inheritdoc/>
public int MaxTokenTotal { get; }
/// <inheritdoc/>
public int CountTokens(string text)
{
return this._textTokenizer.CountTokens(text);
}
/// <inheritdoc/>
public IReadOnlyList<string> GetTokens(string text)
{
return this._textTokenizer.GetTokens(text);
}
/// <inheritdoc/>
public async IAsyncEnumerable<string> GenerateTextAsync(
string prompt,
TextGenerationOptions options,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (this._useTextCompletionProtocol)
{
this._log.LogTrace("Sending text generation request, deployment '{0}'", this._deployment);
var openaiOptions = new CompletionsOptions
{
DeploymentName = this._deployment,
MaxTokens = options.MaxTokens,
Temperature = (float)options.Temperature,
NucleusSamplingFactor = (float)options.NucleusSampling,
FrequencyPenalty = (float)options.FrequencyPenalty,
PresencePenalty = (float)options.PresencePenalty,
ChoicesPerPrompt = 1,
};
if (options.StopSequences is { Count: > 0 })
{
foreach (var s in options.StopSequences) { openaiOptions.StopSequences.Add(s); }
}
if (options.TokenSelectionBiases is { Count: > 0 })
{
foreach (var (token, bias) in options.TokenSelectionBiases) { openaiOptions.TokenSelectionBiases.Add(token, (int)bias); }
}
StreamingResponse<Completions>? response = await this._client.GetCompletionsStreamingAsync(openaiOptions, cancellationToken).ConfigureAwait(false);
await foreach (Completions? completions in response.EnumerateValues().WithCancellation(cancellationToken).ConfigureAwait(false))
{
foreach (Choice? choice in completions.Choices)
{
yield return choice.Text;
}
}
}
else
{
this._log.LogTrace("Sending chat message generation request, deployment '{0}'", this._deployment);
var openaiOptions = new ChatCompletionsOptions
{
DeploymentName = this._deployment,
MaxTokens = options.MaxTokens,
Temperature = (float)options.Temperature,
NucleusSamplingFactor = (float)options.NucleusSampling,
FrequencyPenalty = (float)options.FrequencyPenalty,
PresencePenalty = (float)options.PresencePenalty,
// ChoiceCount = 1,
};
if (options.StopSequences is { Count: > 0 })
{
foreach (var s in options.StopSequences) { openaiOptions.StopSequences.Add(s); }
}
if (options.TokenSelectionBiases is { Count: > 0 })
{
foreach (var (token, bias) in options.TokenSelectionBiases) { openaiOptions.TokenSelectionBiases.Add(token, (int)bias); }
}
openaiOptions.Messages.Add(new ChatRequestSystemMessage(prompt));
StreamingResponse<StreamingChatCompletionsUpdate>? response = await this._client.GetChatCompletionsStreamingAsync(openaiOptions, cancellationToken).ConfigureAwait(false);
await foreach (StreamingChatCompletionsUpdate? update in response.EnumerateValues().WithCancellation(cancellationToken).ConfigureAwait(false))
{
yield return update.ContentUpdate;
}
}
}
}