One day when I check the maven BOM of Spring-AI
, I found a Springboot starter for Spring AI to access Dashscope, which was the former name of Alibaba Cloud Model Studio.
But the doc of Spring AI didn't announce that, so, let's learn to use it.
Spring AI need your jdk version >= 17
1. Apply a Api-key on Alibaba Cloud Model Studio
2. Import Spring-AI
This is a new Springboot maven project
- Add Spring Milestone and Snapshot Repositories
You will need to add Spring milestone and snapshot repositories to your build system. Because Spring didn't publish it to maven center repository.
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
- Import Spring AI BOM to Manage the Version of Spring AI
<dependencyManagement>
<dependencies>
<dependency>
<groupId>group.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- Add
spring-ai-dashscope-spring-boot-starter
and Some Assistant Package
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>group.springframework.ai</groupId>
<artifactId>spring-ai-dashscope-spring-boot-starter</artifactId>
</dependency>
</dependencies>
3. Write application.yaml
spring:
ai:
dashscope:
# DashScope API Key
api-key: # TODO
chat:
options:
# choose the model you want to use
model: deepseek-v3
# temperature is the probability of the model to generate a new word, 0.7 is the default value
temperature: 0.7
logging:
level:
root: info
# if you want to check the log of Spring AI, you need enable this config
# org.springframework: debug
server:
port: 9090
The value of temperature you can fellow the advice of deepseek
4. Register ChatClient
as a Spring Bean
@Configuration
public class SpringAiConfig {
@Autowired
private ChatModel chatModel;
@Bean
public ChatClient chatClient() {
return ChatClient.builder(chatModel)
// Set default system prompt
.defaultSystem("""
Imitate Max Verstappen's tone
date of today is {current_date}
""")
.build();
}
}
Then you can use ChatClient to access AI model
5. Test ChatClient
I create a controller to test ChatClient, All test will based on web request to access.
@RestController
@RequiredArgsConstructor
@RequestMapping("/ai")
@Slf4j
public class AiController {
private final ChatClient chatClient;
}
Let's start Spring AI with a normal chat
@GetMapping("/chat")
public String chat(@RequestParam(value = "msg") String msg) {
log.info("msg: {}", msg);
return chatClient.prompt()
.user(msg)
// config default system's current date
.system(promptSystemSpec -> promptSystemSpec.param("current_date", LocalDateTime.now()))
.call()
.content();
}
Start the project and test it using postman
How amazing it is!!!🎉🎉🎉
If you want to learn the advance of Spring AI, follow me and I will publish it later.
Top comments (0)