Fix and optimize azure openai chat/embedding clients
- Also restructore and clarify the chat/embedding docs.
This commit is contained in:
@@ -15,6 +15,7 @@ import org.springframework.ai.document.Document;
|
||||
import org.springframework.ai.document.MetadataMode;
|
||||
import org.springframework.ai.embedding.AbstractEmbeddingClient;
|
||||
import org.springframework.ai.embedding.Embedding;
|
||||
import org.springframework.ai.embedding.EmbeddingOptions;
|
||||
import org.springframework.ai.embedding.EmbeddingRequest;
|
||||
import org.springframework.ai.embedding.EmbeddingResponse;
|
||||
import org.springframework.ai.embedding.EmbeddingResponseMetadata;
|
||||
@@ -27,9 +28,7 @@ public class AzureOpenAiEmbeddingClient extends AbstractEmbeddingClient {
|
||||
|
||||
private final OpenAIClient azureOpenAiClient;
|
||||
|
||||
private AzureOpenAiEmbeddingOptions defaultOptions = AzureOpenAiEmbeddingOptions.builder()
|
||||
.withModel("text-embedding-ada-002")
|
||||
.build();
|
||||
private final AzureOpenAiEmbeddingOptions defaultOptions;
|
||||
|
||||
private final MetadataMode metadataMode;
|
||||
|
||||
@@ -38,10 +37,18 @@ public class AzureOpenAiEmbeddingClient extends AbstractEmbeddingClient {
|
||||
}
|
||||
|
||||
public AzureOpenAiEmbeddingClient(OpenAIClient azureOpenAiClient, MetadataMode metadataMode) {
|
||||
this(azureOpenAiClient, metadataMode,
|
||||
AzureOpenAiEmbeddingOptions.builder().withModel("text-embedding-ada-002").build());
|
||||
}
|
||||
|
||||
public AzureOpenAiEmbeddingClient(OpenAIClient azureOpenAiClient, MetadataMode metadataMode,
|
||||
AzureOpenAiEmbeddingOptions options) {
|
||||
Assert.notNull(azureOpenAiClient, "com.azure.ai.openai.OpenAIClient must not be null");
|
||||
Assert.notNull(metadataMode, "Metadata mode must not be null");
|
||||
Assert.notNull(options, "Options must not be null");
|
||||
this.azureOpenAiClient = azureOpenAiClient;
|
||||
this.metadataMode = metadataMode;
|
||||
this.defaultOptions = options;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -58,14 +65,7 @@ public class AzureOpenAiEmbeddingClient extends AbstractEmbeddingClient {
|
||||
public EmbeddingResponse call(EmbeddingRequest embeddingRequest) {
|
||||
logger.debug("Retrieving embeddings");
|
||||
|
||||
EmbeddingsOptions azureOptions = new EmbeddingsOptions(embeddingRequest.getInstructions());
|
||||
if (this.defaultOptions != null) {
|
||||
azureOptions = ModelOptionsUtils.merge(azureOptions, this.defaultOptions, EmbeddingsOptions.class);
|
||||
}
|
||||
if (embeddingRequest.getOptions() != null) {
|
||||
azureOptions = ModelOptionsUtils.merge(embeddingRequest.getOptions(), azureOptions,
|
||||
EmbeddingsOptions.class);
|
||||
}
|
||||
EmbeddingsOptions azureOptions = toEmbeddingOptions(embeddingRequest);
|
||||
Embeddings embeddings = this.azureOpenAiClient.getEmbeddings(azureOptions.getModel(), azureOptions);
|
||||
|
||||
logger.debug("Embeddings retrieved");
|
||||
@@ -78,9 +78,10 @@ public class AzureOpenAiEmbeddingClient extends AbstractEmbeddingClient {
|
||||
EmbeddingsOptions toEmbeddingOptions(EmbeddingRequest embeddingRequest) {
|
||||
var azureOptions = new EmbeddingsOptions(embeddingRequest.getInstructions());
|
||||
if (this.defaultOptions != null) {
|
||||
azureOptions = ModelOptionsUtils.merge(azureOptions, this.defaultOptions, EmbeddingsOptions.class);
|
||||
azureOptions.setModel(this.defaultOptions.getModel());
|
||||
azureOptions.setUser(this.defaultOptions.getUser());
|
||||
}
|
||||
if (embeddingRequest.getOptions() != null) {
|
||||
if (embeddingRequest.getOptions() != null && !EmbeddingOptions.EMPTY.equals(embeddingRequest.getOptions())) {
|
||||
azureOptions = ModelOptionsUtils.merge(embeddingRequest.getOptions(), azureOptions,
|
||||
EmbeddingsOptions.class);
|
||||
}
|
||||
@@ -116,14 +117,4 @@ public class AzureOpenAiEmbeddingClient extends AbstractEmbeddingClient {
|
||||
return this.defaultOptions;
|
||||
}
|
||||
|
||||
public void setDefaultOptions(AzureOpenAiEmbeddingOptions defaultOptions) {
|
||||
Assert.notNull(defaultOptions, "Default options must not be null");
|
||||
this.defaultOptions = defaultOptions;
|
||||
}
|
||||
|
||||
public AzureOpenAiEmbeddingClient withDefaultOptions(AzureOpenAiEmbeddingOptions options) {
|
||||
this.defaultOptions = options;
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.azure.ai.openai.OpenAIClient;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import org.springframework.ai.document.MetadataMode;
|
||||
import org.springframework.ai.embedding.EmbeddingRequest;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -36,7 +37,7 @@ public class AzureEmbeddingsOptionsTests {
|
||||
public void createRequestWithChatOptions() {
|
||||
|
||||
OpenAIClient mockClient = Mockito.mock(OpenAIClient.class);
|
||||
var client = new AzureOpenAiEmbeddingClient(mockClient).withDefaultOptions(
|
||||
var client = new AzureOpenAiEmbeddingClient(mockClient, MetadataMode.EMBED,
|
||||
AzureOpenAiEmbeddingOptions.builder().withModel("DEFAULT_MODEL").withUser("USER_TEST").build());
|
||||
|
||||
var requestOptions = client.toEmbeddingOptions(new EmbeddingRequest(List.of("Test message content"), null));
|
||||
|
||||
@@ -4,11 +4,108 @@ Azure's OpenAI offering, powered by ChatGPT, extends beyond traditional OpenAI c
|
||||
|
||||
Azure offers Java developers the opportunity to leverage AI's full potential by integrating it with an array of Azure services, which includes AI-related resources such as Vector Stores on Azure.
|
||||
|
||||
== Getting Started
|
||||
== Pre-requisites
|
||||
|
||||
Obtain your Azure OpenAI `endpoint` and `api-key` from the Azure OpenAI Service section on the link:https://portal.azure.com[Azure Portal].
|
||||
|
||||
=== Configure the Azure OpenAI Chat Client Manually
|
||||
Spring AI defines a configuration property named `spring.ai.azure.openai.api-key` that you should set to the value of the `API Key` obtained from Azure.
|
||||
There is also a configuration property named `spring.ai.azure.openai.endpoint` that you should set to the endpoint URL obtained when provisioning your model in Azure.
|
||||
|
||||
Exporting environment variables is one way to set these configuration properties:
|
||||
|
||||
[source,shell]
|
||||
----
|
||||
export SPRING_AI_AZURE_OPENAI_API_KEY=<INSERT KEY HERE>
|
||||
export SPRING_AI_AZURE_OPENAI_ENDPOINT=<INSERT ENDPOINT URL HERE>
|
||||
----
|
||||
|
||||
== Auto-configuration
|
||||
|
||||
Spring AI provides Spring Boot auto-configuration for the Azure OpenAI Chat Client.
|
||||
To enable it add the following dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
[source, xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
or to your Gradle `build.gradle` build file.
|
||||
|
||||
[source,groovy]
|
||||
----
|
||||
dependencies {
|
||||
implementation 'org.springframework.ai:spring-ai-azure-openai-spring-boot-starter:0.8.0-SNAPSHOT'
|
||||
}
|
||||
----
|
||||
|
||||
=== Chat Properties
|
||||
|
||||
The prefix `spring.ai.azure.openai` is the property prefix to configure the connection to Azure OpenAI.
|
||||
|
||||
[cols="3,5,3"]
|
||||
|====
|
||||
| Property | Description | Default
|
||||
|
||||
| spring.ai.azure.openai.api-key | The Key from Azure AI OpenAI `Keys and Endpoint` section under `Resource Management` | -
|
||||
| spring.ai.azure.openai.endpoint | The endpoint from the Azure AI OpenAI `Keys and Endpoint` section under `Resource Management` | -
|
||||
|====
|
||||
|
||||
The prefix `spring.ai.azure.openai.chat` is the property prefix that configures the `ChatClient` implementation for Azure OpenAI.
|
||||
|
||||
[cols="3,5,3"]
|
||||
|====
|
||||
| Property | Description | Default
|
||||
|
||||
| spring.ai.azure.openai.chat.options.model | * The model name to provide as part of this completions request. Not applicable to Azure OpenAI, where deployment information should be included in the Azure resource URI that's connected to.
|
||||
| gpt-35-turbo
|
||||
| spring.ai.azure.openai.chat.options.maxTokens | The maximum number of tokens to generate. | -
|
||||
| spring.ai.azure.openai.chat.options.temperature | The sampling temperature to use that controls the apparent creativity of generated completions. Higher values will make output more random while lower values will make results more focused and deterministic. It is not recommended to modify temperature and top_p for the same completions request as the interaction of these two settings is difficult to predict. | 0.7
|
||||
| spring.ai.azure.openai.chat.options.topP | An alternative to sampling with temperature called nucleus sampling. This value causes the model to consider the results of tokens with the provided probability mass. | -
|
||||
| spring.ai.azure.openai.chat.options.logitBias | A map between GPT token IDs and bias scores that influences the probability of specific tokens appearing in a completions response. Token IDs are computed via external tokenizer tools, while bias scores reside in the range of -100 to 100 with minimum and maximum values corresponding to a full ban or exclusive selection of a token, respectively. The exact behavior of a given bias score varies by model. | -
|
||||
| spring.ai.azure.openai.chat.options.user | An identifier for the caller or end user of the operation. This may be used for tracking or rate-limiting purposes. | -
|
||||
| spring.ai.azure.openai.chat.options.n | The number of chat completions choices that should be generated for a chat completions response. | -
|
||||
| spring.ai.azure.openai.chat.options.stop | A collection of textual sequences that will end completions generation. | -
|
||||
| spring.ai.azure.openai.chat.options.presencePenalty | A value that influences the probability of generated tokens appearing based on their existing presence in generated text. Positive values will make tokens less likely to appear when they already exist and increase the model's likelihood to output new topics. | -
|
||||
| spring.ai.azure.openai.chat.options.frequencyPenalty | A value that influences the probability of generated tokens appearing based on their cumulative frequency in generated text. Positive values will make tokens less likely to appear as their frequency increases and decrease the likelihood of the model repeating the same statements verbatim. | -
|
||||
|====
|
||||
|
||||
=== Sample Code
|
||||
|
||||
This will create a `ChatClient` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the `ChatClient` implementation.
|
||||
|
||||
[source,application.properties]
|
||||
----
|
||||
spring.ai.azure.openai.api-key=YOUR_API_KEY
|
||||
spring.ai.azure.openai.endpoint=YOUR_ENDPOINT
|
||||
spring.ai.azure.openai.chat.options.model=gpt-35-turbo
|
||||
spring.ai.azure.openai.chat.options.temperature=0.7
|
||||
----
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@RestController
|
||||
public class ChatController {
|
||||
|
||||
private final ChatClient chatClient;
|
||||
|
||||
@Autowired
|
||||
public ChatController(ChatClient chatClient) {
|
||||
this.chatClient = chatClient;
|
||||
}
|
||||
|
||||
@GetMapping("/ai/generate")
|
||||
public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
|
||||
return Map.of("generation", chatClient.generate(message));
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
Add the `spring-ai-azure-openai` dependency to your project's Maven `pom.xml` file:
|
||||
[source, xml]
|
||||
@@ -58,11 +155,13 @@ Flux<ChatResponse> response = chatClient.stream(
|
||||
|
||||
NOTE: the `gpt-35-turbo` is actually the `Deployment Name` as presented in the Azure AI Portal.
|
||||
|
||||
=== Chat Options
|
||||
|
||||
The `AzureOpenAiChatOptions` provides the configuration information for the chat requests.
|
||||
The `AzureOpenAiChatOptions` offers a builder to create the options.
|
||||
|
||||
At start time use the `AzureOpenAiChatClient#withDefaultOptions()` to configure the default options used for all char requests.
|
||||
Furthermore, at runtime, you can override the default options by passing a `AzureOpenAiChatOptions` instance with your to the `Prompt` request.
|
||||
At start time use the `AzureOpenAiChatClient` constructor to set the default options used for all char requests.
|
||||
At runtime, you can override the default options by passing a `AzureOpenAiChatOptions` instance with your to the `Prompt` request.
|
||||
|
||||
For example to override the default model name for a specific request:
|
||||
|
||||
@@ -74,103 +173,3 @@ ChatResponse response = chatClient.call(
|
||||
AzureOpenAiChatOptions.builder().withModel("gpt-4-32k").build()
|
||||
));
|
||||
----
|
||||
|
||||
=== Spring Boot Auto-configuration
|
||||
|
||||
Spring AI provides Spring Boot auto-configuration for the Azure OpenAI Chat Client.
|
||||
To enable it add the following dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
[source, xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
or to your Gradle `build.gradle` build file.
|
||||
|
||||
[source,groovy]
|
||||
----
|
||||
dependencies {
|
||||
implementation 'org.springframework.ai:spring-ai-azure-openai-spring-boot-starter:0.8.0-SNAPSHOT'
|
||||
}
|
||||
----
|
||||
|
||||
Spring AI defines a configuration property named `spring.ai.azure.openai.api-key` that you should set to the value of the `API Key` obtained from Azure.
|
||||
There is also a configuration property named `spring.ai.azure.openai.endpoint` that you should set to the endpoint URL obtained when provisioning your model in Azure.
|
||||
|
||||
Exporting environment variables is one way to set these configuration properties:
|
||||
|
||||
[source,shell]
|
||||
----
|
||||
export SPRING_AI_AZURE_OPENAI_API_KEY=<INSERT KEY HERE>
|
||||
export SPRING_AI_AZURE_OPENAI_ENDPOINT=<INSERT ENDPOINT URL HERE>
|
||||
----
|
||||
|
||||
The `spring.ai.azure.openai.chat.options.*` properties are used to configure the default options used for all chat requests.
|
||||
|
||||
==== Sample Code
|
||||
|
||||
This will create a `ChatClient` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the `ChatClient` implementation.
|
||||
|
||||
[source,application.properties]
|
||||
----
|
||||
spring.ai.azure.openai.api-key=YOUR_API_KEY
|
||||
spring.ai.azure.openai.endpoint=YOUR_ENDPOINT
|
||||
spring.ai.azure.openai.chat.options.model=gpt-35-turbo
|
||||
spring.ai.azure.openai.chat.options.temperature=0.7
|
||||
----
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@RestController
|
||||
public class ChatController {
|
||||
|
||||
private final ChatClient chatClient;
|
||||
|
||||
@Autowired
|
||||
public ChatController(ChatClient chatClient) {
|
||||
this.chatClient = chatClient;
|
||||
}
|
||||
|
||||
@GetMapping("/ai/generate")
|
||||
public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
|
||||
return Map.of("generation", chatClient.generate(message));
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
== Azure OpenAI Chat Properties
|
||||
|
||||
The prefix `spring.ai.azure.openai` is the property prefix to configure the connection to Azure OpenAI.
|
||||
|
||||
[cols="3,5,3"]
|
||||
|====
|
||||
| Property | Description | Default
|
||||
|
||||
| spring.ai.azure.openai.api-key | The Key from Azure AI OpenAI `Keys and Endpoint` section under `Resource Management` | -
|
||||
| spring.ai.azure.openai.endpoint | The endpoint from the Azure AI OpenAI `Keys and Endpoint` section under `Resource Management` | -
|
||||
|====
|
||||
|
||||
|
||||
The prefix `spring.ai.azure.openai.chat` is the property prefix that configures the `ChatClient` implementation for Azure OpenAI.
|
||||
|
||||
[cols="3,5,3"]
|
||||
|====
|
||||
| Property | Description | Default
|
||||
|
||||
| spring.ai.azure.openai.chat.options.model | * The model name to provide as part of this completions request. Not applicable to Azure OpenAI, where deployment information should be included in the Azure resource URI that's connected to.
|
||||
| gpt-35-turbo
|
||||
| spring.ai.azure.openai.chat.options.maxTokens | The maximum number of tokens to generate. | -
|
||||
| spring.ai.azure.openai.chat.options.temperature | The sampling temperature to use that controls the apparent creativity of generated completions. Higher values will make output more random while lower values will make results more focused and deterministic. It is not recommended to modify temperature and top_p for the same completions request as the interaction of these two settings is difficult to predict. | 0.7
|
||||
| spring.ai.azure.openai.chat.options.topP | An alternative to sampling with temperature called nucleus sampling. This value causes the model to consider the results of tokens with the provided probability mass. | -
|
||||
| spring.ai.azure.openai.chat.options.logitBias | A map between GPT token IDs and bias scores that influences the probability of specific tokens appearing in a completions response. Token IDs are computed via external tokenizer tools, while bias scores reside in the range of -100 to 100 with minimum and maximum values corresponding to a full ban or exclusive selection of a token, respectively. The exact behavior of a given bias score varies by model. | -
|
||||
| spring.ai.azure.openai.chat.options.user | An identifier for the caller or end user of the operation. This may be used for tracking or rate-limiting purposes. | -
|
||||
| spring.ai.azure.openai.chat.options.n | The number of chat completions choices that should be generated for a chat completions response. | -
|
||||
| spring.ai.azure.openai.chat.options.stop | A collection of textual sequences that will end completions generation. | -
|
||||
| spring.ai.azure.openai.chat.options.presencePenalty | A value that influences the probability of generated tokens appearing based on their existing presence in generated text. Positive values will make tokens less likely to appear when they already exist and increase the model's likelihood to output new topics. | -
|
||||
| spring.ai.azure.openai.chat.options.frequencyPenalty | A value that influences the probability of generated tokens appearing based on their cumulative frequency in generated text. Positive values will make tokens less likely to appear as their frequency increases and decrease the likelihood of the model repeating the same statements verbatim. | -
|
||||
|====
|
||||
|
||||
@@ -8,14 +8,103 @@ Azure's OpenAI extends the OpenAI capabilities, offering safe text generation an
|
||||
|
||||
The Azure OpenAI embeddings rely on `cosine similarity` to compute similarity between documents and a query.
|
||||
|
||||
== Getting Started
|
||||
== Pre-requisites
|
||||
|
||||
Obtain your Azure OpenAI `endpoint` and `api-key` from the Azure OpenAI Service section on the link:https://portal.azure.com[Azure Portal].
|
||||
|
||||
Spring AI defines a configuration property named `spring.ai.azure.openai.api-key` that you should set to the value of the `API Key` obtained from Azure.
|
||||
There is also a configuration property named `spring.ai.azure.openai.endpoint` that you should set to the endpoint URL obtained when provisioning your model in Azure.
|
||||
|
||||
=== Configure the Azure OpenAI Embedding Client Manually
|
||||
Exporting environment variables is one way to set these configuration properties:
|
||||
[source,shell]
|
||||
----
|
||||
export SPRING_AI_AZURE_OPENAI_API_KEY=<INSERT KEY HERE>
|
||||
export SPRING_AI_AZURE_OPENAI_ENDPOINT=<INSERT ENDPOINT URL HERE>
|
||||
----
|
||||
|
||||
Add the `spring-ai-azure-openai` dependency to your project's Maven `pom.xml` file:
|
||||
== Auto-configuration
|
||||
|
||||
Spring AI provides Spring Boot auto-configuration for the Azure OpenAI Embedding Client.
|
||||
To enable it add the following dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
[source, xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
or to your Gradle `build.gradle` build file.
|
||||
|
||||
[source,groovy]
|
||||
----
|
||||
dependencies {
|
||||
implementation 'org.springframework.ai:spring-ai-azure-openai-spring-boot-starter:0.8.0-SNAPSHOT'
|
||||
}
|
||||
----
|
||||
|
||||
=== Embedding Properties
|
||||
|
||||
The prefix `spring.ai.azure.openai` is the property prefix to configure the connection to Azure OpenAI.
|
||||
|
||||
[cols="3,5,1"]
|
||||
|====
|
||||
| Property | Description | Default
|
||||
|
||||
| spring.ai.azure.openai.api-key | The Key from Azure AI OpenAI `Keys and Endpoint` section under `Resource Management` | -
|
||||
| spring.ai.azure.openai.endpoint | The endpoint from the Azure AI OpenAI `Keys and Endpoint` section under `Resource Management` | -
|
||||
|====
|
||||
|
||||
|
||||
The prefix `spring.ai.azure.openai.embeddings` is the property prefix that configures the `EmbeddingClient` implementation for Azure OpenAI
|
||||
|
||||
[cols="3,5,1"]
|
||||
|====
|
||||
| Property | Description | Default
|
||||
|
||||
| spring.ai.azure.openai.embedding.metadata-mode | Document content extraction mode | EMBED
|
||||
| spring.ai.azure.openai.embedding.options.model | This is the value of the 'Deployment Name' as presented in the Azure AI Portal | text-embedding-ada-002
|
||||
| spring.ai.azure.openai.embedding.options.user | An identifier for the caller or end user of the operation. This may be used for tracking or rate-limiting purposes. | -
|
||||
|====
|
||||
|
||||
=== Sample Code
|
||||
|
||||
This will create a `EmbeddingClient` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the `EmbeddingClient` implementation.
|
||||
|
||||
[source,application.properties]
|
||||
----
|
||||
spring.ai.azure.openai.api-key=YOUR_API_KEY
|
||||
spring.ai.azure.openai.endpoint=YOUR_ENDPOINT
|
||||
spring.ai.azure.openai.embedding.options.model=text-embedding-ada-002
|
||||
----
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@RestController
|
||||
public class EmbeddingController {
|
||||
|
||||
private final EmbeddingClient embeddingClient;
|
||||
|
||||
@Autowired
|
||||
public ChatController(EmbeddingClient embeddingClient) {
|
||||
this.embeddingClient = embeddingClient;
|
||||
}
|
||||
|
||||
@GetMapping("/ai/embedding")
|
||||
public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
|
||||
EmbeddingResponse embeddingResponse = this.embeddingClient.embedForResponse(List.of(message));
|
||||
return Map.of("embedding", embeddingResponse);
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
== Manual Configuration
|
||||
|
||||
If you prefer not to use the Spring Boot auto-configuration, you can manually configure the `AzureOpenAiEmbeddingClient` in your application.
|
||||
For this add the `spring-ai-azure-openai` dependency to your project's Maven `pom.xml` file:
|
||||
[source, xml]
|
||||
----
|
||||
<dependency>
|
||||
@@ -60,8 +149,8 @@ NOTE: the `text-embedding-ada-002` is actually the `Deployment Name` as presente
|
||||
The `AzureOpenAiEmbeddingOptions` provides the configuration information for the embedding requests.
|
||||
The `AzureOpenAiEmbeddingOptions` offers a builder to create the options.
|
||||
|
||||
At start time use the `AzureOpenAiEmbeddingClient#withDefaultOptions()` to configure the default options used for all embedding requests.
|
||||
Furthermore you can override the default options, at runtime, by passing a `AzureOpenAiEmbeddingOptions` instance with your to the `EmbeddingRequest` request.
|
||||
At start time use the `AzureOpenAiEmbeddingClient` constructor to set the default options used for all embedding requests.
|
||||
At run-time you can override the default options, by passing a `AzureOpenAiEmbeddingOptions` instance with your to the `EmbeddingRequest` request.
|
||||
|
||||
For example to override the default model name for a specific request:
|
||||
|
||||
@@ -74,94 +163,3 @@ EmbeddingResponse embeddingResponse = embeddingClient.call(
|
||||
.build()));
|
||||
----
|
||||
|
||||
=== Spring Boot Auto-configuration
|
||||
|
||||
Spring AI provides Spring Boot auto-configuration for the Azure OpenAI Embedding Client.
|
||||
To enable it add the following dependency to your project's Maven `pom.xml` file:
|
||||
|
||||
[source, xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
or to your Gradle `build.gradle` build file.
|
||||
|
||||
[source,groovy]
|
||||
----
|
||||
dependencies {
|
||||
implementation 'org.springframework.ai:spring-ai-azure-openai-spring-boot-starter:0.8.0-SNAPSHOT'
|
||||
}
|
||||
----
|
||||
|
||||
Spring AI defines a configuration property named `spring.ai.azure.openai.api-key` that you should set to the value of the `API Key` obtained from Azure.
|
||||
There is also a configuration property named `spring.ai.azure.openai.endpoint` that you should set to the endpoint URL obtained when provisioning your model in Azure.
|
||||
|
||||
Exporting environment variables is one way to set these configuration properties:
|
||||
|
||||
[source,shell]
|
||||
----
|
||||
export SPRING_AI_AZURE_OPENAI_API_KEY=<INSERT KEY HERE>
|
||||
export SPRING_AI_AZURE_OPENAI_ENDPOINT=<INSERT ENDPOINT URL HERE>
|
||||
----
|
||||
|
||||
The `spring.ai.azure.openai.embedding.options.*` properties are used to configure the default options used for all embedding requests.
|
||||
|
||||
==== Sample Code
|
||||
|
||||
This will create a `EmbeddingClient` implementation that you can inject into your class.
|
||||
Here is an example of a simple `@Controller` class that uses the `EmbeddingClient` implementation.
|
||||
|
||||
[source,application.properties]
|
||||
----
|
||||
spring.ai.azure.openai.api-key=YOUR_API_KEY
|
||||
spring.ai.azure.openai.endpoint=YOUR_ENDPOINT
|
||||
spring.ai.azure.openai.embedding.options.model=text-embedding-ada-002
|
||||
----
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@RestController
|
||||
public class EmbeddingController {
|
||||
|
||||
private final EmbeddingClient embeddingClient;
|
||||
|
||||
@Autowired
|
||||
public ChatController(EmbeddingClient embeddingClient) {
|
||||
this.embeddingClient = embeddingClient;
|
||||
}
|
||||
|
||||
@GetMapping("/ai/embedding")
|
||||
public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
|
||||
EmbeddingResponse embeddingResponse = this.embeddingClient.embedForResponse(List.of(message));
|
||||
return Map.of("embedding", embeddingResponse);
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
== Azure OpenAI Embedding Properties
|
||||
|
||||
The prefix `spring.ai.azure.openai` is the property prefix to configure the connection to Azure OpenAI.
|
||||
|
||||
[cols="3,5,3"]
|
||||
|====
|
||||
| Property | Description | Default
|
||||
|
||||
| spring.ai.azure.openai.api-key | The Key from Azure AI OpenAI `Keys and Endpoint` section under `Resource Management` | -
|
||||
| spring.ai.azure.openai.endpoint | The endpoint from the Azure AI OpenAI `Keys and Endpoint` section under `Resource Management` | -
|
||||
|====
|
||||
|
||||
|
||||
The prefix `spring.ai.azure.openai.embeddings` is the property prefix that configures the `EmbeddingClient` implementation for Azure OpenAI
|
||||
|
||||
[cols="3,5,3"]
|
||||
|====
|
||||
| Property | Description | Default
|
||||
|
||||
| spring.ai.azure.openai.embedding.options.model | This is the value of the 'Deployment Name' as presented in the Azure AI Portal | text-embedding-ada-002
|
||||
| spring.ai.azure.openai.embedding.options.user | An identifier for the caller or end user of the operation. This may be used for tracking or rate-limiting purposes. | -
|
||||
|====
|
||||
|
||||
@@ -61,7 +61,7 @@ The prefix `spring.ai.openai.embedding` is property prefix that configures the `
|
||||
| Property | Description | Default
|
||||
| spring.ai.openai.embedding.base-url | Optional overrides the spring.ai.openai.base-url to provide embedding specific url | -
|
||||
| spring.ai.openai.embedding.api-key | Optional overrides the spring.ai.openai.api-key to provide embedding specific api-key | -
|
||||
| spring.ai.openai.embedding.metadata-mode | Document content extraction mode | EMBED
|
||||
| spring.ai.openai.embedding.metadata-mode | Document content extraction mode. | EMBED
|
||||
| spring.ai.openai.embedding.options.model | The model to use | text-embedding-ada-002
|
||||
| spring.ai.openai.embedding.options.encodingFormat | The format to return the embeddings in. Can be either float or base64. | -
|
||||
| spring.ai.openai.embedding.options.user | A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. | -
|
||||
@@ -153,7 +153,7 @@ The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-open
|
||||
|
||||
The default options can be configured using the `spring.ai.openai.embedding.options` properties as well.
|
||||
|
||||
At start-time use the `OpenAiEmbeddingClient#withDefaultOptions()` to configure the default options used for all embedding requests.
|
||||
At start-time use the `OpenAiEmbeddingClient` constructor to set the default options used for all embedding requests.
|
||||
At run-time you can override the default options, using a `OpenAiEmbeddingOptions` instance as part of your `EmbeddingRequest`.
|
||||
|
||||
For example to override the default model name for a specific request:
|
||||
|
||||
@@ -60,7 +60,8 @@ public class AzureOpenAiAutoConfiguration {
|
||||
@Bean
|
||||
public AzureOpenAiEmbeddingClient azureOpenAiEmbeddingClient(OpenAIClient openAIClient,
|
||||
AzureOpenAiEmbeddingProperties embeddingProperties) {
|
||||
return new AzureOpenAiEmbeddingClient(openAIClient).withDefaultOptions(embeddingProperties.getOptions());
|
||||
return new AzureOpenAiEmbeddingClient(openAIClient, embeddingProperties.getMetadataMode(),
|
||||
embeddingProperties.getOptions());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.ai.autoconfigure.azure.openai;
|
||||
|
||||
import org.springframework.ai.azure.openai.AzureOpenAiEmbeddingOptions;
|
||||
import org.springframework.ai.document.MetadataMode;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -29,6 +30,8 @@ public class AzureOpenAiEmbeddingProperties {
|
||||
.withModel("text-embedding-ada-002")
|
||||
.build();
|
||||
|
||||
private MetadataMode metadataMode = MetadataMode.EMBED;
|
||||
|
||||
public AzureOpenAiEmbeddingOptions getOptions() {
|
||||
return options;
|
||||
}
|
||||
@@ -38,4 +41,13 @@ public class AzureOpenAiEmbeddingProperties {
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
public MetadataMode getMetadataMode() {
|
||||
return metadataMode;
|
||||
}
|
||||
|
||||
public void setMetadataMode(MetadataMode metadataMode) {
|
||||
Assert.notNull(metadataMode, "Metadata mode must not be null");
|
||||
this.metadataMode = metadataMode;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -57,11 +57,11 @@ public class AzureOpenAiAutoConfigurationIT {
|
||||
"spring.ai.azure.openai.api-key=" + System.getenv("AZURE_OPENAI_API_KEY"),
|
||||
"spring.ai.azure.openai.endpoint=" + System.getenv("AZURE_OPENAI_ENDPOINT"),
|
||||
|
||||
"spring.ai.azure.openai.chat.model=" + CHAT_MODEL_NAME,
|
||||
"spring.ai.azure.openai.chat.temperature=0.8",
|
||||
"spring.ai.azure.openai.chat.maxTokens=123",
|
||||
"spring.ai.azure.openai.chat.options.model=" + CHAT_MODEL_NAME,
|
||||
"spring.ai.azure.openai.chat.options.temperature=0.8",
|
||||
"spring.ai.azure.openai.chat.options.maxTokens=123",
|
||||
|
||||
"spring.ai.azure.openai.embedding.model=" + EMBEDDING_MODEL_NAME
|
||||
"spring.ai.azure.openai.embedding.options.model=" + EMBEDDING_MODEL_NAME
|
||||
// @formatter:on
|
||||
).withConfiguration(AutoConfigurations.of(AzureOpenAiAutoConfiguration.class));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user