框架概述
ThinkAi4j 是一个简单、强大、开箱即用的 Spring Boot 3 AI 大模型开发框架。核心优势:开箱即用(Ollama 零配置)、多模型支持、自由切换、通用兼容架构。
开箱即用
Ollama 本地模型零配置,云模型 1 行配置即可使用
多模型支持
10 种主流模型,所有 OpenAI 兼容模型
自由切换
配置或代码中随时切换模型,无需改业务代码
流式输出
支持 Flux<String>、SSE 多种流式格式
对话记忆
内置内存记忆,支持 Redis 持久化
工具调用
@AiTool 注解即可让 AI 调用你的方法
架构优势
通用兼容 + 特殊适配
1 个通用模块 think-ai4j-provider-openai-compat 支持所有 OpenAI 兼容 API 的大模型。配置即接入 —— 新增模型只需修改配置文件,无需编写代码。
快速开始
环境要求
- Java 17+
- Maven 3.6.3+
- Spring Boot 3.2+
引入依赖
<dependency>
<groupId>com.thinkai4j</groupId>
<artifactId>think-ai4j-spring-boot-starter</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
配置模型
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
memory:
type: memory # memory=内存 | redis=持久化
max-messages: 20
🎉 Ollama 本地模型零配置
只要本地安装了 Ollama(默认端口 11434),无需任何配置即可使用。
支持的 AI 模型
| 模型 | 提供商名称 | 兼容 OpenAI |
|---|---|---|
| 豆包 (Doubao) | doubao | ✅ |
| 百度文心 (Qianfan) | qianfan | ✅ |
| 腾讯混元 (Hunyuan) | hunyuan | ✅ |
| Kimi (Moonshot) | moonshot | ✅ |
| 智谱 GLM | glm | ✅ |
| MiniMax | minimax | ✅ |
| DeepSeek | deepseek | ✅ |
| 通义千问 (Qwen) | qwen | ✅ |
| Ollama 本地 | ollama | ✅ |
| OpenAI GPT | openai | ✅ |
对话功能
简单对话
@Autowired
private AiChat chat;
String result = chat.ask("你好");
带系统提示词
String result = chat.system("你是Java专家").ask("如何设计单例模式?");
流式输出(SSE)
@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("你好");
对话记忆(Redis 持久化)
@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度";
}
}
RAG 文档问答
@Autowired
private RagPipeline ragPipeline;
List<Document> docs = List.of(
new Document("公司规定年假为15天"),
new Document("加班费按每小时100元计算")
);
ragPipeline.ingest(docs);
String answer = ragPipeline.query("年假有多少天?");
Agent 智能代理
基本 Agent
Agent agent = new Agent("助手", "你是一个专业的助手", chat)
.addToolBean(new WeatherTool())
.addToolBean(new SearchTool());
String result = agent.execute("北京天气如何?");
Agent 长期记忆
ChatMemory memory = new InMemoryChatMemory();
AgentLongTermMemory longTermMemory = new AgentLongTermMemory("assistant-1", memory);
longTermMemory.rememberFact("用户姓名:张三");
longTermMemory.rememberFact("用户偏好:Java开发");
Agent agent = new Agent("助手", "你是专业助手", chat)
.longTermMemory(longTermMemory);
多 Agent 协作
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(
"研究员", "研究技术趋势",
"写手", "写文章摘要"
));
内置 5 大 Skill
📁 FileSkill - 文件操作
读写文件、列出目录。支持 allowedDirs 限定访问范围。
FileSkill fileSkill = new FileSkill("/workspace");
fileSkill.writeFile("notes.txt", "重要笔记");
String content = fileSkill.readFile("notes.txt");🌐 HttpSkill - HTTP 请求
GET / POST 请求,支持自定义 Header。
HttpSkill httpSkill = new HttpSkill()
.setDefaultHeader("Authorization", "Bearer token");
String response = httpSkill.httpGet("https://api.example.com/data", null);🗄️ DatabaseSkill - 数据库查询
SQL 查询、获取表列表、查看表结构。
DatabaseSkill dbSkill = new DatabaseSkill(
"jdbc:h2:mem:testdb", "sa", "");
String results = dbSkill.executeQuery("SELECT * FROM users");🕐 TimeSkill - 时间日期
获取当前时间、日期、时区转换、格式化时间戳。
TimeSkill timeSkill = new TimeSkill();
String now = timeSkill.getCurrentDateTime("Asia/Shanghai");🧠 MemorySkill - 记忆管理
记住/查询/忘记信息、清空记忆。
MemorySkill memorySkill = new MemorySkill();
memorySkill.remember("用户名", "张三");
String name = memorySkill.recall("用户名");项目结构
think-ai4j/
├── think-ai4j-core/ # 核心模块
├── think-ai4j-provider-openai-compat/ # 通用兼容模块
├── think-ai4j-memory/ # 内存记忆
├── think-ai4j-memory-redis/ # Redis 持久化记忆
├── think-ai4j-tool/ # 工具调用
├── think-ai4j-skill/ # 内置 Skill
├── think-ai4j-rag/ # RAG 检索增强
├── think-ai4j-agent/ # Agent 框架
├── think-ai4j-observability/ # 可观测性
├── think-ai4j-store-pgvector/ # PgVector 向量存储
├── think-ai4j-spring-boot-starter/ # Spring Boot 自动配置
├── think-ai4j-example/ # 示例项目
└── think-ai4j-test/ # 测试模块(147 个测试用例)
API 文档
| 方法 | 说明 | 示例 |
|---|---|---|
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("写诗") |
仓库地址
| 平台 | 地址 |
|---|---|
| Gitee | https://gitee.com/hongxinge/think-ai4j |
| GitHub | https://github.com/hongxinge/ThinkAi4j |