1、环境配置
- maven依赖pom.xml
注意配置远程仓库,原因见:Unresolved dependency: ‘org.springframework.ai:spring-ai-core:jar:0.8.1’
<dependencies>
<!--Base-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--AI-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-ai</artifactId>
<version>2023.0.1.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
- application.yaml
# Tomcat
server:
port: 9310
# Spring
spring:
cloud:
ai:
tongyi:
api-key: api密钥
api-key申请地址:申请地址
2、服务实现
controller就不详细展示了,主要展示service中如何调用
@Service
@AllArgsConstructor
public class TongyiServiceImpl implements ITongyiService {
private final ChatClient chatClient;
private final ImageClient imageClient;
private final SpeechClient speechClient;
// 对话
@Override
public String chat(String question) {
return chatClient.call(question);
}
// 文生图
@Override
public String image(String promptWord) {
ImagePrompt imagePrompt = new ImagePrompt(promptWord);
return imageClient.call(imagePrompt).getResult().getOutput().getUrl();
}
// 语音
@Override
public ByteBuffer speech(String promptWord) {
SpeechPrompt question = new SpeechPrompt(promptWord);
return speechClient.call(question).getResult().getOutput();
}
}
效果图