openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.12

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Custom/Embeddings/EmbeddingClient.cs

222lines · modecode

1using System;
2using System.ClientModel;
3using System.ClientModel.Primitives;
4using System.Collections.Generic;
5using System.Linq;
6using System.Threading;
7using System.Threading.Tasks;
8
9namespace OpenAI.Embeddings;
10
11// CUSTOM:
12// - Renamed.
13// - Suppressed constructor that takes endpoint parameter; endpoint is now a property in the options class.
14// - Suppressed methods that only take the options parameter.
15/// <summary> The service client for OpenAI embedding operations. </summary>
16[CodeGenClient("Embeddings")]
17[CodeGenSuppress("EmbeddingClient", typeof(ClientPipeline), typeof(ApiKeyCredential), typeof(Uri))]
18[CodeGenSuppress("CreateEmbeddingAsync", typeof(EmbeddingGenerationOptions))]
19[CodeGenSuppress("CreateEmbedding", typeof(EmbeddingGenerationOptions))]
20public partial class EmbeddingClient
21{
22 private readonly string _model;
23
24 // CUSTOM: Added as a convenience.
25 /// <summary> Initializes a new instance of <see cref="EmbeddingClient">. </summary>
26 /// <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>
27 /// <param name="apiKey"> The API key to authenticate with the service. </param>
28 /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="apiKey"/> is null. </exception>
29 /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
30 public EmbeddingClient(string model, string apiKey) : this(model, new ApiKeyCredential(apiKey), new OpenAIClientOptions())
31 {
32 }
33
34 // CUSTOM: Added as a convenience.
35 /// <summary> Initializes a new instance of <see cref="EmbeddingClient">. </summary>
36 /// <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>
37 /// <param name="apiKey"> The API key to authenticate with the service. </param>
38 /// <param name="options"> The options to configure the client. </param>
39 /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="apiKey"/> is null. </exception>
40 /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
41 public EmbeddingClient(string model, string apiKey, OpenAIClientOptions options) : this(model, new ApiKeyCredential(apiKey), options)
42 {
43 }
44
45 // CUSTOM:
46 // - Added `model` parameter.
47 // - Used a custom pipeline.
48 // - Demoted the endpoint parameter to be a property in the options class.
49 /// <summary> Initializes a new instance of <see cref="EmbeddingClient">. </summary>
50 /// <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>
51 /// <param name="credential"> The API key to authenticate with the service. </param>
52 /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception>
53 /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
54 public EmbeddingClient(string model, ApiKeyCredential credential) : this(model, credential, new OpenAIClientOptions())
55 {
56 }
57
58 // CUSTOM:
59 // - Added `model` parameter.
60 // - Used a custom pipeline.
61 // - Demoted the endpoint parameter to be a property in the options class.
62 /// <summary> Initializes a new instance of <see cref="EmbeddingClient">. </summary>
63 /// <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>
64 /// <param name="credential"> The API key to authenticate with the service. </param>
65 /// <param name="options"> The options to configure the client. </param>
66 /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception>
67 /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
68 public EmbeddingClient(string model, ApiKeyCredential credential, OpenAIClientOptions options)
69 {
70 Argument.AssertNotNullOrEmpty(model, nameof(model));
71 Argument.AssertNotNull(credential, nameof(credential));
72 options ??= new OpenAIClientOptions();
73
74 _model = model;
75 _pipeline = OpenAIClient.CreatePipeline(credential, options);
76 _endpoint = OpenAIClient.GetEndpoint(options);
77 }
78
79 // CUSTOM:
80 // - Added `model` parameter.
81 // - Used a custom pipeline.
82 // - Demoted the endpoint parameter to be a property in the options class.
83 // - Made protected.
84 /// <summary> Initializes a new instance of <see cref="EmbeddingClient">. </summary>
85 /// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param>
86 /// <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>
87 /// <param name="options"> The options to configure the client. </param>
88 /// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> or <paramref name="model"/> is null. </exception>
89 /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception>
90 protected internal EmbeddingClient(ClientPipeline pipeline, string model, OpenAIClientOptions options)
91 {
92 Argument.AssertNotNull(pipeline, nameof(pipeline));
93 Argument.AssertNotNullOrEmpty(model, nameof(model));
94 options ??= new OpenAIClientOptions();
95
96 _model = model;
97 _pipeline = pipeline;
98 _endpoint = OpenAIClient.GetEndpoint(options);
99 }
100
101 // CUSTOM: Added to simplify generating a single embedding from a string input.
102 /// <summary> Generates an embedding representing the text input. </summary>
103 /// <param name="input"> The text input to generate an embedding for. </param>
104 /// <param name="options"> The options to configure the embedding generation. </param>
105 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
106 /// <exception cref="ArgumentNullException"> <paramref name="input"/> is null. </exception>
107 /// <exception cref="ArgumentException"> <paramref name="input"/> is an empty string, and was expected to be non-empty. </exception>
108 public virtual async Task<ClientResult<Embedding>> GenerateEmbeddingAsync(string input, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default)
109 {
110 Argument.AssertNotNullOrEmpty(input, nameof(input));
111
112 options ??= new();
113 CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(input), ref options);
114
115 using BinaryContent content = options.ToBinaryContent();
116 ClientResult result = await GenerateEmbeddingsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
117 return ClientResult.FromValue(EmbeddingCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse());
118 }
119
120 // CUSTOM: Added to simplify generating a single embedding from a string input.
121 /// <summary> Generates an embedding representing the text input. </summary>
122 /// <param name="input"> The text input to generate an embedding for. </param>
123 /// <param name="options"> The options to configure the embedding generation. </param>
124 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
125 /// <exception cref="ArgumentNullException"> <paramref name="input"/> is null. </exception>
126 /// <exception cref="ArgumentException"> <paramref name="input"/> is an empty string, and was expected to be non-empty. </exception>
127 public virtual ClientResult<Embedding> GenerateEmbedding(string input, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default)
128 {
129 Argument.AssertNotNullOrEmpty(input, nameof(input));
130
131 options ??= new();
132 CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(input), ref options);
133
134 using BinaryContent content = options.ToBinaryContent();
135 ClientResult result = GenerateEmbeddings(content, cancellationToken.ToRequestOptions());
136 return ClientResult.FromValue(EmbeddingCollection.FromResponse(result.GetRawResponse()).FirstOrDefault(), result.GetRawResponse());
137 }
138
139 // CUSTOM: Added to simplify passing the input as a collection of strings instead of BinaryData.
140 /// <summary> Generates embeddings representing the text inputs. </summary>
141 /// <param name="inputs"> The text inputs to generate embeddings for. </param>
142 /// <param name="options"> The options to configure the embedding generation. </param>
143 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
144 /// <exception cref="ArgumentNullException"> <paramref name="inputs"/> is null. </exception>
145 /// <exception cref="ArgumentException"> <paramref name="inputs"/> is an empty collection, and was expected to be non-empty. </exception>
146 public virtual async Task<ClientResult<EmbeddingCollection>> GenerateEmbeddingsAsync(IEnumerable<string> inputs, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default)
147 {
148 Argument.AssertNotNullOrEmpty(inputs, nameof(inputs));
149
150 options ??= new();
151 CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs), ref options);
152
153 using BinaryContent content = options.ToBinaryContent();
154 ClientResult result = await GenerateEmbeddingsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
155 return ClientResult.FromValue(EmbeddingCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
156
157 }
158
159 // CUSTOM: Added to simplify passing the input as a collection of strings instead of BinaryData.
160 /// <summary> Generates embeddings representing the text inputs. </summary>
161 /// <param name="inputs"> The text inputs to generate embeddings for. </param>
162 /// <param name="options"> The options to configure the embedding generation. </param>
163 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
164 /// <exception cref="ArgumentNullException"> <paramref name="inputs"/> is null. </exception>
165 /// <exception cref="ArgumentException"> <paramref name="inputs"/> is an empty collection, and was expected to be non-empty. </exception>
166 public virtual ClientResult<EmbeddingCollection> GenerateEmbeddings(IEnumerable<string> inputs, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default)
167 {
168 Argument.AssertNotNullOrEmpty(inputs, nameof(inputs));
169
170 options ??= new();
171 CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs), ref options);
172
173 using BinaryContent content = options.ToBinaryContent();
174 ClientResult result = GenerateEmbeddings(content, cancellationToken.ToRequestOptions());
175 return ClientResult.FromValue(EmbeddingCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
176 }
177
178 // CUSTOM: Added to simplify passing the input as a collection of a collection of tokens instead of BinaryData.
179 /// <summary> Generates embeddings representing the text inputs. </summary>
180 /// <param name="inputs"> The text inputs to generate embeddings for. </param>
181 /// <param name="options"> The options to configure the embedding generation. </param>
182 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
183 /// <exception cref="ArgumentNullException"> <paramref name="inputs"/> is null. </exception>
184 /// <exception cref="ArgumentException"> <paramref name="inputs"/> is an empty collection, and was expected to be non-empty. </exception>
185 public virtual async Task<ClientResult<EmbeddingCollection>> GenerateEmbeddingsAsync(IEnumerable<IEnumerable<int>> inputs, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default)
186 {
187 Argument.AssertNotNullOrEmpty(inputs, nameof(inputs));
188
189 options ??= new();
190 CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs), ref options);
191
192 using BinaryContent content = options.ToBinaryContent();
193 ClientResult result = await GenerateEmbeddingsAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false);
194 return ClientResult.FromValue(EmbeddingCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
195 }
196
197 // CUSTOM: Added to simplify passing the input as a collection of a collection of tokens instead of BinaryData.
198 /// <summary> Generates embeddings representing the text inputs. </summary>
199 /// <param name="inputs"> The text inputs to generate embeddings for. </param>
200 /// <param name="options"> The options to configure the embedding generation. </param>
201 /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param>
202 /// <exception cref="ArgumentNullException"> <paramref name="inputs"/> is null. </exception>
203 /// <exception cref="ArgumentException"> <paramref name="inputs"/> is an empty collection, and was expected to be non-empty. </exception>
204 public virtual ClientResult<EmbeddingCollection> GenerateEmbeddings(IEnumerable<IEnumerable<int>> inputs, EmbeddingGenerationOptions options = null, CancellationToken cancellationToken = default)
205 {
206 Argument.AssertNotNullOrEmpty(inputs, nameof(inputs));
207
208 options ??= new();
209 CreateEmbeddingGenerationOptions(BinaryData.FromObjectAsJson(inputs), ref options);
210
211 using BinaryContent content = options.ToBinaryContent();
212 ClientResult result = GenerateEmbeddings(content, cancellationToken.ToRequestOptions());
213 return ClientResult.FromValue(EmbeddingCollection.FromResponse(result.GetRawResponse()), result.GetRawResponse());
214 }
215
216 private void CreateEmbeddingGenerationOptions(BinaryData input, ref EmbeddingGenerationOptions options)
217 {
218 options.Input = input;
219 options.Model = _model;
220 options.EncodingFormat = InternalCreateEmbeddingRequestEncodingFormat.Base64;
221 }
222}
223