# react-ai-agent **Repository Path**: newitman/react-ai-agent ## Basic Information - **Project Name**: react-ai-agent - **Description**: spring ai 继承 react提示词模式 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-23 - **Last Updated**: 2026-02-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # ReAct Spring AI Agent 基于 Spring AI 的 ReAct(Reasoning + Acting)模式智能代理系统。 ## 项目说明 本项目是一个基于 Spring AI 0.8.1 的 ReAct MVP 实现,展示了如何使用 Spring AI 的工具调用机制来实现 ReAct 模式,**无需手动循环调用工具**。 ## 核心特性 - ✅ 使用 Spring AI 的工具调用机制,自动处理工具调用循环 - ✅ 支持多种工具:计算器、时间查询、知识搜索等 - ✅ 支持多轮对话记忆(简化实现) - ✅ 使用提示词模板进行结构化推理 - ✅ 完全基于 Spring AI 官方推荐的最佳实践 ## 项目结构 ``` src/main/java/com/example/reactagent/ ├── ReactAgentApplication.java # 主应用类 ├── config/ │ └── ReActConfig.java # ReAct 配置(ChatClient, PromptTemplate) ├── tools/ │ └── ReActTools.java # 工具定义(使用 FunctionCallback) ├── service/ │ └── ReActAgentService.java # ReAct 服务层 └── controller/ └── ReActController.java # REST API 控制器 src/main/resources/ ├── application.yml # 应用配置 └── prompts/ └── react-system.st # ReAct 提示词模板 ``` ## 快速开始 ### 1. 配置 API Key 在 `application.yml` 中配置你的 OpenAI API Key: ```yaml spring: ai: openai: api-key: ${OPENAI_API_KEY:your-api-key-here} ``` ### 2. 运行应用 ```bash mvn spring-boot:run ``` ### 3. 测试 API ```bash # 健康检查 curl http://localhost:8080/api/react/test # 执行 ReAct 推理 curl -X POST http://localhost:8080/api/react/execute \ -H "Content-Type: application/json" \ -d '{"question": "计算一下 (25 + 17) * 3 等于多少?"}' ``` ## API 端点 - `GET /api/react/test` - 健康检查 - `POST /api/react/execute` - 执行 ReAct 推理(单次对话) - `POST /api/react/execute-with-memory` - 执行 ReAct 推理(支持多轮对话) - `POST /api/react/execute-structured` - 使用结构化提示词的 ReAct 推理 ## 工具说明 项目定义了以下工具: 1. **getCurrentDateTime** - 获取当前日期和时间 2. **calculate** - 计算数学表达式(支持加减乘除和乘方) 3. **search** - 查询信息(模拟知识库搜索) 4. **checkCompletion** - 检查任务完成状态 ## 技术栈 - Spring Boot 3.2.0 - Spring AI 1.0.0-M6(已升级,支持完整的工具调用 API) - Java 17 - Lombok ## 版本说明 ✅ **已升级到 Spring AI 1.0.0-M6** 项目已从 Spring AI 0.8.1 升级到 1.0.0-M6,原因: - 0.8.1 版本缺少 `ChatClient` 等关键 API - 1.0.0-M6 提供了完整的工具调用支持 - 使用 `FunctionToolCallback` 定义工具,API 更稳定 ## 注意事项 - 项目使用 Spring AI 1.0.0-M6(里程碑版本) - 如果遇到依赖下载问题,确保 Maven 配置了正确的仓库 - 某些空值安全警告是正常的,不影响编译和运行 ## 参考文档 - [Spring AI 工具调用文档](https://docs.spring.io/spring-ai/reference/api/tools.html) - [Spring AI 提示词模板](https://docs.spring.io/spring-ai/reference/api/prompt.html) - [Spring AI 对话记忆](https://docs.spring.io/spring-ai/reference/api/chat-memory.html) - [Spring AI Advisors](https://docs.spring.io/spring-ai/reference/api/advisors.html) ## 许可证 MIT License