简单、强大、开箱即用的 Spring Boot 3 AI 大模型开发框架
- Java: 17+
- Maven: 3.6.3+
- Spring Boot: 3.2+
- Gitee: https://gitee.com/hongxinge/think-ai4j
- GitHub: https://github.com/hongxinge/ThinkAi4j
- 开箱即用 - Ollama 本地模型零配置,云模型 1 行配置即可使用
- 多模型支持 - 豆包、通义千问、智谱、百度、腾讯、Kimi、DeepSeek、MiniMax、OpenAI 等所有 OpenAI 兼容模型
- 自由切换 - 配置或代码中随时切换模型,无需改业务代码
- 流式输出 - 支持 Flux、SSE 多种流式格式
- 对话记忆 - 内置内存记忆,支持 Redis 持久化
- 工具调用 - @AiTool 注解即可让 AI 调用你的方法
- RAG 增强 - 文档问答、知识库检索增强生成
- Agent 框架 - 智能代理,支持多轮工具调用、长期记忆、多Agent协作
- 内置 Skill - 文件操作、HTTP请求、数据库查询、时间日期、记忆管理等5大Skill
- OpenAI 标准 - 完全兼容 OpenAI API 规范
- MIT 协议 - 完全免费,可自由商用
ThinkAi4j 采用通用兼容 + 特殊适配的设计:
- 1 个通用模块
think-ai4j-provider-openai-compat- 支持所有 OpenAI 兼容 API 的大模型 - 配置即接入 - 新增模型只需修改配置文件,无需编写代码
| 模型 | 提供商名称 | 兼容 OpenAI | 状态 |
|---|---|---|---|
| 豆包 (Doubao) | doubao |
✅ 是 | ✅ |
| 百度文心 (Qianfan) | qianfan |
✅ 是 | ✅ |
| 腾讯混元 (Hunyuan) | hunyuan |
✅ 是 | ✅ |
| Kimi (Moonshot) | moonshot |
✅ 是 | ✅ |
| 智谱 GLM | glm |
✅ 是 | ✅ |
| MiniMax | minimax |
✅ 是 | ✅ |
| DeepSeek | deepseek |
✅ 是 | ✅ |
| 通义千问 (Qwen) | qwen |
✅ 是 | ✅ |
| Ollama 本地 | ollama |
✅ 是 | ✅ |
| OpenAI GPT | openai |
✅ 是 | ✅ |
所有符合 OpenAI API 规范的模型都可通过配置接入
只需在你的 Spring Boot 项目 pom.xml 中添加:
<!-- Spring Boot Starter(自动装配) -->
<dependency>
<groupId>com.hongxinge</groupId>
<artifactId>think-ai4j-spring-boot-starter</artifactId>
<version>1.0.1</version>
</dependency>框架已内置 Spring Boot Starter,引入后自动生效,无需额外配置。Maven 会自动从中央仓库下载,零配置即可使用。
git clone https://gitee.com/hongxinge/think-ai4j.git
# 或 GitHub: https://github.com/hongxinge/ThinkAi4j.git
cd think-ai4j
# 编译并安装到本地 Maven 仓库
# Windows (PowerShell)
$env:JAVA_HOME="你的JDK路径"
mvn clean install -DskipTests然后在你的项目 pom.xml 中引入依赖(与方式一相同):
<dependency>
<groupId>com.hongxinge</groupId>
<artifactId>think-ai4j-spring-boot-starter</artifactId>
<version>1.0.1</version>
</dependency>
mvn clean install会将 1.0.1 版本安装到你本地的 Maven 仓库,之后你的项目就可以正常引用了。
think:
ai:
default-provider: doubao
compat:
providers:
- name: doubao
baseUrl: https://ark.cn-beijing.volces.com/api/v3
apiKey: 你的API密钥
model: 你的模型ID
# 可同时配置多个模型
# - name: moonshot
# baseUrl: https://api.moonshot.cn/v1
# apiKey: 你的API密钥
# model: moonshot-v1-8k
# - name: glm
# baseUrl: https://open.bigmodel.cn/api/paas/v4
# apiKey: 你的API密钥
# model: glm-4
httpClient:
connectionPool:
max-idle-connections: 50
keep-alive-minutes: 5
timeout:
connect-seconds: 30
read-seconds: 60
write-seconds: 30
memory:
type: memory # memory=内存 | redis=持久化
max-messages: 20Ollama 本地模型零配置:只要本地安装了 Ollama(默认端口 11434),无需任何配置即可使用。
@Autowired
private AiChat chat;
String result = chat.ask("你好");String result = chat.system("你是Java专家").ask("如何设计单例模式?");@GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> stream(@RequestParam String q) {
return chat.stream(q);
}String result = chat.provider("glm").ask("你好");think:
ai:
memory:
type: redis
max-messages: 50
ttl-minutes: 120@Autowired
private ChatMemory memory;
memory.addMessage("user-123", AiMessage.user("我叫小明"));
List<AiMessage> history = memory.getMessages("user-123");@Component
public class WeatherTool {
@AiTool("查询天气")
public String getWeather(
@ToolParam(description = "城市名称") String city
) {
return "晴天,25度";
}
}@Autowired
private RagPipeline ragPipeline;
List<Document> docs = List.of(
new Document("公司规定年假为15天"),
new Document("加班费按每小时100元计算")
);
ragPipeline.ingest(docs);
String answer = ragPipeline.query("年假有多少天?");Agent agent = new Agent("助手", "你是一个专业的助手", chat)
.addToolBean(new WeatherTool())
.addToolBean(new SearchTool());
String result = agent.execute("北京天气如何?");ChatMemory memory = new InMemoryChatMemory();
AgentLongTermMemory longTermMemory = new AgentLongTermMemory("assistant-1", memory);
// 记住关键信息
longTermMemory.rememberFact("用户姓名:张三");
longTermMemory.rememberFact("用户偏好:Java开发");
Agent agent = new Agent("助手", "你是专业助手", chat)
.longTermMemory(longTermMemory);AgentBus bus = new AgentBus();
bus.register("研究员", researcherAgent);
bus.register("写手", writerAgent);
bus.register("审核员", reviewerAgent);
// 链式执行:研究员->写手->审核员
String report = bus.chainExecute(
List.of("研究员", "写手", "审核员"),
"研究AI趋势并写报告"
);
// 并行执行
String results = bus.parallelExecute(Map.of(
"研究员", "研究技术趋势",
"写手", "写文章摘要"
));FileSkill fileSkill = new FileSkill("/workspace");
// 读写文件
fileSkill.writeFile("notes.txt", "重要笔记");
String content = fileSkill.readFile("notes.txt");
// 列出目录
String dirList = fileSkill.listDirectory("");HttpSkill httpSkill = new HttpSkill()
.setDefaultHeader("Authorization", "Bearer token");
// GET 请求
String response = httpSkill.httpGet("https://api.example.com/data", null);
// POST 请求
String postResult = httpSkill.httpPost(
"https://api.example.com/submit",
null,
"{\"key\": \"value\"}"
);// 使用 H2 内存数据库示例
DatabaseSkill dbSkill = new DatabaseSkill(
"jdbc:h2:mem:testdb", "sa", ""
);
// SQL 查询
String results = dbSkill.executeQuery("SELECT * FROM users");
// 获取表列表
String tables = dbSkill.listTables();
// 查看表结构
String schema = dbSkill.describeTable("users");TimeSkill timeSkill = new TimeSkill();
// 获取当前时间
String now = timeSkill.getCurrentDateTime("Asia/Shanghai");
String date = timeSkill.getCurrentDate();
String time = timeSkill.getCurrentTime();
// 格式化时间戳
String formatted = timeSkill.formatTimestamp(1700000000, "yyyy-MM-dd HH:mm:ss");MemorySkill memorySkill = new MemorySkill();
// 记住信息
memorySkill.remember("用户名", "张三");
memorySkill.remember("年龄", "25");
// 查询记忆
String name = memorySkill.recall("用户名");
// 查询所有记忆
String allMemories = memorySkill.recall(null);
// 忘记信息
memorySkill.forget("年龄");
// 清空记忆
memorySkill.clearMemory();| 方法 | 说明 | 示例 |
|---|---|---|
chat.ask(q) |
同步对话 | chat.ask("你好") |
chat.system(s).ask(q) |
带系统提示词 | chat.system("你是专家").ask("问题") |
chat.provider(p).ask(q) |
指定模型 | chat.provider("glm").ask("问题") |
chat.stream(q) |
流式输出(Flux) | chat.stream("问题") |
chat.temperature(t).ask(q) |
控制创造性 | chat.temperature(0.7).ask("写诗") |
think-ai4j/
├── think-ai4j-core/ # 核心模块
├── think-ai4j-provider-openai-compat/ # 通用兼容模块(支持所有OpenAI格式模型)
├── think-ai4j-memory/ # 内存记忆
├── think-ai4j-memory-redis/ # Redis 持久化记忆
├── think-ai4j-tool/ # 工具调用
├── think-ai4j-skill/ # 内置Skill(文件、HTTP、数据库、时间、记忆)
├── think-ai4j-rag/ # RAG 检索增强
├── think-ai4j-agent/ # Agent 框架(支持长期记忆、多Agent协作)
├── think-ai4j-observability/ # 可观测性/指标采集
├── think-ai4j-store-pgvector/ # PgVector 向量存储
├── think-ai4j-spring-boot-starter/ # Spring Boot 自动配置
├── think-ai4j-example/ # 示例项目
└── think-ai4j-test/ # 测试模块(172个测试用例,全量覆盖)
普通用户直接使用 Maven 依赖即可,无需克隆源码。以下适合二次开发/贡献者。
环境要求:Java 17+、Maven 3.6.3+、Spring Boot 3.2+
set JAVA_HOME=D:\JavaSdk\sdk-17
D:\maven\apache-maven-3.9.9\bin\mvn.cmd clean installcd think-ai4j-example
set JAVA_HOME=D:\JavaSdk\sdk-17
D:\maven\apache-maven-3.9.9\bin\mvn.cmd spring-boot:run然后访问: