Skip to content

Commit

Permalink
Add Dimensions parameter for embedding requests #143
Browse files Browse the repository at this point in the history
  • Loading branch information
marcominerva committed Jan 29, 2024
1 parent e154db2 commit 702bb23
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 23 deletions.
2 changes: 1 addition & 1 deletion samples/ChatGptApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
})
.WithOpenApi();

app.MapPost("/api/embeddings/CosineSimilarity", async (CosineSimilarityRequest request, IChatGptClient chatGptClient) =>
app.MapPost("/api/embeddings/cosine-similarity", async (CosineSimilarityRequest request, IChatGptClient chatGptClient) =>
{
var firstEmbeddingResponse = await chatGptClient.GenerateEmbeddingAsync(request.FirstMessage);
var secondEmbeddingResponse = await chatGptClient.GenerateEmbeddingAsync(request.SecondMessage);
Expand Down
11 changes: 7 additions & 4 deletions samples/ChatGptApi/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"ChatGPT": {
"Provider": "OpenAI", // Optional. Allowed values: OpenAI (default) or Azure
"Provider": "OpenaI", // Optional. Allowed values: OpenAI (default) or Azure
"ApiKey": "", // Required
//"Organization": "", // Optional, used only by OpenAI
//"Organization": "", // Optional, used only by OpenAI
"ResourceName": "", // Required when using Azure OpenAI Service
"ApiVersion": "2023-12-01-preview", // Optional, used only by Azure OpenAI Service (default: 2023-12-01-preview)
"ApiVersion": "2023-12-01-preview", // Optional, used only by Azure OpenAI Service (default: 2023-08-01-preview)
"AuthenticationType": "ApiKey", // Optional, used only by Azure OpenAI Service. Allowed values: ApiKey (default) or ActiveDirectory

"DefaultModel": "my-model",
"DefaultEmbeddingModel": "text-embedding-ada-002", // Optional, set it if you want to use embeddings
"MessageLimit": 20,
"MessageExpiration": "00:30:00",
"ThrowExceptionOnError": true, // Optional, default: true
"ThrowExceptionOnError": true // Optional, default: true
//"User": "UserName",
//"DefaultParameters": {
// "Temperature": 0.8,
Expand All @@ -21,6 +21,9 @@
// "FrequencyPenalty": 0,
// "ResponseFormat": { "Type": "text" }, // Allowed values for Type: text (default) or json_object
// "Seed": 42 // Optional (any integer value)
//},
//"DefaultEmbeddingParameters": {
// "Dimensions": 1536
//}
},
"Logging": {
Expand Down
5 changes: 4 additions & 1 deletion samples/ChatGptConsole/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"DefaultEmbeddingModel": "text-embedding-ada-002", // Optional, it set if you want to use embeddings
"MessageLimit": 20,
"MessageExpiration": "00:30:00",
"ThrowExceptionOnError": true,
"ThrowExceptionOnError": true
//"User": "UserName",
//"DefaultParameters": {
// "Temperature": 0.8,
Expand All @@ -21,6 +21,9 @@
// "FrequencyPenalty": 0,
// "ResponseFormat": { "Type": "text" }, // Allowed values for Type: text (default) or json_object
// "Seed": 42 // Optional (any integer value)
//},
//"DefaultEmbeddingParameters": {
// "Dimensions": 1536
//}
},
"Logging": {
Expand Down
5 changes: 4 additions & 1 deletion samples/ChatGptFunctionCallingConsole/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"DefaultEmbeddingModel": "text-embedding-ada-002", // Optional, it set if you want to use embeddings
"MessageLimit": 20,
"MessageExpiration": "00:30:00",
"ThrowExceptionOnError": true, // Optional, default: true
"ThrowExceptionOnError": true // Optional, default: true
//"User": "UserName",
//"DefaultParameters": {
// "Temperature": 0.8,
Expand All @@ -21,6 +21,9 @@
// "FrequencyPenalty": 0,
// "ResponseFormat": { "Type": "text" }, // Allowed values for Type: text (default) or json_object
// "Seed": 42 // Optional (any integer value)
//},
//"DefaultEmbeddingParameters": {
// "Dimensions": 1536
//}
},
"Logging": {
Expand Down
5 changes: 4 additions & 1 deletion samples/ChatGptStreamConsole/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"DefaultEmbeddingModel": "text-embedding-ada-002", // Optional, set it if you want to use embeddings
"MessageLimit": 20,
"MessageExpiration": "00:30:00",
"ThrowExceptionOnError": true, // Optional, default: true
"ThrowExceptionOnError": true // Optional, default: true
//"User": "UserName",
//"DefaultParameters": {
// "Temperature": 0.8,
Expand All @@ -21,6 +21,9 @@
// "FrequencyPenalty": 0,
// "ResponseFormat": { "Type": "text" }, // Allowed values for Type: text (default) or json_object
// "Seed": 42 // Optional (any integer value)
//},
//"DefaultEmbeddingParameters": {
// "Dimensions": 1536
//}
},
"Logging": {
Expand Down
11 changes: 6 additions & 5 deletions src/ChatGptNet/ChatGptClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ public async Task AddToolResponseAsync(Guid conversationId, string? toolId, stri
await UpdateCacheAsync(conversationId, messages, cancellationToken);
}

public async Task<EmbeddingResponse> GenerateEmbeddingAsync(IEnumerable<string> messages, string? model = null, CancellationToken cancellationToken = default)
public async Task<EmbeddingResponse> GenerateEmbeddingAsync(IEnumerable<string> texts, EmbeddingParameters? embeddingParameters = null, string? model = null, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(messages);
ArgumentNullException.ThrowIfNull(texts);

var request = CreateEmbeddingRequest(messages, model);
var request = CreateEmbeddingRequest(texts, embeddingParameters, model);

var requestUri = options.ServiceConfiguration.GetEmbeddingEndpoint(model ?? options.DefaultEmbeddingModel);
using var httpResponse = await httpClient.PostAsJsonAsync(requestUri, request, jsonSerializerOptions, cancellationToken);
Expand Down Expand Up @@ -360,11 +360,12 @@ private ChatGptRequest CreateChatGptRequest(IEnumerable<ChatGptMessage> messages
ResponseFormat = parameters?.ResponseFormat ?? options.DefaultParameters.ResponseFormat
};

private EmbeddingRequest CreateEmbeddingRequest(IEnumerable<string> messages, string? model = null)
private EmbeddingRequest CreateEmbeddingRequest(IEnumerable<string> messages, EmbeddingParameters? parameters, string? model)
=> new()
{
Model = model ?? options.DefaultEmbeddingModel,
Input = messages
Input = messages,
Dimensions = parameters?.Dimensions ?? options.DefaultEmbeddingParameters.Dimensions,
};

private async Task AddAssistantResponseAsync(Guid conversationId, IList<ChatGptMessage> messages, ChatGptMessage? message, CancellationToken cancellationToken = default)
Expand Down
6 changes: 6 additions & 0 deletions src/ChatGptNet/ChatGptOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public class ChatGptOptions
/// <see cref="ChatGptParameters"/>
public ChatGptParameters DefaultParameters { get; internal set; } = new();

/// <summary>
/// Gets or sets the default parameters for embeddings.
/// </summary>
/// <see cref="EmbeddingParameters"/>
public EmbeddingParameters DefaultEmbeddingParameters { get; internal set; } = new();

/// <summary>
/// Gets or sets the user identification for chat completion, which can help OpenAI to monitor and detect abuse.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion src/ChatGptNet/ChatGptOptionsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class ChatGptOptionsBuilder
public string? DefaultModel { get; set; }

/// <summary>
/// Gets or sets the default model for embedding. (default: <see cref="OpenAIEmbeddingModels.TextEmbeddingAda002"/> when the provider is <see cref="OpenAIChatGptServiceConfiguration"> OpenAI</see>).
/// Gets or sets the default model for embeddings. (default: <see cref="OpenAIEmbeddingModels.TextEmbeddingAda002"/> when the provider is <see cref="OpenAIChatGptServiceConfiguration"> OpenAI</see>).
/// </summary>
/// <seealso cref="OpenAIEmbeddingModels"/>
/// <seealso cref="OpenAIChatGptServiceConfiguration"/>
Expand All @@ -55,6 +55,12 @@ public class ChatGptOptionsBuilder
/// <see cref="ChatGptParameters"/>
public ChatGptParameters? DefaultParameters { get; set; } = new();

/// <summary>
/// Gets or sets the default parameters for embeddings.
/// </summary>
/// <see cref="EmbeddingParameters"/>
public EmbeddingParameters DefaultEmbeddingParameters { get; internal set; } = new();

/// <summary>
/// Gets or sets the user identification for chat completion, which can help OpenAI to monitor and detect abuse.
/// </summary>
Expand All @@ -70,6 +76,7 @@ internal ChatGptOptions Build()
DefaultModel = DefaultModel,
DefaultEmbeddingModel = DefaultEmbeddingModel,
DefaultParameters = DefaultParameters ?? new(),
DefaultEmbeddingParameters = DefaultEmbeddingParameters ?? new(),
MessageExpiration = MessageExpiration,
ThrowExceptionOnError = ThrowExceptionOnError,
ServiceConfiguration = ServiceConfiguration,
Expand Down
4 changes: 2 additions & 2 deletions src/ChatGptNet/Extensions/EmbeddingUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static float CosineSimilarity(ReadOnlySpan<float> x, ReadOnlySpan<float>
/// <param name="embeddingResponse">The embedding response.</param>
/// <param name="y">The other vector.</param>
/// <returns>The cosine similarity.</returns>
/// <seealso cref="IChatGptClient.GenerateEmbeddingAsync(string, string?, CancellationToken)"/>
/// <seealso cref="IChatGptClient.GenerateEmbeddingAsync(string, EmbeddingParameters?, string?, CancellationToken)"/>
/// <seealso cref="EmbeddingResponse"/>
public static float CosineSimilarity(this EmbeddingResponse embeddingResponse, ReadOnlySpan<float> y)
=> CosineSimilarity(embeddingResponse.GetEmbedding() ?? [], y);
Expand All @@ -44,7 +44,7 @@ public static float CosineSimilarity(this EmbeddingResponse embeddingResponse, R
/// <param name="embeddingResponse">The first embedding response.</param>
/// <param name="otherResponse">The second embedding response.</param>
/// <returns>The cosine similarity.</returns>
/// <seealso cref="IChatGptClient.GenerateEmbeddingAsync(string, string?, CancellationToken)"/>
/// <seealso cref="IChatGptClient.GenerateEmbeddingAsync(string, EmbeddingParameters?, string?, CancellationToken)"/>
/// <seealso cref="EmbeddingResponse"/>
public static float CosineSimilarity(this EmbeddingResponse embeddingResponse, EmbeddingResponse otherResponse)
=> CosineSimilarity(embeddingResponse.GetEmbedding() ?? [], otherResponse.GetEmbedding() ?? []);
Expand Down
16 changes: 9 additions & 7 deletions src/ChatGptNet/IChatGptClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,23 +289,25 @@ Task AddToolResponseAsync(Guid conversationId, ChatGptToolCall tool, string cont
Task AddToolResponseAsync(Guid conversationId, string? toolId, string name, string content, CancellationToken cancellationToken = default);

/// <summary>
/// Generates embeddings for a message.
/// Generates embeddings for a text.
/// </summary>
/// <param name="message">The message to use for generating embeddings.</param>
/// <param name="text">The text to use for generating embeddings.</param>
/// <param name="parameters">An <see cref="EmbeddingParameters"/> object used to override the default embedding parameters in the <see cref="ChatGptOptions.DefaultEmbeddingParameters"/> property.</param>
/// <param name="model">The name of the embedding model. If <paramref name="model"/> is <see langword="null"/>, then the one specified in the <see cref="ChatGptOptions.DefaultEmbeddingModel"/> property will be used.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>The embeddings for the provided message.</returns>
/// <exception cref="EmbeddingException">An error occurred while calling the API and the <see cref="ChatGptOptions.ThrowExceptionOnError"/> is <see langword="true"/>.</exception>
Task<EmbeddingResponse> GenerateEmbeddingAsync(string message, string? model = null, CancellationToken cancellationToken = default)
=> GenerateEmbeddingAsync([message], model, cancellationToken);
Task<EmbeddingResponse> GenerateEmbeddingAsync(string text, EmbeddingParameters? parameters = null, string? model = null, CancellationToken cancellationToken = default)
=> GenerateEmbeddingAsync([text], parameters, model, cancellationToken);

/// <summary>
/// Generates embeddings for a list of messages.
/// Generates embeddings for a list of texts.
/// </summary>
/// <param name="messages">The messages to use for generating embeddings.</param>
/// <param name="texts">The texts to use for generating embeddings.</param>
/// <param name="parameters">An <see cref="EmbeddingParameters"/> object used to override the default embedding parameters in the <see cref="ChatGptOptions.DefaultEmbeddingParameters"/> property.</param>
/// <param name="model">The name of the embedding model. If <paramref name="model"/> is <see langword="null"/>, then the one specified in the <see cref="ChatGptOptions.DefaultEmbeddingModel"/> property will be used.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <returns>The embeddings for the provided messages.</returns>
/// <exception cref="EmbeddingException">An error occurred while calling the API and the <see cref="ChatGptOptions.ThrowExceptionOnError"/> is <see langword="true"/>.</exception>
Task<EmbeddingResponse> GenerateEmbeddingAsync(IEnumerable<string> messages, string? model = null, CancellationToken cancellationToken = default);
Task<EmbeddingResponse> GenerateEmbeddingAsync(IEnumerable<string> texts, EmbeddingParameters? parameters = null, string? model = null, CancellationToken cancellationToken = default);
}
15 changes: 15 additions & 0 deletions src/ChatGptNet/Models/Embeddings/EmbeddingParameters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace ChatGptNet.Models.Embeddings;

/// <summary>
/// Represents embeddings parameters.
/// </summary>
/// <remarks>
/// See <see href="https://platform.openai.com/docs/api-reference/embeddings/create">Create embeddings</see> for more information.
/// </remarks>
public class EmbeddingParameters
{
/// <summary>
/// The number of dimensions the resulting output embeddings should have. Only supported in <c>text-embedding-3</c> and later models.
/// </summary>
public int? Dimensions { get; set; }
}
5 changes: 5 additions & 0 deletions src/ChatGptNet/Models/Embeddings/EmbeddingRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ internal class EmbeddingRequest
/// <seealso cref="Input"/>
public IEnumerable<string> Input { get; set; } = [];

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

/// <summary>
/// Gets or sets the user identification for embedding request, which can help to monitor and detect abuse.
/// </summary>
Expand Down

0 comments on commit 702bb23

Please sign in to comment.