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

OpenAI-DotNet 7.6.3 #229

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add new OpenAI embedding models + dimensions parameter (#226)
This PR adds the new OpenAI embedding models, as announced here:

https://openai.com/blog/new-embedding-models-and-api-updates

Also added the new `dimensions` parameter described in the announcement.

- openai/openai-openapi#179

---------

Co-authored-by: Stephen Hodgson <[email protected]>
Mitch528 and StephenHodgson authored Jan 26, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 45967a2189285e7a4fd3a7336d0c7bfb6e78ebb7
14 changes: 13 additions & 1 deletion OpenAI-DotNet-Tests/TestFixture_06_Embeddings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NUnit.Framework;
using System.Threading.Tasks;
using OpenAI.Models;

namespace OpenAI.Tests
{
@@ -13,9 +14,20 @@ public async Task Test_1_CreateEmbedding()
Assert.IsNotNull(embedding);
Assert.IsNotEmpty(embedding.Data);
}

[Test]
public async Task Test_2_CreateEmbeddingWithDimensions()
{
Assert.IsNotNull(OpenAIClient.EmbeddingsEndpoint);
var embedding = await OpenAIClient.EmbeddingsEndpoint.CreateEmbeddingAsync("The food was delicious and the waiter...",
Model.Embedding_3_Small, dimensions: 512);
Assert.IsNotNull(embedding);
Assert.IsNotEmpty(embedding.Data);
Assert.AreEqual(512, embedding.Data[0].Embedding.Count);
}

[Test]
public async Task Test_2_CreateEmbeddingsWithMultipleInputs()
public async Task Test_3_CreateEmbeddingsWithMultipleInputs()
{
Assert.IsNotNull(OpenAIClient.EmbeddingsEndpoint);
var embeddings = new[]
16 changes: 12 additions & 4 deletions OpenAI-DotNet/Embeddings/EmbeddingsEndpoint.cs
Original file line number Diff line number Diff line change
@@ -35,9 +35,13 @@ public EmbeddingsEndpoint(OpenAIClient client) : base(client) { }
/// <param name="user">
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </param>
/// <param name="dimensions">
/// The number of dimensions the resulting output embeddings should have.
/// Only supported in text-embedding-3 and later models
/// </param>
/// <returns><see cref="EmbeddingsResponse"/></returns>
public async Task<EmbeddingsResponse> CreateEmbeddingAsync(string input, string model = null, string user = null)
=> await CreateEmbeddingAsync(new EmbeddingsRequest(input, model, user)).ConfigureAwait(false);
public async Task<EmbeddingsResponse> CreateEmbeddingAsync(string input, string model = null, string user = null, int? dimensions = null)
=> await CreateEmbeddingAsync(new EmbeddingsRequest(input, model, user, dimensions)).ConfigureAwait(false);

/// <summary>
/// Creates an embedding vector representing the input text.
@@ -54,10 +58,14 @@ public async Task<EmbeddingsResponse> CreateEmbeddingAsync(string input, string
/// <param name="user">
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </param>
/// <param name="dimensions">
/// The number of dimensions the resulting output embeddings should have.
/// Only supported in text-embedding-3 and later models
/// </param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="EmbeddingsResponse"/></returns>
public async Task<EmbeddingsResponse> CreateEmbeddingAsync(IEnumerable<string> input, string model = null, string user = null, CancellationToken cancellationToken = default)
=> await CreateEmbeddingAsync(new EmbeddingsRequest(input, model, user), cancellationToken).ConfigureAwait(false);
public async Task<EmbeddingsResponse> CreateEmbeddingAsync(IEnumerable<string> input, string model = null, string user = null, int? dimensions = null, CancellationToken cancellationToken = default)
=> await CreateEmbeddingAsync(new EmbeddingsRequest(input, model, user, dimensions), cancellationToken).ConfigureAwait(false);

/// <summary>
/// Creates an embedding vector representing the input text.
19 changes: 16 additions & 3 deletions OpenAI-DotNet/Embeddings/EmbeddingsRequest.cs
Original file line number Diff line number Diff line change
@@ -25,8 +25,12 @@ public sealed class EmbeddingsRequest
/// <param name="user">
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </param>
public EmbeddingsRequest(string input, string model = null, string user = null)
: this(new List<string> { input }, model, user)
/// <param name="dimensions">
/// The number of dimensions the resulting output embeddings should have.
/// Only supported in text-embedding-3 and later models
/// </param>
public EmbeddingsRequest(string input, string model = null, string user = null, int? dimensions = null)
: this(new List<string> { input }, model, user, dimensions)
{
if (string.IsNullOrWhiteSpace(input))
{
@@ -49,7 +53,12 @@ public EmbeddingsRequest(string input, string model = null, string user = null)
/// <param name="user">
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </param>
public EmbeddingsRequest(IEnumerable<string> input, string model = null, string user = null)
/// <param name="dimensions">
/// The number of dimensions the resulting output embeddings should have.
/// Only supported in text-embedding-3 and later models
/// </param>
public EmbeddingsRequest(IEnumerable<string> input, string model = null, string user = null,
int? dimensions = null)
{
Input = input?.ToList();

@@ -60,13 +69,17 @@ public EmbeddingsRequest(IEnumerable<string> input, string model = null, string

Model = string.IsNullOrWhiteSpace(model) ? Models.Model.Embedding_Ada_002 : model;
User = user;
Dimensions = dimensions;
}

[JsonPropertyName("input")]
public IReadOnlyList<string> Input { get; }

[JsonPropertyName("model")]
public string Model { get; }

[JsonPropertyName("dimensions")]
public int? Dimensions { get; }

[JsonPropertyName("user")]
public string User { get; }
10 changes: 10 additions & 0 deletions OpenAI-DotNet/Models/Model.cs
Original file line number Diff line number Diff line change
@@ -130,6 +130,16 @@ public Model(string id, string ownedBy = null)
/// The default model for <see cref="Embeddings.EmbeddingsEndpoint"/>.
/// </summary>
public static Model Embedding_Ada_002 { get; } = new("text-embedding-ada-002", "openai");

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

/// <summary>
/// A next generation larger model with embeddings of up to 3072 dimensions.
/// </summary>
public static Model Embedding_3_Large { get; } = new("text-embedding-3-large", "openai");

/// <summary>
/// The default model for <see cref="Audio.AudioEndpoint"/>.