Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 2 new embedding models to Model.cs #193

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions OpenAI_API/Embedding/EmbeddingEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ internal EmbeddingEndpoint(OpenAIAPI api) : base(api) { }
/// Ask the API to embedd text using the default embedding model <see cref="Model.AdaTextEmbedding"/>
/// </summary>
/// <param name="input">Text to be embedded</param>
/// <param name="model">Embeddings model to be used</param>
/// <returns>Asynchronously returns the embedding result. Look in its <see cref="Data.Embedding"/> property of <see cref="EmbeddingResult.Data"/> to find the vector of floating point numbers</returns>
public async Task<EmbeddingResult> CreateEmbeddingAsync(string input)
public async Task<EmbeddingResult> CreateEmbeddingAsync(string input, Model model = null)
{
EmbeddingRequest req = new EmbeddingRequest(DefaultEmbeddingRequestArgs.Model, input);
EmbeddingRequest req = new EmbeddingRequest(model ?? DefaultEmbeddingRequestArgs.Model, input);
return await CreateEmbeddingAsync(req);
}

Expand All @@ -46,14 +47,25 @@ public async Task<EmbeddingResult> CreateEmbeddingAsync(EmbeddingRequest request
}

/// <summary>
/// Ask the API to embedd text using the default embedding model <see cref="Model.AdaTextEmbedding"/>
/// Ask the API to embedd text using the default embedding model <see cref="Model.AdaTextEmbedding"/> in case no other model is specified
/// </summary>
/// <param name="input">Text to be embedded</param>
/// <param name="model">Embeddings model to be used</param>
/// <returns>Asynchronously returns the first embedding result as an array of floats.</returns>
public async Task<float[]> GetEmbeddingsAsync(string input, Model model = null)
{
EmbeddingRequest req = new EmbeddingRequest(model ?? DefaultEmbeddingRequestArgs.Model, input);
return await GetEmbeddingsAsync(req);
}

/// <summary>
/// Ask the API to embedd text
/// </summary>
/// <param name="request">Request to be send</param>
/// <returns>Asynchronously returns the first embedding result as an array of floats.</returns>
public async Task<float[]> GetEmbeddingsAsync(string input)
public async Task<float[]> GetEmbeddingsAsync(EmbeddingRequest request)
{
EmbeddingRequest req = new EmbeddingRequest(DefaultEmbeddingRequestArgs.Model, input);
var embeddingResult = await CreateEmbeddingAsync(req);
var embeddingResult = await CreateEmbeddingAsync(request);
return embeddingResult?.Data?[0]?.Embedding;
}
}
Expand Down
12 changes: 12 additions & 0 deletions OpenAI_API/Embedding/EmbeddingRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ public class EmbeddingRequest
[JsonProperty("input")]
public string Input { get; set; }

/// <summary>
/// The number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models.
/// </summary>
[JsonProperty("dimensions")]
public int? Dimensions { get; set; }

/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </summary>
[JsonProperty("user")]
public string User { get; set; }

/// <summary>
/// Cretes a new, empty <see cref="EmbeddingRequest"/>
/// </summary>
Expand Down
15 changes: 12 additions & 3 deletions OpenAI_API/Embedding/IEmbeddingEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ public interface IEmbeddingEndpoint
/// Ask the API to embedd text using the default embedding model <see cref="Model.AdaTextEmbedding"/>
/// </summary>
/// <param name="input">Text to be embedded</param>
/// <param name="model">Embeddings model to be used</param>
/// <returns>Asynchronously returns the embedding result. Look in its <see cref="Data.Embedding"/> property of <see cref="EmbeddingResult.Data"/> to find the vector of floating point numbers</returns>
Task<EmbeddingResult> CreateEmbeddingAsync(string input);
Task<EmbeddingResult> CreateEmbeddingAsync(string input, Model model = null);

/// <summary>
/// Ask the API to embedd text using a custom request
Expand All @@ -28,10 +29,18 @@ public interface IEmbeddingEndpoint
Task<EmbeddingResult> CreateEmbeddingAsync(EmbeddingRequest request);

/// <summary>
/// Ask the API to embedd text using the default embedding model <see cref="Model.AdaTextEmbedding"/>
/// Ask the API to embedd text using the default embedding model <see cref="Model.AdaTextEmbedding"/> in case no other model is specified
/// </summary>
/// <param name="input">Text to be embedded</param>
/// <param name="model">Embeddings model to be used</param>
/// <returns>Asynchronously returns the first embedding result as an array of floats.</returns>
Task<float[]> GetEmbeddingsAsync(string input, Model model = null);

/// <summary>
/// Ask the API to embedd text using the default embedding model <see cref="Model.AdaTextEmbedding"/> in case no other model is specified
/// </summary>
/// <param name="request">Request to be send</param>
/// <returns>Asynchronously returns the first embedding result as an array of floats.</returns>
Task<float[]> GetEmbeddingsAsync(string input);
Task<float[]> GetEmbeddingsAsync(EmbeddingRequest request);
}
}
12 changes: 11 additions & 1 deletion OpenAI_API/Model/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,19 @@ public async Task<Model> RetrieveModelDetailsAsync(OpenAI_API.OpenAIAPI api)

#region Embeddings
/// <summary>
/// OpenAI offers one second-generation embedding model for use with the embeddings API endpoint.
/// This model is not deprecated yet, but OpenAI recommends to use the newer models text-embedding-3-small and text-embedding-3-large.
/// </summary>
public static Model AdaTextEmbedding => new Model("text-embedding-ada-002") { OwnedBy = "openai" };

/// <summary>
/// Highly efficient embedding model which provides a significant upgrade over its predecessor, the text-embedding-ada-002 model
/// </summary>
public static Model SmallTextEmbedding => new Model("text-embedding-3-small") { OwnedBy = "openai" };

/// <summary>
/// Next generation larger embedding model which creates embeddings with up to 3072 dimensions
/// </summary>
public static Model LargeTextEmbedding => new Model("text-embedding-3-large") { OwnedBy = "openai" };
#endregion

#region Moderation
Expand Down
14 changes: 14 additions & 0 deletions OpenAI_Tests/EmbeddingEndpointTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,19 @@ public void GetSimpleEmbedding()
Assert.IsNotNull(results);
Assert.That(results.Length == 1536);
}

[Test]
public void GetSimpleEmbeddingWithDimensions()
{
var api = new OpenAI_API.OpenAIAPI();

Assert.IsNotNull(api.Embeddings);

var request = new EmbeddingRequest(Model.SmallTextEmbedding, "A test text for embedding");
request.Dimensions = 256;
var results = api.Embeddings.GetEmbeddingsAsync(request).Result;
Assert.IsNotNull(results);
Assert.That(results.Length == 256);
}
}
}