砍材农夫砍材农夫
  • 微信记账小程序
  • java
  • redis
  • mysql
  • 场景类
  • 框架类
  • vuepress搭建
  • hexo搭建
  • 云图
  • llm wiki

    • 基于karpathy
    • gradle
  • 常用工具

    • git
    • gradle
    • Zadig
    • it-tools
    • 开源推荐
    • curl
  • 大前端

    • nodejs
    • npm
    • webpack
    • 微信
    • 正则
    • uniapp
    • app
  • java

    • java基础
    • jdk体系
    • jvm
    • spring
    • spring_cloud
    • spring_boot
    • 分库分表
    • zookeeper
  • python

    • python基础
    • python高级
    • python框架
  • 算法

    • 算法
  • 网关

    • spring_cloud_gateway
    • openresty
  • 高可用

    • 秒杀
    • 分布式
    • 缓存一致
  • MQ

    • MQ
    • rabbitMQ
    • rocketMQ
    • kafka
  • 其它

    • 设计模式
    • 领域驱动(ddd)
  • 关系型数据库

    • mysql5.0
    • mysql8.0
  • 非关系型数据库

    • redis
    • mongoDB
  • 分布式/其他

    • ShardingSphere
    • 区块链
  • 向量数据库

    • M3E
    • OPEN AI
  • Jmeter
  • fiddler
  • wireshark
  • AI入门
  • AI大模型
  • AI插件
  • AI集成框架
  • 相关算法
  • AI训练师
  • 量化交易
  • gitee
  • github
  • infoq
  • osc
  • 砍材工具
  • 关于
  • 相关运营
  • docker
  • k8s
  • devops
  • nginx
  • 元宇宙
  • 区块链
  • 物联网
  • linux
  • webrtc
  • web3.0
  • gitee
  • github
  • infoq
  • osc
  • 砍材工具
  • 关于
  • 中考
  • 投资
  • 保险
  • 思
  • 微信记账小程序
  • java
  • redis
  • mysql
  • 场景类
  • 框架类
  • vuepress搭建
  • hexo搭建
  • 云图
  • llm wiki

    • 基于karpathy
    • gradle
  • 常用工具

    • git
    • gradle
    • Zadig
    • it-tools
    • 开源推荐
    • curl
  • 大前端

    • nodejs
    • npm
    • webpack
    • 微信
    • 正则
    • uniapp
    • app
  • java

    • java基础
    • jdk体系
    • jvm
    • spring
    • spring_cloud
    • spring_boot
    • 分库分表
    • zookeeper
  • python

    • python基础
    • python高级
    • python框架
  • 算法

    • 算法
  • 网关

    • spring_cloud_gateway
    • openresty
  • 高可用

    • 秒杀
    • 分布式
    • 缓存一致
  • MQ

    • MQ
    • rabbitMQ
    • rocketMQ
    • kafka
  • 其它

    • 设计模式
    • 领域驱动(ddd)
  • 关系型数据库

    • mysql5.0
    • mysql8.0
  • 非关系型数据库

    • redis
    • mongoDB
  • 分布式/其他

    • ShardingSphere
    • 区块链
  • 向量数据库

    • M3E
    • OPEN AI
  • Jmeter
  • fiddler
  • wireshark
  • AI入门
  • AI大模型
  • AI插件
  • AI集成框架
  • 相关算法
  • AI训练师
  • 量化交易
  • gitee
  • github
  • infoq
  • osc
  • 砍材工具
  • 关于
  • 相关运营
  • docker
  • k8s
  • devops
  • nginx
  • 元宇宙
  • 区块链
  • 物联网
  • linux
  • webrtc
  • web3.0
  • gitee
  • github
  • infoq
  • osc
  • 砍材工具
  • 关于
  • 中考
  • 投资
  • 保险
  • 思
  • 首页

    • 介绍
    • 框架比较
  • spring-ai

    • spring-ai 第一步入门
    • spring-ai 第二提示词介绍
    • spring-ai 第三结构化输出
    • spring-ai 第四多模态API
    • spring-ai 第五模型介绍
    • spring-ai 第六模型介绍-聊天模型
    • spring-ai 第七模型介绍-向量模型
    • spring-ai 第八模型介绍-图像模型
    • spring-ai 第九模型介绍-聊天记录
    • spring-ai 第十tool调用
    • spring-ai 第十一 MCP server 调用入门(stdio协议)
    • spring-ai 第十二 MCP server 调用入门(http协议)
    • spring-ai 第十三 MCP client 调用入门
    • spring-ai 第十四 MCP和tool区别
    • spring-ai 第十五 RAG
    • spring-ai 第十五一节 RAG(加更ETL)
    • spring-ai 第十六 向量数据库
  • spring-ai-alibaba

    • spring-ai-alibaba 第一搭建demo
    • spring-ai-alibaba 第二相关概念
    • spring-ai-alibaba 第三 ReAct Agent
    • spring-ai-alibaba 第四 Hooks 和 Interceptors
  • spring-skills

    • 搭建demo
    • 相关概念
    • demo搭建springAIAlibaba
  • AgentScopeJava
    • 搭建demo
    • demo搭建springAIAlibaba
  • langchain

    • v1.0

      • 如何升级版本
      • 环境搭建
      • 架构结构
      • 模型
      • langchain核心
      • langchain-model
      • langchain提示词模板
      • langchain语言模型
      • 调用本地大模型
      • langChain中Chain链使用
      • langChain流程编排
      • langChain服务部署与链路监控
      • langChain消息管理与聊天历史存储
      • langchain_loader
      • langchain中RAG
      • langchain中Agent
  • dify

    • dify入门和安装
    • dify模型配置和验证
  • n8n

    • n8n入门和安装
  • coze

    • coze入门和安装
  • spring-ai-alibaba 第三 ReAct Agent
    • 官网地址
    • ReAct Agent 核心实现
    • demo地址
      • 1. 定义工具类 (Tools)
      • 2. 配置 ReactAgent
      • 3. 提供 HTTP 接口
      • 4. 测试

