https://github.com/caoshuai03/cs-ai-agent
cs-ai-agent 是一个基于 Spring Boot 构建的 AI 代理应用,旨在提供智能对话和情感咨询服务。 项目集成了 Spring AI 框架,利用阿里云 DashScope 模型进行 AI 对话,并通过 MyBatis-Plus 实现聊天记录的持久化管理。 此外,项目还包含了自定义的 Advisor 机制,用于实现日志记录、违禁词过滤和提示词复述等高级功能。
Science Score: 26.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
○CITATION.cff file
-
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
○DOI references
-
○Academic publication links
-
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (4.1%) to scientific vocabulary
Repository
cs-ai-agent 是一个基于 Spring Boot 构建的 AI 代理应用,旨在提供智能对话和情感咨询服务。 项目集成了 Spring AI 框架,利用阿里云 DashScope 模型进行 AI 对话,并通过 MyBatis-Plus 实现聊天记录的持久化管理。 此外,项目还包含了自定义的 Advisor 机制,用于实现日志记录、违禁词过滤和提示词复述等高级功能。
Basic Info
Statistics
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
- Releases: 0
Metadata Files
README.md
本项目是一个基于 Spring Boot 的智能 AI 代理应用,旨在提供智能对话服务。核心功能包括集成阿里云通义千问 (DashScope) 模型,实现了数据库持久化和多轮对话记忆管理。项目还支持流式调用、自定义 Advisor、RAG 检索增强、结构化输出、工具集成和 MCP 协议等功能。
🛠️ 技术栈
- 核心框架: Spring Boot 3.4.4, Spring AI
- ORM框架: MyBatis-Plus
- AI模型: 阿里云通义千问 (DashScope)
- 数据库: MySQL (聊天记忆存储), PostgreSQL + pgvector (向量数据存储)
- 工具库: Lombok, Hutool, Jsoup, iText, Knife4j
- RAG支持: 检索增强生成
- 工具集成: 文件操作(Hutool)、网络搜索(Google Search API)、网页抓取(Jsoup)、终端命令(ProcessBuilder)、资源下载(Hutool)、PDF生成(iText)
- MCP 协议: 可调用高德地图、时间获取、文生图等工具
- 智能体规划: ReAct (Reasoning and Acting) 框架,实现智能体的自主规划和工具调用能力
✨ 功能特性
1. 多轮对话记忆管理
通过 Spring AI 的 ChatMemory 实现对话状态持久化,支持会话ID跟踪和历史会话数量控制,确保上下文连贯性。 - 通过设置会话ID实现多轮对话跟踪 - 可配置历史会话数量限制 - MySQL 数据库持久化存储聊天记录
2. 自定义Advisor
- 自定义Advisor包括:
- 日志记录:记录用户输入和AI输出
- 重读机制:提示词复述提高模型能力
- 违禁词校验:确保内容合规性
3. RAG检索增强生成
通过RAG技术解决大模型幻觉问题,提升特定领域知识理解能力。文档处理流程包括:
- 文档拆分:设置最大最小长度和段落混合拆分,设置重叠字符增强片段联系
- 向量存储:使用PostgreSQL pgvector扩展
- 查询重写增强:优化用户查询提高检索效果
plainText
原始查询: 怎么脱单
重写后的查询: 单身人士提高脱单成功率的方法有哪些
- 高效近似最近邻搜索(HNSW索引):提高大规模向量数据搜索性能
4. 智能体规划能力
通过集成智能体规划能力,AI可以根据用户指令,自主进行任务分解、工具选择和执行,实现更复杂的自动化工作流程。 - 支持多步骤任务规划 - 自动选择和调用可用工具 - 灵活应对复杂场景
5. 结构化输出
通过 Spring AI 的结构化输出功能,确保AI返回结果符合预设格式。 - 定义Record类作为结构化输出模板 - 使用entity方法进行结构化处理 - 适用于报告生成等场景
6. 工具集成框架
实现了六种核心工具集成: - 文件操作(Hutool): 文件读写、目录管理等文件系统操作 - 联网搜索: 集成Google Search API实现网络信息获取 - 网页抓取: 使用Jsoup解析网页内容 - 终端命令: 通过ProcessBuilder执行系统命令 - 资源下载: 使用Hutool工具类实现网络资源下载 - PDF生成: 基于iText实现文档生成
7. MCP协议集成
通过标准化协议实现AI对外部服务的调用,包括: - 高德地图API调用 - 实时时间获取 - minimax文生图/文生视频
🚀 快速启动
环境准备
在开始之前,请确保您已安装以下环境:
- Java 21+ 或更高版本
- Maven 3.8+
- MySQL 数据库
- PostgreSQL 数据库: 需安装
pgvector扩展。
项目设置
- 克隆项目仓库:
bash
git clone https://github.com/caoshuai03/cs-ai-agent.git
cd cs-ai-agent
- 数据库配置:
项目使用了 MySQL 和 PostgreSQL + pgvector 两种数据库。
MySQL 配置 (聊天记忆存储):
更新数据库连接信息:
yaml spring: datasource: mysql: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/cs_ai_agent # <-- 连接 URL username: root # <-- 用户名 password: 123456 # <-- 密码PostgreSQL + pgvector 配置 (向量存储):
创建一个 PostgreSQL 数据库,并确保已启用
pgvector扩展,更新数据库连接信息:yaml spring: datasource: postgres: driver-class-name: org.postgresql.Driver url: jdbc:postgresql://localhost:5432/vector_db?useSSL=false # <-- 替换为 PostgreSQL 连接 URL username: postgres # <-- 用户名 password: postgres # <-- 密码
- 配置阿里云 DashScope API Key:
配置阿里云 DashScope API Key。可以将密钥直接放在配置文件中,或使用环境变量 ${DASHSCOPE_API_KEY} (推荐方式):
yaml
spring:
ai:
dashscope:
api-key: ${DASHSCOPE_API_KEY} # <-- DashScope API Key
chat:
model: qwen-plus # <-- 可选,根据需要修改模型名称
运行项目
bash
mvn clean install spring-boot:run
访问地址:
- 应用程序接口 (端口 8123):
http://localhost:8123 - API 文档 (Knife4j):
http://localhost:8123/swagger-ui.html
🤝 贡献
欢迎提交 Pull Request 或报告 Issue。
📄 许可证
本项目采用 Apache 许可证。
Owner
- Name: maila
- Login: caoshuai03
- Kind: user
- Repositories: 1
- Profile: https://github.com/caoshuai03
GitHub Events
Total
- Push event: 8
- Create event: 4
Last Year
- Push event: 8
- Create event: 4
Dependencies
- nginx alpine build
- node 20-alpine build
- cn.hutool:hutool-all 5.8.39
- com.alibaba.cloud.ai:spring-ai-alibaba-starter 1.0.0-M6.1
- com.alibaba:dashscope-sdk-java 2.19.1
- com.baomidou:mybatis-plus-spring-boot3-starter 3.5.12
- com.github.victools:jsonschema-generator 4.38.0
- com.github.xiaoymin:knife4j-openapi3-jakarta-spring-boot-starter 4.4.0
- dev.langchain4j:langchain4j-community-dashscope 1.0.0-beta2
- mysql:mysql-connector-java 8.0.28
- org.postgresql:postgresql
- org.projectlombok:lombok 1.18.32
- org.springframework.ai:spring-ai-markdown-document-reader 1.0.0-M6
- org.springframework.ai:spring-ai-pgvector-store 1.0.0-M6
- org.springframework.boot:spring-boot-starter-jdbc
- org.springframework.boot:spring-boot-starter-web
- org.springframework.boot:spring-boot-starter-test test
- @esbuild/android-arm 0.18.20 development
- @esbuild/android-arm64 0.18.20 development
- @esbuild/android-x64 0.18.20 development
- @esbuild/darwin-arm64 0.18.20 development
- @esbuild/darwin-x64 0.18.20 development
- @esbuild/freebsd-arm64 0.18.20 development
- @esbuild/freebsd-x64 0.18.20 development
- @esbuild/linux-arm 0.18.20 development
- @esbuild/linux-arm64 0.18.20 development
- @esbuild/linux-ia32 0.18.20 development
- @esbuild/linux-loong64 0.18.20 development
- @esbuild/linux-mips64el 0.18.20 development
- @esbuild/linux-ppc64 0.18.20 development
- @esbuild/linux-riscv64 0.18.20 development
- @esbuild/linux-s390x 0.18.20 development
- @esbuild/linux-x64 0.18.20 development
- @esbuild/netbsd-x64 0.18.20 development
- @esbuild/openbsd-x64 0.18.20 development
- @esbuild/sunos-x64 0.18.20 development
- @esbuild/win32-arm64 0.18.20 development
- @esbuild/win32-ia32 0.18.20 development
- @esbuild/win32-x64 0.18.20 development
- @vitejs/plugin-vue 4.6.2 development
- esbuild 0.18.20 development
- fsevents 2.3.3 development
- rollup 3.29.5 development
- vite 4.5.14 development
- @babel/helper-string-parser 7.27.1
- @babel/helper-validator-identifier 7.27.1
- @babel/parser 7.27.2
- @babel/types 7.27.1
- @jridgewell/sourcemap-codec 1.5.0
- @unhead/dom 1.11.20
- @unhead/schema 1.11.20
- @unhead/shared 1.11.20
- @unhead/ssr 1.11.20
- @unhead/vue 1.11.20
- @vue/compiler-core 3.5.14
- @vue/compiler-dom 3.5.14
- @vue/compiler-sfc 3.5.14
- @vue/compiler-ssr 3.5.14
- @vue/devtools-api 6.6.4
- @vue/reactivity 3.5.14
- @vue/runtime-core 3.5.14
- @vue/runtime-dom 3.5.14
- @vue/server-renderer 3.5.14
- @vue/shared 3.5.14
- @vueuse/head 2.0.0
- asynckit 0.4.0
- axios 1.9.0
- call-bind-apply-helpers 1.0.2
- combined-stream 1.0.8
- csstype 3.1.3
- delayed-stream 1.0.0
- dunder-proto 1.0.1
- entities 4.5.0
- es-define-property 1.0.1
- es-errors 1.3.0
- es-object-atoms 1.1.1
- es-set-tostringtag 2.1.0
- estree-walker 2.0.2
- follow-redirects 1.15.9
- form-data 4.0.2
- function-bind 1.1.2
- get-intrinsic 1.3.0
- get-proto 1.0.1
- gopd 1.2.0
- has-symbols 1.1.0
- has-tostringtag 1.0.2
- hasown 2.0.2
- hookable 5.5.3
- magic-string 0.30.17
- math-intrinsics 1.1.0
- mime-db 1.52.0
- mime-types 2.1.35
- nanoid 3.3.11
- packrup 0.1.2
- picocolors 1.1.1
- postcss 8.5.3
- proxy-from-env 1.1.0
- source-map-js 1.2.1
- unhead 1.11.20
- vue 3.5.14
- vue-router 4.5.1
- zhead 2.2.4
- @vitejs/plugin-vue ^4.1.0 development
- vite ^4.3.9 development
- @vueuse/head ^2.0.0
- axios ^1.3.6
- vue ^3.2.47
- vue-router ^4.1.6