QianFan Chat

Spring AI supports the various AI language models from QianFan. You can interact with QianFan language models and create a multilingual conversational assistant based on QianFan models.spring-doc.cn

Prerequisites

You will need to create an API with QianFan to access QianFan language models.spring-doc.cn

Create an account at QianFan registration page and generate the token on the API Keys page. The Spring AI project defines a configuration property named spring.ai.qianfan.api-key and spring.ai.qianfan.secret-key. you should set to the value of the API Key and Secret Key obtained from API Keys page. Exporting an environment variable is one way to set that configuration property:spring-doc.cn

export SPRING_AI_QIANFAN_API_KEY=<INSERT API KEY HERE>
export SPRING_AI_QIANFAN_SECRET_KEY=<INSERT SECRET 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.spring-doc.cn

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.spring-doc.cn

Auto-configuration

Spring AI provides Spring Boot auto-configuration for the Azure QianFan Embedding Client. To enable it add the following dependency to your project’s Maven pom.xml file:spring-doc.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-qianfan-spring-boot-starter</artifactId>
</dependency>

or to your Gradle build.gradle build file.spring-doc.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-qianfan-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 QianFan Embedding client.spring-doc.cn

Property Description Default

spring.ai.retry.max-attemptsspring-doc.cn

Maximum number of retry attempts.spring-doc.cn

10spring-doc.cn

spring.ai.retry.backoff.initial-intervalspring-doc.cn

Initial sleep duration for the exponential backoff policy.spring-doc.cn

2 sec.spring-doc.cn

spring.ai.retry.backoff.multiplierspring-doc.cn

Backoff interval multiplier.spring-doc.cn

5spring-doc.cn

spring.ai.retry.backoff.max-intervalspring-doc.cn

Maximum backoff duration.spring-doc.cn

3 min.spring-doc.cn

spring.ai.retry.on-client-errorsspring-doc.cn

If false, throw a NonTransientAiException, and do not attempt retry for 4xx client error codesspring-doc.cn

falsespring-doc.cn

spring.ai.retry.exclude-on-http-codesspring-doc.cn

List of HTTP status codes that should not trigger a retry (e.g. to throw NonTransientAiException).spring-doc.cn

emptyspring-doc.cn

spring.ai.retry.on-http-codesspring-doc.cn

List of HTTP status codes that should trigger a retry (e.g. to throw TransientAiException).spring-doc.cn

emptyspring-doc.cn

Connection Properties

The prefix spring.ai.qianfan is used as the property prefix that lets you connect to QianFan.spring-doc.cn

Property Description Default

spring.ai.qianfan.base-urlspring-doc.cn

The URL to connect tospring-doc.cn

aip.baidubce.com/rpc/2.0/ai_customspring-doc.cn

spring.ai.qianfan.api-keyspring-doc.cn

The API Keyspring-doc.cn

-spring-doc.cn

spring.ai.qianfan.secret-keyspring-doc.cn

The Secret Keyspring-doc.cn

-spring-doc.cn

Configuration Properties

The prefix spring.ai.qianfan.embedding is property prefix that configures the EmbeddingClient implementation for QianFan.spring-doc.cn

Property Description Default

spring.ai.qianfan.embedding.enabledspring-doc.cn

Enable QianFan embedding client.spring-doc.cn

truespring-doc.cn

spring.ai.qianfan.embedding.base-urlspring-doc.cn

Optional overrides the spring.ai.qianfan.base-url to provide embedding specific urlspring-doc.cn

-spring-doc.cn

spring.ai.qianfan.embedding.api-keyspring-doc.cn

Optional overrides the spring.ai.qianfan.api-key to provide embedding specific api-keyspring-doc.cn

-spring-doc.cn

spring.ai.qianfan.embedding.secret-keyspring-doc.cn

Optional overrides the spring.ai.qianfan.secret-key to provide embedding specific secret-keyspring-doc.cn

-spring-doc.cn

spring.ai.qianfan.embedding.options.modelspring-doc.cn

The model to usespring-doc.cn

bge_large_zhspring-doc.cn

You can override the common spring.ai.qianfan.base-url, spring.ai.qianfan.api-key and spring.ai.qianfan.secret-key for the ChatModel and EmbeddingModel implementations. The spring.ai.qianfan.chat.base-url, spring.ai.qianfan.chat.api-key and spring.ai.qianfan.chat.secret-key properties if set take precedence over the common properties. Similarly, the spring.ai.qianfan.chat.base-url, spring.ai.qianfan.chat.api-key and spring.ai.qianfan.chat.secret-key properties if set take precedence over the common properties. This is useful if you want to use different QianFan accounts for different models and different model endpoints.
All properties prefixed with spring.ai.qianfan.embedding.options can be overridden at runtime by adding a request specific Runtime Options to the EmbeddingRequest call.

Runtime Options

The QianFanEmbeddingOptions.java provides the QianFan configurations, such as the model to use and etc.spring-doc.cn

The default options can be configured using the spring.ai.qianfan.embedding.options properties as well.spring-doc.cn

At start-time use the QianFanEmbeddingModel constructor to set the default options used for all embedding requests. At run-time you can override the default options, using a QianFanEmbeddingOptions instance as part of your EmbeddingRequest.spring-doc.cn

For example to override the default model name for a specific request:spring-doc.cn

EmbeddingResponse embeddingResponse = embeddingClient.call(
    new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
        QianFanEmbeddingOptions.builder()
            .withModel("Different-Embedding-Model-Deployment-Name")
        .build()));

Sample Controller

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.spring-doc.cn

spring.ai.qianfan.api-key=YOUR_API_KEY
spring.ai.qianfan.secret-key=YOUR_SECRET_KEY
spring.ai.qianfan.embedding.options.model=tao_8k
@RestController
public class EmbeddingController {

    private final EmbeddingClient embeddingClient;

    @Autowired
    public EmbeddingController(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 are not using Spring Boot, you can manually configure the QianFan Embedding Client. For this add the spring-ai-qianfan dependency to your project’s Maven pom.xml file:spring-doc.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-qianfan</artifactId>
</dependency>

or to your Gradle build.gradle build file.spring-doc.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-qianfan'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file.
The spring-ai-qianfan dependency provides access also to the QianFanChatModel. For more information about the QianFanChatModel refer to the QianFan Chat Client section.

Next, create an QianFanEmbeddingModel instance and use it to compute the similarity between two input texts:spring-doc.cn

var qianFanApi = new QianFanApi(System.getenv("MINIMAX_API_KEY"), System.getenv("QIANFAN_SECRET_KEY"));

var embeddingClient = new QianFanEmbeddingModel(qianFanApi)
    .withDefaultOptions(QianFanChatOptions.build()
        .withModel("bge_large_en")
        .build());

EmbeddingResponse embeddingResponse = this.embeddingClient
	.embedForResponse(List.of("Hello World", "World is big and salvation is near"));

The QianFanEmbeddingOptions provides the configuration information for the embedding requests. The options class offers a builder() for easy options creation.spring-doc.cn