forked from microsoft/kernel-memory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DependencyInjection.cs
301 lines (270 loc) · 12.6 KB
/
DependencyInjection.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright (c) Microsoft. All rights reserved.
using System.Net.Http;
using Azure.AI.OpenAI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory.AI;
using Microsoft.KernelMemory.AI.OpenAI;
#pragma warning disable IDE0130 // reduce number of "using" statements
// ReSharper disable once CheckNamespace - reduce number of "using" statements
namespace Microsoft.KernelMemory;
public static partial class KernelMemoryBuilderExtensions
{
// Using GPT 3.5 Turbo - https://platform.openai.com/docs/models/gpt-3-5
private const string DefaultTextModel = "gpt-3.5-turbo-16k";
private const int DefaultTextModelMaxToken = 16_384;
// Using Ada v2
private const string DefaultEmbeddingModel = "text-embedding-ada-002";
private const int DefaultEmbeddingModelMaxToken = 8_191;
/// <summary>
/// Use default OpenAI models (3.5-Turbo and Ada-002) and settings for ingestion and retrieval.
/// </summary>
/// <param name="builder">Kernel Memory builder</param>
/// <param name="apiKey">OpenAI API Key</param>
/// <param name="organization">OpenAI Organization ID (usually not required)</param>
/// <param name="textGenerationTokenizer">Tokenizer used to count tokens used by prompts</param>
/// <param name="textEmbeddingTokenizer">Tokenizer used to count tokens sent to the embedding generator</param>
/// <param name="loggerFactory">.NET Logger factory</param>
/// <param name="onlyForRetrieval">Whether to use OpenAI defaults only for ingestion, and not for retrieval (search and ask API)</param>
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <returns>KM builder instance</returns>
public static IKernelMemoryBuilder WithOpenAIDefaults(
this IKernelMemoryBuilder builder,
string apiKey,
string? organization = null,
ITextTokenizer? textGenerationTokenizer = null,
ITextTokenizer? textEmbeddingTokenizer = null,
ILoggerFactory? loggerFactory = null,
bool onlyForRetrieval = false,
HttpClient? httpClient = null)
{
textGenerationTokenizer ??= new DefaultGPTTokenizer();
textEmbeddingTokenizer ??= new DefaultGPTTokenizer();
var openAIConfig = new OpenAIConfig
{
TextModel = DefaultTextModel,
TextModelMaxTokenTotal = DefaultTextModelMaxToken,
EmbeddingModel = DefaultEmbeddingModel,
EmbeddingModelMaxTokenTotal = DefaultEmbeddingModelMaxToken,
APIKey = apiKey,
OrgId = organization
};
openAIConfig.Validate();
builder.Services.AddOpenAITextEmbeddingGeneration(openAIConfig, textEmbeddingTokenizer, httpClient);
builder.Services.AddOpenAITextGeneration(openAIConfig, textGenerationTokenizer, httpClient);
if (!onlyForRetrieval)
{
builder.AddIngestionEmbeddingGenerator(new OpenAITextEmbeddingGenerator(
config: openAIConfig,
textTokenizer: textEmbeddingTokenizer,
loggerFactory: loggerFactory,
httpClient: httpClient));
}
return builder;
}
/// <summary>
/// Use OpenAI models for ingestion and retrieval
/// </summary>
/// <param name="builder">Kernel Memory builder</param>
/// <param name="config">OpenAI settings</param>
/// <param name="textGenerationTokenizer">Tokenizer used to count tokens used by prompts</param>
/// <param name="textEmbeddingTokenizer">Tokenizer used to count tokens sent to the embedding generator</param>
/// <param name="onlyForRetrieval">Whether to use OpenAI only for ingestion, not for retrieval (search and ask API)</param>
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <returns>KM builder instance</returns>
public static IKernelMemoryBuilder WithOpenAI(
this IKernelMemoryBuilder builder,
OpenAIConfig config,
ITextTokenizer? textGenerationTokenizer = null,
ITextTokenizer? textEmbeddingTokenizer = null,
bool onlyForRetrieval = false,
HttpClient? httpClient = null)
{
config.Validate();
textGenerationTokenizer ??= new DefaultGPTTokenizer();
textEmbeddingTokenizer ??= new DefaultGPTTokenizer();
builder.WithOpenAITextEmbeddingGeneration(config, textEmbeddingTokenizer, onlyForRetrieval, httpClient);
builder.WithOpenAITextGeneration(config, textGenerationTokenizer);
return builder;
}
/// <summary>
/// Use OpenAI models for ingestion and retrieval
/// </summary>
/// <param name="builder">Kernel Memory builder</param>
/// <param name="config">OpenAI settings</param>
/// <param name="openAIClient">Custom pre-configured OpenAI client</param>
/// <param name="textGenerationTokenizer">Tokenizer used to count tokens used by prompts</param>
/// <param name="textEmbeddingTokenizer">Tokenizer used to count tokens sent to the embedding generator</param>
/// <param name="onlyForRetrieval">Whether to use OpenAI only for ingestion, not for retrieval (search and ask API)</param>
/// <returns>KM builder instance</returns>
public static IKernelMemoryBuilder WithOpenAI(
this IKernelMemoryBuilder builder,
OpenAIConfig config,
OpenAIClient openAIClient,
ITextTokenizer? textGenerationTokenizer = null,
ITextTokenizer? textEmbeddingTokenizer = null,
bool onlyForRetrieval = false)
{
config.Validate();
textGenerationTokenizer ??= new DefaultGPTTokenizer();
textEmbeddingTokenizer ??= new DefaultGPTTokenizer();
builder.WithOpenAITextEmbeddingGeneration(config, openAIClient, textEmbeddingTokenizer, onlyForRetrieval);
builder.WithOpenAITextGeneration(config, openAIClient, textGenerationTokenizer);
return builder;
}
/// <summary>
/// Use OpenAI to generate text embedding.
/// </summary>
/// <param name="builder">Kernel Memory builder</param>
/// <param name="config">OpenAI settings</param>
/// <param name="textTokenizer">Tokenizer used to count tokens sent to the embedding generator</param>
/// <param name="onlyForRetrieval">Whether to use OpenAI only for ingestion, not for retrieval (search and ask API)</param>
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <returns>KM builder instance</returns>
public static IKernelMemoryBuilder WithOpenAITextEmbeddingGeneration(
this IKernelMemoryBuilder builder,
OpenAIConfig config,
ITextTokenizer? textTokenizer = null,
bool onlyForRetrieval = false,
HttpClient? httpClient = null)
{
config.Validate();
textTokenizer ??= new DefaultGPTTokenizer();
builder.Services.AddOpenAITextEmbeddingGeneration(config, httpClient: httpClient);
if (!onlyForRetrieval)
{
builder.AddIngestionEmbeddingGenerator(
new OpenAITextEmbeddingGenerator(config, textTokenizer, loggerFactory: null, httpClient));
}
return builder;
}
/// <summary>
/// Use OpenAI to generate text embedding.
/// </summary>
/// <param name="builder">Kernel Memory builder</param>
/// <param name="config">OpenAI settings</param>
/// <param name="openAIClient">Custom pre-configured OpenAI client</param>
/// <param name="textTokenizer">Tokenizer used to count tokens sent to the embedding generator</param>
/// <param name="onlyForRetrieval">Whether to use OpenAI only for ingestion, not for retrieval (search and ask API)</param>
/// <returns>KM builder instance</returns>
public static IKernelMemoryBuilder WithOpenAITextEmbeddingGeneration(
this IKernelMemoryBuilder builder,
OpenAIConfig config,
OpenAIClient openAIClient,
ITextTokenizer? textTokenizer = null,
bool onlyForRetrieval = false)
{
config.Validate();
textTokenizer ??= new DefaultGPTTokenizer();
builder.Services.AddOpenAITextEmbeddingGeneration(config, openAIClient);
if (!onlyForRetrieval)
{
builder.AddIngestionEmbeddingGenerator(
new OpenAITextEmbeddingGenerator(config, openAIClient, textTokenizer));
}
return builder;
}
/// <summary>
/// Use OpenAI to generate text, e.g. answers and summaries.
/// </summary>
/// <param name="builder">Kernel Memory builder</param>
/// <param name="config">OpenAI settings</param>
/// <param name="textTokenizer">Tokenizer used to count tokens used by prompts</param>
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <returns>KM builder instance</returns>
public static IKernelMemoryBuilder WithOpenAITextGeneration(
this IKernelMemoryBuilder builder,
OpenAIConfig config,
ITextTokenizer? textTokenizer = null,
HttpClient? httpClient = null)
{
config.Validate();
textTokenizer ??= new DefaultGPTTokenizer();
builder.Services.AddOpenAITextGeneration(config, textTokenizer, httpClient);
return builder;
}
/// <summary>
/// Use OpenAI to generate text, e.g. answers and summaries.
/// </summary>
/// <param name="builder">Kernel Memory builder</param>
/// <param name="config">OpenAI settings</param>
/// <param name="openAIClient">Custom pre-configured OpenAI client</param>
/// <param name="textTokenizer">Tokenizer used to count tokens used by prompts</param>
/// <returns>KM builder instance</returns>
public static IKernelMemoryBuilder WithOpenAITextGeneration(
this IKernelMemoryBuilder builder,
OpenAIConfig config,
OpenAIClient openAIClient,
ITextTokenizer? textTokenizer = null)
{
config.Validate();
textTokenizer ??= new DefaultGPTTokenizer();
builder.Services.AddOpenAITextGeneration(config, openAIClient, textTokenizer);
return builder;
}
}
public static partial class DependencyInjection
{
public static IServiceCollection AddOpenAITextEmbeddingGeneration(
this IServiceCollection services,
OpenAIConfig config,
ITextTokenizer? textTokenizer = null,
HttpClient? httpClient = null)
{
config.Validate();
textTokenizer ??= new DefaultGPTTokenizer();
return services
.AddSingleton<ITextEmbeddingGenerator>(
serviceProvider => new OpenAITextEmbeddingGenerator(
config: config,
textTokenizer: textTokenizer,
loggerFactory: serviceProvider.GetService<ILoggerFactory>(),
httpClient));
}
public static IServiceCollection AddOpenAITextEmbeddingGeneration(
this IServiceCollection services,
OpenAIConfig config,
OpenAIClient openAIClient,
ITextTokenizer? textTokenizer = null)
{
config.Validate();
textTokenizer ??= new DefaultGPTTokenizer();
return services
.AddSingleton<ITextEmbeddingGenerator>(
serviceProvider => new OpenAITextEmbeddingGenerator(
config: config,
openAIClient: openAIClient,
textTokenizer: textTokenizer,
loggerFactory: serviceProvider.GetService<ILoggerFactory>()));
}
public static IServiceCollection AddOpenAITextGeneration(
this IServiceCollection services,
OpenAIConfig config,
ITextTokenizer? textTokenizer = null,
HttpClient? httpClient = null)
{
config.Validate();
textTokenizer ??= new DefaultGPTTokenizer();
return services
.AddSingleton<ITextGenerator, OpenAITextGenerator>(serviceProvider => new OpenAITextGenerator(
config: config,
textTokenizer: textTokenizer,
loggerFactory: serviceProvider.GetService<ILoggerFactory>(),
httpClient));
}
public static IServiceCollection AddOpenAITextGeneration(
this IServiceCollection services,
OpenAIConfig config,
OpenAIClient openAIClient,
ITextTokenizer? textTokenizer = null)
{
config.Validate();
textTokenizer ??= new DefaultGPTTokenizer();
return services
.AddSingleton<ITextGenerator, OpenAITextGenerator>(serviceProvider => new OpenAITextGenerator(
config: config,
openAIClient: openAIClient,
textTokenizer: textTokenizer,
loggerFactory: serviceProvider.GetService<ILoggerFactory>()));
}
}