ZhiPuAI Embeddings
Spring AI supports the ZhiPuAI’s text embeddings models. ZhiPuAI’s text embeddings measure the relatedness of text strings. An embedding is a vector (list) of floating point numbers. The distance between two vectors measures their relatedness. Small distances suggest high relatedness and large distances suggest low relatedness.
Prerequisites
You will need to create an API with ZhiPuAI to access ZhiPu AI language models.
Create an account at ZhiPu AI registration page and generate the token on the API Keys page.
The Spring AI project defines a configuration property named spring.ai.zhipu.api-key
that you should set to the value of the API Key
obtained from API Keys page.
Exporting an environment variable is one way to set that configuration property:
export SPRING_AI_ZHIPU_AI_API_KEY=<INSERT KEY HERE>
Add Repositories and BOM
Spring AI artifacts are published in Spring Milestone and Snapshot repositories. Refer to the Repositories section to add these repositories to your build system.
To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the Dependency Management section to add the Spring AI BOM to your build system.
Auto-configuration
Spring AI provides Spring Boot auto-configuration for the Azure ZhiPuAI Embedding Model.
To enable it add the following dependency to your project’s Maven pom.xml
file:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-zhipuai-spring-boot-starter</artifactId>
</dependency>
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-zhipuai-spring-boot-starter'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
Embedding Properties
Retry Properties
The prefix spring.ai.retry
is used as the property prefix that lets you configure the retry mechanism for the ZhiPuAI Embedding model.
Property | Description | Default |
---|---|---|
spring.ai.retry.max-attempts |
Maximum number of retry attempts. |
10 |
spring.ai.retry.backoff.initial-interval |
Initial sleep duration for the exponential backoff policy. |
2 sec. |
spring.ai.retry.backoff.multiplier |
Backoff interval multiplier. |
5 |
spring.ai.retry.backoff.max-interval |
Maximum backoff duration. |
3 min. |
spring.ai.retry.on-client-errors |
If false, throw a NonTransientAiException, and do not attempt retry for |
false |
spring.ai.retry.exclude-on-http-codes |
List of HTTP status codes that should not trigger a retry (e.g. to throw NonTransientAiException). |
empty |
spring.ai.retry.on-http-codes |
List of HTTP status codes that should trigger a retry (e.g. to throw TransientAiException). |
empty |
Connection Properties
The prefix spring.ai.zhipuai
is used as the property prefix that lets you connect to ZhiPuAI.
Property | Description | Default |
---|---|---|
spring.ai.zhipuai.base-url |
The URL to connect to |
|
spring.ai.zhipuai.api-key |
The API Key |
- |
Configuration Properties
The prefix spring.ai.zhipuai.embedding
is property prefix that configures the EmbeddingModel
implementation for ZhiPuAI.
Property | Description | Default |
---|---|---|
spring.ai.zhipuai.embedding.enabled |
Enable ZhiPuAI embedding model. |
true |
spring.ai.zhipuai.embedding.base-url |
Optional overrides the spring.ai.zhipuai.base-url to provide embedding specific url |
- |
spring.ai.zhipuai.embedding.api-key |
Optional overrides the spring.ai.zhipuai.api-key to provide embedding specific api-key |
- |
spring.ai.zhipuai.embedding.options.model |
The model to use |
embedding-2 |
You can override the common spring.ai.zhipuai.base-url and spring.ai.zhipuai.api-key for the ChatModel and EmbeddingModel implementations.
The spring.ai.zhipuai.embedding.base-url and spring.ai.zhipuai.embedding.api-key properties if set take precedence over the common properties.
Similarly, the spring.ai.zhipuai.chat.base-url and spring.ai.zhipuai.chat.api-key properties if set take precedence over the common properties.
This is useful if you want to use different ZhiPuAI accounts for different models and different model endpoints.
|
All properties prefixed with spring.ai.zhipuai.embedding.options can be overridden at runtime by adding a request specific Runtime Options to the EmbeddingRequest call.
|
Runtime Options
The ZhiPuAiEmbeddingOptions.java provides the ZhiPuAI configurations, such as the model to use and etc.
The default options can be configured using the spring.ai.zhipuai.embedding.options
properties as well.
At start-time use the ZhiPuAiEmbeddingModel
constructor to set the default options used for all embedding requests.
At run-time you can override the default options, using a ZhiPuAiEmbeddingOptions
instance as part of your EmbeddingRequest
.
For example to override the default model name for a specific request:
EmbeddingResponse embeddingResponse = embeddingModel.call(
new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
ZhiPuAiEmbeddingOptions.builder()
.withModel("Different-Embedding-Model-Deployment-Name")
.build()));
Sample Controller
This will create a EmbeddingModel
implementation that you can inject into your class.
Here is an example of a simple @Controller
class that uses the EmbeddingModel
implementation.
spring.ai.zhipuai.api-key=YOUR_API_KEY
spring.ai.zhipuai.embedding.options.model=embedding-2
@RestController
public class EmbeddingController {
private final EmbeddingModel embeddingModel;
@Autowired
public EmbeddingController(EmbeddingModel embeddingModel) {
this.embeddingModel = embeddingModel;
}
@GetMapping("/ai/embedding")
public Map embed(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
EmbeddingResponse embeddingResponse = this.embeddingModel.embedForResponse(List.of(message));
return Map.of("embedding", embeddingResponse);
}
}
Manual Configuration
If you are not using Spring Boot, you can manually configure the ZhiPuAI Embedding Model.
For this add the spring-ai-zhipuai
dependency to your project’s Maven pom.xml
file:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-zhipuai</artifactId>
</dependency>
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-zhipuai'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
The spring-ai-zhipuai dependency provides access also to the ZhiPuAiChatModel .
For more information about the ZhiPuAiChatModel refer to the ZhiPuAI Chat Client section.
|
Next, create an ZhiPuAiEmbeddingModel
instance and use it to compute the similarity between two input texts:
var zhiPuAiApi = new ZhiPuAiApi(System.getenv("ZHIPU_AI_API_KEY"));
var embeddingModel = new ZhiPuAiEmbeddingModel(this.zhiPuAiApi)
.withDefaultOptions(ZhiPuAiChatOptions.build()
.withModel("embedding-2")
.build());
EmbeddingResponse embeddingResponse = this.embeddingModel
.embedForResponse(List.of("Hello World", "World is big and salvation is near"));
The ZhiPuAiEmbeddingOptions
provides the configuration information for the embedding requests.
The options class offers a builder()
for easy options creation.