DashScope 是阿里云提供的大模型服务平台,提供通义千问等多个大语言模型。Spring AI Alibaba 提供了 DashScopeChatModel
的集成。
前置条件
在使用 DashScopeChatModel 之前,你需要:
- 获取 DashScope API Key:访问 阿里云百炼
- 设置环境变量:export AI_DASHSCOPE_API_KEY=your_api_key
添加依赖
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
<version>1.1.2.1</version>
</dependency>
基础使用
创建 ChatModel
import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import org.springframework.ai.chat.model.ChatModel;
// 创建 DashScope API 实例
DashScopeApi dashScopeApi = DashScopeApi.builder()
.apiKey(System.getenv("AI_DASHSCOPE_API_KEY"))
.build();
// 创建 ChatModel
ChatModel chatModel = DashScopeChatModel.builder()
.dashScopeApi(dashScopeApi)
.build();
简单调用
// 使用字符串直接调用
String response = chatModel.call("介绍一下Spring框架");
System.out.println(response);
使用 Prompt
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
// 创建 Prompt
Prompt prompt = new Prompt(new UserMessage("解释什么是微服务架构"));
// 调用并获取响应
ChatResponse response = chatModel.call(prompt);
String answer = response.getResult().getOutput().getText();
System.out.println(answer);
配置选项
使用 ChatOptions
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
DashScopeChatOptions options = DashScopeChatOptions.builder()
.withModel("qwen-plus") // 模型名称
.withTemperature(0.7) // Temperature 参数
.withMaxToken(2000) // 最大令牌数
.withTopP(0.9) // Top-P 采样
.build();
ChatModel chatModel = DashScopeChatModel.builder()
.dashScopeApi(dashScopeApi)
.defaultOptions(options)
.build();
运行时覆盖选项
// 创建带有特定选项的 Prompt
DashScopeChatOptions runtimeOptions = DashScopeChatOptions.builder()
.withTemperature(0.3) // 更低的温度,更确定的输出
.withMaxToken(500)
.build();
Prompt prompt = new Prompt(
new UserMessage("用一句话总结Java的特点"),
runtimeOptions
);
ChatResponse response = chatModel.call(prompt);
流式响应
import reactor.core.publisher.Flux;
// 使用流式 API
Flux<ChatResponse> responseStream = chatModel.stream(
new Prompt("详细解释Spring Boot的自动配置原理")
);
// 订阅并处理流式响应
responseStream.subscribe(
chatResponse -> {
String content = chatResponse.getResult()
.getOutput()
.getText();
System.out.print(content);
},
error -> System.err.println("错误: " + error.getMessage()),
() -> System.out.println("
流式响应完成")
);
多轮对话
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.AssistantMessage;
import java.util.List;
// 创建对话历史
List<Message> messages = List.of(
new SystemMessage("你是一个Java专家"),
new UserMessage("什么是Spring Boot?"),
new AssistantMessage("Spring Boot是..."),
new UserMessage("它有什么优势?")
);
Prompt prompt = new Prompt(messages);
ChatResponse response = chatModel.call(prompt);
支持的模型
DashScope 支持多个模型,包括:
- qwen-turbo: 通义千问超大规模语言模型,支持中文、英文等
- qwen-plus: 通义千问增强版
- qwen-max: 通义千问旗舰版
- qwen-max-longcontext: 支持长文本的通义千问
函数调用
DashScopeChatModel 支持函数调用(Function Calling),允许模型调用外部函数:
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.function.FunctionToolCallback;
// 定义函数工具
ToolCallback weatherFunction = FunctionToolCallback.builder("getWeather", (String city) -> {
// 实际的天气查询逻辑
return "晴朗,25°C";
})
.description("获取指定城市的天气")
.inputType(String.class)
.build();
// 使用函数
DashScopeChatOptions options = DashScopeChatOptions.builder()
.withToolCallbacks(List.of(weatherFunction))
.build();
Prompt prompt = new Prompt("北京的天气怎么样?", options);
ChatResponse response = chatModel.call(prompt);
在 Spring AI Alibaba Agent Framework 中使用 DashScopeChatModel:
import com.alibaba.cloud.ai.graph.agent.ReactAgent;
ReactAgent agent = ReactAgent.builder()
.name("my_agent")
.model(chatModel)
.systemPrompt("你是一个有帮助的AI助手")
.build();
// 调用 Agent
AssistantMessage response = agent.call("帮我分析这个问题");
详细的 Agent 使用方法请参考 Agents 文档。
通过这个统一的 API,开发者可以轻松地在不同的 AI 服务提供商之间切换,同时保持代码的一致性和可维护性。
本文整理自 Spring AI Alibaba 官方文档《Models 模型》:https://java2ai.com/docs/frameworks/agent-framework/tutorials/models

AI项目博客






