From 9bf6471c9a0ac48e6abcc5630182aa8e55637485 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Wed, 28 Jun 2023 13:02:32 -0700 Subject: [PATCH] Add batch upload capability for CreateEmbeddingAsync. --- OpenAI_API/Embedding/EmbeddingEndpoint.cs | 23 +++++++--- OpenAI_API/Embedding/EmbeddingRequest.cs | 50 +++++++++++++++------- OpenAI_API/Embedding/IEmbeddingEndpoint.cs | 6 +++ 3 files changed, 57 insertions(+), 22 deletions(-) diff --git a/OpenAI_API/Embedding/EmbeddingEndpoint.cs b/OpenAI_API/Embedding/EmbeddingEndpoint.cs index a324de9..c0ea25b 100644 --- a/OpenAI_API/Embedding/EmbeddingEndpoint.cs +++ b/OpenAI_API/Embedding/EmbeddingEndpoint.cs @@ -35,12 +35,23 @@ public async Task CreateEmbeddingAsync(string input) return await CreateEmbeddingAsync(req); } - /// - /// Ask the API to embedd text using a custom request - /// - /// Request to be send - /// Asynchronously returns the embedding result. Look in its property of to find the vector of floating point numbers - public async Task CreateEmbeddingAsync(EmbeddingRequest request) + /// + /// Ask the API to embedd text using the default embedding model + /// + /// Text to be embedded + /// Asynchronously returns the embedding resu + public async Task CreateEmbeddingAsync(string[] batchInput) + { + EmbeddingRequest req = new EmbeddingRequest(DefaultEmbeddingRequestArgs.Model, batchInput); + return await CreateEmbeddingAsync(req); + } + + /// + /// Ask the API to embedd text using a custom request + /// + /// Request to be send + /// Asynchronously returns the embedding result. Look in its property of to find the vector of floating point numbers + public async Task CreateEmbeddingAsync(EmbeddingRequest request) { return await HttpPost(postData: request); } diff --git a/OpenAI_API/Embedding/EmbeddingRequest.cs b/OpenAI_API/Embedding/EmbeddingRequest.cs index 99780eb..16052fb 100644 --- a/OpenAI_API/Embedding/EmbeddingRequest.cs +++ b/OpenAI_API/Embedding/EmbeddingRequest.cs @@ -14,16 +14,16 @@ public class EmbeddingRequest [JsonProperty("model")] public string Model { get; set; } - /// - /// Main text to be embedded - /// - [JsonProperty("input")] - public string Input { get; set; } + /// + /// Main text to be embedded + /// + [JsonProperty("input")] + public object Input { get; set; } - /// - /// Cretes a new, empty - /// - public EmbeddingRequest() + /// + /// Cretes a new, empty + /// + public EmbeddingRequest() { } @@ -38,15 +38,33 @@ public EmbeddingRequest(Model model, string input) Model = model; this.Input = input; } - - /// - /// Creates a new with the specified input and the model. - /// - /// The prompt to transform - public EmbeddingRequest(string input) + /// + /// Creates a new with the specified parameters + /// + /// The model to use. You can use to see all of your available models, or use a standard model like . + /// The prompt to transform + public EmbeddingRequest(Model model, string[] batchInput) + { + Model = model; + Input = batchInput; + } + /// + /// Creates a new with the specified input and the model. + /// + /// The prompt to transform + public EmbeddingRequest(string input) { Model = OpenAI_API.Models.Model.AdaTextEmbedding; this.Input = input; } - } + /// + /// Creates a new with the specified input and the model. + /// + /// The prompt to transform + public EmbeddingRequest(string[] batchInput) + { + Model = OpenAI_API.Models.Model.AdaTextEmbedding; + Input = batchInput; + } + } } diff --git a/OpenAI_API/Embedding/IEmbeddingEndpoint.cs b/OpenAI_API/Embedding/IEmbeddingEndpoint.cs index acd9069..08c9d54 100644 --- a/OpenAI_API/Embedding/IEmbeddingEndpoint.cs +++ b/OpenAI_API/Embedding/IEmbeddingEndpoint.cs @@ -20,6 +20,12 @@ public interface IEmbeddingEndpoint /// Asynchronously returns the embedding result. Look in its property of to find the vector of floating point numbers Task CreateEmbeddingAsync(string input); + /// + /// Ask the API to embedd text using the default embedding model + /// + /// Text to be embedded + /// Asynchronously returns the embedding result. Look in its property of to find the vector of floating point numbers + Task CreateEmbeddingAsync(string[] batchInput); /// /// Ask the API to embedd text using a custom request ///