diff --git a/Directory.Packages.props b/Directory.Packages.props index d34f41597..18d630813 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -53,9 +53,9 @@ - - - + + + diff --git a/extensions/AzureOpenAI/AzureOpenAIConfig.cs b/extensions/AzureOpenAI/AzureOpenAIConfig.cs index d54a182d8..92b6468bf 100644 --- a/extensions/AzureOpenAI/AzureOpenAIConfig.cs +++ b/extensions/AzureOpenAI/AzureOpenAIConfig.cs @@ -67,6 +67,13 @@ public enum APITypes /// public int MaxRetries { get; set; } = 10; + /// + /// The number of dimensions output embeddings should have. + /// Only supported in "text-embedding-3" and later models developed with + /// MRL, see https://arxiv.org/abs/2205.13147 + /// + public int? EmbeddingDimensions { get; set; } + /// /// Set credentials manually from code /// @@ -120,5 +127,10 @@ public void Validate() { throw new ConfigurationException($"Azure OpenAI: {nameof(this.MaxTokenTotal)} cannot be less than 1"); } + + if (this.EmbeddingDimensions is < 1) + { + throw new ConfigurationException($"Azure OpenAI: {nameof(this.EmbeddingDimensions)} cannot be less than 1"); + } } } diff --git a/extensions/AzureOpenAI/AzureOpenAITextEmbeddingGenerator.cs b/extensions/AzureOpenAI/AzureOpenAITextEmbeddingGenerator.cs index 9056bf5a2..359827eed 100644 --- a/extensions/AzureOpenAI/AzureOpenAITextEmbeddingGenerator.cs +++ b/extensions/AzureOpenAI/AzureOpenAITextEmbeddingGenerator.cs @@ -58,7 +58,8 @@ public AzureOpenAITextEmbeddingGenerator( modelId: config.Deployment, endpoint: config.Endpoint, credential: new DefaultAzureCredential(), - httpClient: httpClient); + httpClient: httpClient, + dimensions: config.EmbeddingDimensions); break; case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: @@ -67,7 +68,8 @@ public AzureOpenAITextEmbeddingGenerator( modelId: config.Deployment, endpoint: config.Endpoint, credential: config.GetTokenCredential(), - httpClient: httpClient); + httpClient: httpClient, + dimensions: config.EmbeddingDimensions); break; case AzureOpenAIConfig.AuthTypes.APIKey: @@ -76,7 +78,8 @@ public AzureOpenAITextEmbeddingGenerator( modelId: config.Deployment, endpoint: config.Endpoint, apiKey: config.APIKey, - httpClient: httpClient); + httpClient: httpClient, + dimensions: config.EmbeddingDimensions); break; default: diff --git a/extensions/OpenAI/OpenAIConfig.cs b/extensions/OpenAI/OpenAIConfig.cs index 347e75fe3..4a15b6ebf 100644 --- a/extensions/OpenAI/OpenAIConfig.cs +++ b/extensions/OpenAI/OpenAIConfig.cs @@ -68,6 +68,13 @@ public enum TextGenerationTypes /// public int MaxRetries { get; set; } = 10; + /// + /// The number of dimensions output embeddings should have. + /// Only supported in "text-embedding-3" and later models developed with + /// MRL, see https://arxiv.org/abs/2205.13147 + /// + public int? EmbeddingDimensions { get; set; } + /// /// Verify that the current state is valid. /// @@ -87,5 +94,10 @@ public void Validate() { throw new ConfigurationException($"OpenAI: {nameof(this.EmbeddingModelMaxTokenTotal)} cannot be less than 1"); } + + if (this.EmbeddingDimensions is < 1) + { + throw new ConfigurationException($"OpenAI: {nameof(this.EmbeddingDimensions)} cannot be less than 1"); + } } } diff --git a/extensions/OpenAI/OpenAITextEmbeddingGenerator.cs b/extensions/OpenAI/OpenAITextEmbeddingGenerator.cs index 9369d74cd..957e161f5 100644 --- a/extensions/OpenAI/OpenAITextEmbeddingGenerator.cs +++ b/extensions/OpenAI/OpenAITextEmbeddingGenerator.cs @@ -50,7 +50,8 @@ public OpenAITextEmbeddingGenerator( this._client = new OpenAITextEmbeddingGenerationService( modelId: config.EmbeddingModel, openAIClient: openAIClient, - loggerFactory: loggerFactory); + loggerFactory: loggerFactory, + dimensions: config.EmbeddingDimensions); } /// @@ -99,7 +100,8 @@ public OpenAITextEmbeddingGenerator( this._client = new OpenAITextEmbeddingGenerationService( modelId: config.EmbeddingModel, openAIClient: OpenAIClientBuilder.BuildOpenAIClient(config, httpClient), - loggerFactory: loggerFactory); + loggerFactory: loggerFactory, + dimensions: config.EmbeddingDimensions); } /// @@ -123,7 +125,8 @@ public OpenAITextEmbeddingGenerator( this._client = new OpenAITextEmbeddingGenerationService( modelId: config.EmbeddingModel, - openAIClient: OpenAIClientBuilder.BuildOpenAIClient(config, httpClient)); + openAIClient: OpenAIClientBuilder.BuildOpenAIClient(config, httpClient), + dimensions: config.EmbeddingDimensions); } /// diff --git a/service/Service/appsettings.json b/service/Service/appsettings.json index 4a9c07526..a7d41d3f4 100644 --- a/service/Service/appsettings.json +++ b/service/Service/appsettings.json @@ -263,7 +263,11 @@ "Deployment": "", // The max number of tokens supported by model deployed // See https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models - "MaxTokenTotal": 8191 + "MaxTokenTotal": 8191, + // The number of dimensions output embeddings should have. + // Only supported in "text-embedding-3" and later models developed with + // MRL, see https://arxiv.org/abs/2205.13147 + "EmbeddingDimensions": null }, "AzureOpenAIText": { // "ApiKey" or "AzureIdentity" @@ -352,7 +356,11 @@ // Change this to use proxies or services compatible with OpenAI HTTP protocol like LM Studio. "Endpoint": "", // How many times to retry in case of throttling - "MaxRetries": 10 + "MaxRetries": 10, + // The number of dimensions output embeddings should have. + // Only supported in "text-embedding-3" and later models developed with + // MRL, see https://arxiv.org/abs/2205.13147 + "EmbeddingDimensions": null }, "Postgres": { // Postgres instance connection string