spring-ai-alibaba 第三 ReAct Agent

官网地址

官网地址[https://java2ai.com/]

ReAct Agent 核心实现

ReAct Agent 的核心是"推理(Reasoning)"与"行动(Acting)"的循环

demo地址

demo源码[https://gitee.com/kcnf_open/spring-ai-sample/tree/master/ali-ai/ali-ai-sample1]

1. 定义工具类 (Tools)

package com.kcnf.ai.tool;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.function.Function;

@Component
public class MenuRecommendTool implements Function<MenuRecommendRequest, String> {

    @Override
    public String apply(MenuRecommendRequest taste) {
        if (taste == null || !StringUtils.hasLength(taste.getTaste())) {
            return "请告诉我你的口味偏好,比如:辣、甜、酸。";
        }

        if (taste.getTaste().contains("辣")) {
            return "🌶️ 推荐菜单:麻辣香锅、水煮牛肉、酸辣粉。";
        } else if (taste.getTaste().contains("甜")) {
            return "🍰 推荐菜单:糖醋排骨、拔丝地瓜、杨枝甘露。";
        } else if (taste.getTaste().contains("清淡")) {
            return "🥬 推荐菜单:白灼菜心、山药排骨汤、清蒸鲈鱼。";
        } else {
            return "🥢 推荐菜单:宫保鸡丁、番茄炒蛋、家常豆腐。";
        }
    }
}

2. 配置 ReactAgent

package com.kcnf.ai.config;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.graph.agent.ReactAgent;
import com.alibaba.cloud.ai.graph.checkpoint.savers.MemorySaver;
import com.kcnf.ai.tool.MenuRecommendRequest;
import com.kcnf.ai.tool.MenuRecommendTool;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.function.FunctionToolCallback;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AgentConfig {

    @Value("${spring.ai.dashscope.api-key}")
    private String apiKey;

    @Bean
    public DashScopeChatModel chatModel() {
        DashScopeApi dashScopeApi = DashScopeApi.builder()
                .apiKey(apiKey)
                .build();
        return DashScopeChatModel.builder()
                .dashScopeApi(dashScopeApi)
                .build();
    }


    @Bean
    public ToolCallback menuRecommendToolCallback(MenuRecommendTool menuTool) {
        return FunctionToolCallback.builder("recommend_menu", menuTool)
                .description("根据用户的口味偏好推荐合适的菜品")
                .inputType(MenuRecommendRequest.class)
                .build();
    }

    @Bean
    public ReactAgent reactAgent(DashScopeChatModel chatModel, ToolCallback menuRecommendToolCallback) {
        return ReactAgent.builder()
                .name("点餐助手")
                .model(chatModel)
                .tools(menuRecommendToolCallback)
                .systemPrompt("你是一个友好的餐厅点餐助手。你有推荐菜品的工具。当用户询问吃什么时,你需要调用工具来推荐。如果用户没有说明口味,请主动询问。")
                .saver(new MemorySaver())
                .build();
    }
}

3. 提供 HTTP 接口

package com.kcnf.ai.controller;


import com.alibaba.cloud.ai.graph.RunnableConfig;
import com.alibaba.cloud.ai.graph.agent.ReactAgent;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ChatController {

    @Autowired
    private ReactAgent reactAgent;

    @SneakyThrows
    @GetMapping("/ai/chat")
    public String chat(@RequestParam String message, @RequestParam(defaultValue = "default-user") String userId) {
        // 通过 threadId 隔离不同用户的会话,实现连续对话
        RunnableConfig config = RunnableConfig.builder()
                .threadId(userId)
                .build();

        // 调用 Agent 并返回结果
        return reactAgent.call(message, config).getText();
    }
}

4. 测试

http://localhost:8081/ai/chat?message=%E6%88%91%E6%83%B3%E5%90%83%E7%82%B9%E8%BE%A3%E7%9A%84&userId=1001

Image text

最近更新: 2026/4/16 23:27
Contributors: kcnf
Prev
spring-ai-alibaba 第二相关概念
Next
spring-ai-alibaba 第四 Hooks 和 Interceptors