news 2026/8/2 23:49:47

01 spring ai alibaba(SAA1.1.2)基础聊天实现-ChatModel

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
01 spring ai alibaba(SAA1.1.2)基础聊天实现-ChatModel

前置条件

在使用 DashScopeChatModel 之前,你需要:

  1. 获取 DashScope API Key:访问 阿里云百炼
  2. 设置环境变量: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

DashScopeChatOptions 配置示例

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions; DashScopeChatOptions options = DashScopeChatOptions.builder() .model("qwen-plus") // 模型名称 .temperature(0.7) // Temperature 参数 .maxToken(2000) // 最大令牌数 .topP(0.9) // Top-P 采样 .build(); ChatModel chatModel = DashScopeChatModel.builder() .dashScopeApi(dashScopeApi) .defaultOptions(options) .build();

常用选项说明

  • model: 要使用的模型 ID
  • frequencyPenalty: 频率惩罚(-2.0 到 2.0),降低重复令牌的可能性
  • maxTokens: 生成响应的最大令牌数
  • presencePenalty: 存在惩罚(-2.0 到 2.0),鼓励谈论新主题
  • stopSequences: 停止序列列表,遇到时停止生成
  • temperature: 采样温度(0.0 到 2.0),控制随机性
  • topK: Top-K 采样参数
  • topP: Top-P(核采样)参数

此外,每个特定模型的 ChatModel/StreamingChatModel 实现都可以有自己的选项。例如,OpenAI Chat Completion 模型有自己的选项,如logitBiasseeduser

这是一个强大的功能,允许开发者在启动应用程序时使用特定于模型的选项,然后在运行时使用Prompt请求覆盖它们。

运行时覆盖选项

通过prompt运行时覆盖选项示例

// 创建带有特定选项的 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);

函数调用(外部工具)

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);
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/14 15:05:07

APM使用LUA脚本发送实现遥控器PWM信号输出CAN协议信号

需求&#xff1a;由于舵机是CAN总线舵机&#xff0c;需实现APM开源飞控遥控器输入PWM通道到CAN的发送。 方法1&#xff1a;修改APM固件源码&#xff0c;编译&#xff0c;运行&#xff0c;测试。实现复杂。 方法2&#xff1a;使用lua脚本。实现简单 目前采用方法2&#xff0c;使…

作者头像 李华
网站建设 2026/7/14 15:05:06

从接口到业务协同:看懂 SAP 集成与 API 的底层逻辑

在很多企业项目里,业务系统从来都不是孤岛。人力、财务、采购、销售、制造、客户服务,往往分布在不同产品、不同平台,甚至不同云环境里。真正让这些系统协同运转的,不是某一个单独的事务码,也不是某一张漂亮的报表,而是隐藏在背后的 integration 和 API。它们决定了一条业…

作者头像 李华
网站建设 2026/7/14 15:05:20

从零搭建ComfyUI:硬件选型、环境部署与工作流优化实战

1. ComfyUI入门&#xff1a;为什么选择节点式工作流&#xff1f; 第一次打开ComfyUI时&#xff0c;那种密密麻麻的节点连线界面确实容易让人发懵——这和我熟悉的WebUI差别太大了&#xff01;但用惯之后才发现&#xff0c;这种看似复杂的设计才是真正的生产力工具。就像从Windo…

作者头像 李华