forked from openai/openai-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbeddingClient.cs
More file actions
211 lines (183 loc) · 13.7 KB
/
EmbeddingClient.cs
File metadata and controls
211 lines (183 loc) · 13.7 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace OpenAI.Embeddings;
// CUSTOM:
// - Renamed.
// - Suppressed constructor that takes endpoint parameter; endpoint is now a property in the options class.
// - Suppressed methods that only take the options parameter.
/// <summary> The service client for OpenAI embedding operations. </summary>
[CodeGenClient("Embeddings")]
[CodeGenSuppress("EmbeddingClient", typeof(ClientPipeline), typeof(ApiKeyCredential), typeof(Uri))]
[CodeGenSuppress("CreateEmbeddingAsync", typeof(EmbeddingGenerationOptions), typeof(CancellationToken))]
[CodeGenSuppress("CreateEmbedding", typeof(EmbeddingGenerationOptions), typeof(CancellationToken))]
public partial class EmbeddingClient
{
private readonly string _model;
// CUSTOM: Added as a convenience.
/// <summary> Initializes a new instance of <see cref="EmbeddingClient"/>. </summary>
/// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param>
/// <param name="apiKey"> The API key to authenticate with the service. </param>
/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="apiKey"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
public EmbeddingClient(string model, string apiKey) : this(model, new ApiKeyCredential(apiKey), new OpenAIClientOptions())
{
}
// CUSTOM:
// - Added `model` parameter.
// - Used a custom pipeline.
// - Demoted the endpoint parameter to be a property in the options class.
/// <summary> Initializes a new instance of <see cref="EmbeddingClient"/>. </summary>
/// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param>
/// <param name="credential"> The API key to authenticate with the service. </param>
/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
public EmbeddingClient(string model, ApiKeyCredential credential) : this(model, credential, new OpenAIClientOptions())
{
}
// CUSTOM:
// - Added `model` parameter.
// - Used a custom pipeline.
// - Demoted the endpoint parameter to be a property in the options class.
/// <summary> Initializes a new instance of <see cref="EmbeddingClient"/>. </summary>
/// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param>
/// <param name="credential"> The API key to authenticate with the service. </param>
/// <param name="options"> The options to configure the client. </param>
/// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
public EmbeddingClient(string model, ApiKeyCredential credential, OpenAIClientOptions options)
{
Argument.AssertNotNullOrEmpty(model, nameof(model));
Argument.AssertNotNull(credential, nameof(credential));
options ??= new OpenAIClientOptions();
_model = model;
Pipeline = OpenAIClient.CreatePipeline(credential, options);
_endpoint = OpenAIClient.GetEndpoint(options);
}
// CUSTOM:
// - Added `model` parameter.
// - Used a custom pipeline.
// - Demoted the endpoint parameter to be a property in the options class.
// - Made protected.
/// <summary> Initializes a new instance of <see cref="EmbeddingClient"/>. </summary>
/// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param>
/// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param>
/// <param name="options"> The options to configure the client. </param>
/// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> or <paramref name="model"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
protected internal EmbeddingClient(ClientPipeline pipeline, string model, OpenAIClientOptions options)
{
Argument.AssertNotNull(pipeline, nameof(pipeline));
Argument.AssertNotNullOrEmpty(model, nameof(model));
options ??= new OpenAIClientOptions();
_model = model;
Pipeline = pipeline;
_endpoint = OpenAIClient.GetEndpoint(options);
}
// CUSTOM: Added to simplify generating a single embedding from a string input.
/// <summary> Generates an embedding representing the text input. </summary>
/// <param name="input"> The text input to generate an embedding for. </param>
/// <param name="options"> The options to configure the embedding generation. </param>
/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
/// <exception cref="ArgumentNullException"> <paramref name="input"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="input"/> is an empty string, and was expected to be non-empty. </exception>
public virtual async Task<ClientResult<OpenAIEmbedding>> GenerateEmbeddingAsync(string input, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(input, nameof(input));
options ??= new();
CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(input, SourceGenerationContext.Default.String), ref options);
using BinaryContent content = options;
ClientResult result = await GenerateEmbeddingsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue(((OpenAIEmbeddingCollection)result).FirstOrDefault(), result.GetRawResponse());
}
// CUSTOM: Added to simplify generating a single embedding from a string input.
/// <summary> Generates an embedding representing the text input. </summary>
/// <param name="input"> The text input to generate an embedding for. </param>
/// <param name="options"> The options to configure the embedding generation. </param>
/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
/// <exception cref="ArgumentNullException"> <paramref name="input"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="input"/> is an empty string, and was expected to be non-empty. </exception>
public virtual ClientResult<OpenAIEmbedding> GenerateEmbedding(string input, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(input, nameof(input));
options ??= new();
CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(input, SourceGenerationContext.Default.String), ref options);
using BinaryContent content = options;
ClientResult result = GenerateEmbeddings(content, cancellationToken.ToRequestOptions());
return ClientResult.FromValue(((OpenAIEmbeddingCollection)result).FirstOrDefault(), result.GetRawResponse());
}
// CUSTOM: Added to simplify passing the input as a collection of strings instead of BinaryData.
/// <summary> Generates embeddings representing the text inputs. </summary>
/// <param name="inputs"> The text inputs to generate embeddings for. </param>
/// <param name="options"> The options to configure the embedding generation. </param>
/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
/// <exception cref="ArgumentNullException"> <paramref name="inputs"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="inputs"/> is an empty collection, and was expected to be non-empty. </exception>
public virtual async Task<ClientResult<OpenAIEmbeddingCollection>> GenerateEmbeddingsAsync(IEnumerable<string> inputs, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(inputs, nameof(inputs));
options ??= new();
CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs, SourceGenerationContext.Default.IEnumerableString), ref options);
using BinaryContent content = options;
ClientResult result = await GenerateEmbeddingsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((OpenAIEmbeddingCollection)result, result.GetRawResponse());
}
// CUSTOM: Added to simplify passing the input as a collection of strings instead of BinaryData.
/// <summary> Generates embeddings representing the text inputs. </summary>
/// <param name="inputs"> The text inputs to generate embeddings for. </param>
/// <param name="options"> The options to configure the embedding generation. </param>
/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
/// <exception cref="ArgumentNullException"> <paramref name="inputs"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="inputs"/> is an empty collection, and was expected to be non-empty. </exception>
public virtual ClientResult<OpenAIEmbeddingCollection> GenerateEmbeddings(IEnumerable<string> inputs, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(inputs, nameof(inputs));
options ??= new();
CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs, SourceGenerationContext.Default.IEnumerableString), ref options);
using BinaryContent content = options;
ClientResult result = GenerateEmbeddings(content, cancellationToken.ToRequestOptions());
return ClientResult.FromValue((OpenAIEmbeddingCollection)result, result.GetRawResponse());
}
// CUSTOM: Added to simplify passing the input as a collection of ReadOnlyMemory tokens instead of BinaryData.
/// <summary> Generates embeddings representing the text inputs. </summary>
/// <param name="inputs"> The text inputs to generate embeddings for. </param>
/// <param name="options"> The options to configure the embedding generation. </param>
/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
/// <exception cref="ArgumentNullException"> <paramref name="inputs"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="inputs"/> is an empty collection, and was expected to be non-empty. </exception>
public virtual async Task<ClientResult<OpenAIEmbeddingCollection>> GenerateEmbeddingsAsync(IEnumerable<ReadOnlyMemory<int>> inputs, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(inputs, nameof(inputs));
options ??= new();
CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs, SourceGenerationContext.Default.IEnumerableReadOnlyMemoryInt32), ref options);
using BinaryContent content = options;
ClientResult result = await GenerateEmbeddingsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
return ClientResult.FromValue((OpenAIEmbeddingCollection)result, result.GetRawResponse());
}
// CUSTOM: Added to simplify passing the input as a collection of ReadOnlyMemory of tokens instead of BinaryData.
/// <summary> Generates embeddings representing the text inputs. </summary>
/// <param name="inputs"> The text inputs to generate embeddings for. </param>
/// <param name="options"> The options to configure the embedding generation. </param>
/// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
/// <exception cref="ArgumentNullException"> <paramref name="inputs"/> is null. </exception>
/// <exception cref="ArgumentException"> <paramref name="inputs"/> is an empty collection, and was expected to be non-empty. </exception>
public virtual ClientResult<OpenAIEmbeddingCollection> GenerateEmbeddings(IEnumerable<ReadOnlyMemory<int>> inputs, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default)
{
Argument.AssertNotNullOrEmpty(inputs, nameof(inputs));
options ??= new();
CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs, SourceGenerationContext.Default.IEnumerableReadOnlyMemoryInt32), ref options);
using BinaryContent content = options;
ClientResult result = GenerateEmbeddings(content, cancellationToken.ToRequestOptions());
return ClientResult.FromValue((OpenAIEmbeddingCollection)result, result.GetRawResponse());
}
private void CreateEmbeddingGenerationOptions(BinaryData input, ref EmbeddingGenerationOptions options)
{
options.Input = input;
options.Model = _model;
options.EncodingFormat = InternalCreateEmbeddingRequestEncodingFormat.Base64;
}
}