feat(ai): 集成阿里云DashScope AI模型支持
- 新增DashScope AI配置项,支持qwen-plus模型 - 添加AIResponse数据模型用于流式响应 - 实现普通对话和流式对话两个接口 - 引入spring-ai-alibaba-starter-dashscope依赖 - 更新OpenAIController移除无用导入 - 在pom.xml中添加spring-ai-alibaba-bom管理依赖版本
This commit is contained in:
12
pom.xml
12
pom.xml
@@ -50,6 +50,11 @@
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-starter-model-openai</artifactId>
|
||||
</dependency>
|
||||
<!-- 阿里云AI -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud.ai</groupId>
|
||||
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
@@ -79,6 +84,13 @@
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud.ai</groupId>
|
||||
<artifactId>spring-ai-alibaba-bom</artifactId>
|
||||
<version>1.0.0.4</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.hanserwei.airobot.controller;
|
||||
|
||||
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
|
||||
import com.hanserwei.airobot.model.AIResponse;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.model.Generation;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/v6/ai")
|
||||
public class DashscopeAIController {
|
||||
|
||||
@Resource
|
||||
private DashScopeChatModel dashScopeChatModel;
|
||||
|
||||
/**
|
||||
* 普通对话
|
||||
*
|
||||
* @param message 对话输入内容
|
||||
* @return 对话结果
|
||||
*/
|
||||
@GetMapping("/generate")
|
||||
public String generate(@RequestParam(value = "message", defaultValue = "你是谁?") String message) {
|
||||
// 一次性返回结果
|
||||
ChatClient chatClient = ChatClient.builder(dashScopeChatModel).build();
|
||||
return chatClient.prompt()
|
||||
.user(message)
|
||||
.call()
|
||||
.content();
|
||||
}
|
||||
|
||||
/**
|
||||
* 流式对话
|
||||
*
|
||||
* @param message 对话输入内容
|
||||
* @return 对话结果
|
||||
*/
|
||||
@GetMapping(value = "/generateStream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public Flux<AIResponse> generateStream(@RequestParam(value = "message", defaultValue = "你是谁?") String message) {
|
||||
ChatClient chatClient = ChatClient.builder(dashScopeChatModel).build();
|
||||
return chatClient.prompt()
|
||||
.user(message)
|
||||
.stream()
|
||||
.chatResponse()
|
||||
.mapNotNull(
|
||||
chatResponse -> {
|
||||
Generation generation = chatResponse.getResult();
|
||||
String text = generation.getOutput().getText();
|
||||
return AIResponse.builder().v(text).build();
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -12,8 +12,6 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/v5/ai")
|
||||
public class OpenAIController {
|
||||
|
||||
15
src/main/java/com/hanserwei/airobot/model/AIResponse.java
Normal file
15
src/main/java/com/hanserwei/airobot/model/AIResponse.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.hanserwei.airobot.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class AIResponse {
|
||||
// 流式响应内容
|
||||
private String v;
|
||||
}
|
||||
@@ -30,6 +30,12 @@ spring:
|
||||
options:
|
||||
model: gpt-4o # 模型名称
|
||||
temperature: 0.7 # 温度值
|
||||
dashscope:
|
||||
api-key: ENC(cMgcKZkFllyE88DIbGwLKot9Vg02co+gsmY8L8o4/o3UjhcmqO4lJzFU35Sx0n+qFG8pDL0wBjoWrT8X6BuRw9vNlQhY1LgRWHaF9S1zzyM=)
|
||||
chat:
|
||||
options:
|
||||
model: qwen-plus
|
||||
temperature: 0.7
|
||||
|
||||
jasypt:
|
||||
encryptor:
|
||||
|
||||
Reference in New Issue
Block a user