当前位置: 首页 > news >正文

Spring AI Alibaba使用教程

准备:

1.环境要求:

  • JDK 17+
  • Maven 3.8+
  • 选择你的 LLM 提供商并获取 API-KEY(如阿里云百炼的 DashScope)

2.mave 依赖:

<dependencies> <!-- Spring AI Alibaba Agent Framework --> <dependency> <groupId>com.alibaba.cloud.ai</groupId> <artifactId>spring-ai-alibaba-agent-framework</artifactId> <version>1.1.0.0-RC2</version> </dependency> <!-- DashScope ChatModel 支持(如果使用其他模型,请跳转 Spring AI 文档选择对应的 starter) --> <dependency> <groupId>com.alibaba.cloud.ai</groupId> <artifactId>spring-ai-alibaba-starter-dashscope</artifactId> <version>1.1.0.0-RC2</version> </dependency> </dependencies>

一,官网信息

1.官网文档地址:https://java2ai.com/

2.阿里巴巴讨论群:https://github.com/alibaba/spring-ai-alibaba/discussions

二,ReactAgent 的入门

1.ReactAgent 的核心执行流程:

1.DashScopeApi 介绍

1. 介绍:DashScopeApi 是阿里云推出的一个 大模型(LLM)统一调用接口/平台,主要用于调用通义千问Qwen)及相关 AI 能力,让开发者可以方便地在应用中接入大模型能力。

2.常用的类型名

apikey

密钥

baseUrl

api

实例代码:

package com.spring.ai.alibaba.agent.springaialibabaagent.test; import com.alibaba.cloud.ai.dashscope.api.DashScopeApi; import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel; import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions; import org.springframework.ai.chat.model.ChatModel; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 基础配置类 */ @Configuration public class ReactAgentModelChatConfig { /** * 配置DashScopeApi * @return */ @Bean(name = "dashScopeApiTest") public DashScopeApi dashScopeApiTest() { return DashScopeApi.builder() .apiKey("sk-9ce4989be07147368bc9760265f82e89") .build(); } /** * 配置DashScopeChatModel * @param dashScopeApi * @return */ @Bean(name = "dashScopeChatModelTest") public ChatModel dashScopeChatModelTest(DashScopeApi dashScopeApi) { return DashScopeChatModel.builder() .dashScopeApi(dashScopeApi) .defaultOptions(DashScopeChatOptions.builder() .maxToken(1024) //token数 .temperature(0.7) //温度 .build()) .build(); } }

2.ReactAgent

简介:ReactAgent 是 Spring AI Alibaba 中提供的一个核心 Agent 实现类,它实现了经典的 ReAct 模式(Reasoning+Acting)模型

  • Reason(推理/思考):分析当前情况,规划下一步
  • Act(行动):调用工具(Tool)获取外部信息

常用的构建和使用方法(基于 builder 模式)

常用的方法

常用的构建和使用方法(基于 builder 模式)

方法 / 属性

作用

.name

Agent 的名字

.model(ChatModel)

注入大模型

.tools

添加工具(支持 FunctionToolCallback、自定义 Tool 等)

.hooks

添加钩子(如 ModelCallLimitHook、LoggingHook、HumanInTheLoopHook)

.systemPrompt(String)

.instruction(String)

系统提示词

.maxIterations(int)

最大迭代轮次(防止无限循环,类似 runLimit)

.savers(ChatMemory)

对话记忆(短期/长期记忆,支持向量存储)

.outputSchema

使用BeanOutputConverter生成时提供类型安全,手动提供字符串时灵活性高,适合动态或复杂的输出格式

.Interceptors

interceptors 提供更粒度的控制,可以拦截和修改模型调用和工具执行。

.outputType

类型安全,适合结构固定的场景(推荐

3.测试学习代码地址:

Spring_AI_Alibaba_ReactAgent_test_demo:ReactAgent的测试项目用于起步学习 - AtomGit | GitCode

http://www.jsqmd.com/news/290278/

相关文章:

  • Cirris Easy-Touch Pro扩展盒C150-ETPA高压电缆测试仪
  • 我觉得 OI 就应该让机器人来打
  • 实用指南:Vue3.x —— Vuex 状态管理
  • 拾光筑梦 向光而行——至圣集团合作伙伴答谢会在昆圆满举办
  • 小学生的题:求桌子多高
  • 掌握技巧:彻底消除错误代码0x80004005带来的困扰
  • 3.3V驱动MOS方法——稳压二极管+NMOS
  • 【两阶段鲁棒微网】【不确定性】基于关键场景辨别算法的两阶段鲁棒微网优化调度附Matlab代码
  • ant -vue a-table去掉表头
  • 【裂纹检测】检测和标记图片中的裂缝附Matlab代码
  • Tulsimer树脂在锂电高盐锂液深度除杂中的技术实践与应用
  • 题解:AT_agc058_f [AGC058F] Authentic Tree DP
  • 别再把“数字化管理”当PPT了!18.3%的权重背后,藏着你的降本增效密码
  • nodejs基于vue框架的宠物猫商城售卖管理系_eq667
  • nodejs基于vue框架的家政服务系统的设计与实现_5o0if
  • nodejs基于vue网上订餐系统的设计与实现_i23b2
  • nodejs基于vue乡镇农村居民政务管理系统_93034
  • 低代码:设备管理数字化的故障快速响应密码
  • 如何启动Carte,如何使用Carte
  • Pentaho 和他的五位成员(五灵威力) kettle spoon pancarte kitchen (pdi的前生今世)
  • javafx程序启动cmd.exe调用其他软件或者脚本指令
  • 如何解决 Spoon 与 Carte 互联的 UTF-8 编码问题
  • spoon如何连接carte如何将.ktr任务或者.kjb任务提交个远程carte服务让,carte的服务端来执行 etl脚本的任务呢?
  • Pentaho Data Integration:Spoon与Carte的对比
  • mast3r-smf测试 - MKT
  • 备份 还原 系统 数据保护
  • 中小厂 AI 招聘隐藏要求:除了技术,这 2 点更关键
  • nodejs基于vue的教学质量评价管理系统的设计与实现 评教系统39j06
  • 二叉树递归实现
  • nodejs基于vue的教师科研项目申报信息管理系统的设计与实现_c7z6m