|
| 1 | +# Spring AI Alibaba DataAgent |
| 2 | + |
| 3 | +## 项目简介 |
| 4 | + |
| 5 | +这是一个基于Spring AI Alibaba的自然语言转SQL项目,能让你用自然语言直接查询数据库,不需要写复杂的SQL。 |
| 6 | + |
| 7 | +## 项目结构 |
| 8 | + |
| 9 | +这个项目分为三个部分: |
| 10 | + |
| 11 | +``` |
| 12 | +spring-ai-alibaba-nl2sql/ |
| 13 | +├── spring-ai-alibaba-nl2sql-management # 管理端(可直接启动的Web应用) |
| 14 | +├── spring-ai-alibaba-nl2sql-chat # 核心功能(不能独立启动,供集成使用) |
| 15 | +└── spring-ai-alibaba-nl2sql-common # 公共代码 |
| 16 | +``` |
| 17 | + |
| 18 | +## 快速启动 |
| 19 | +项目进行本地测试是在spring-ai-alibaba-nl2sql-management中进行 |
| 20 | + |
| 21 | +### 1. 配置数据库 |
| 22 | + |
| 23 | +修改 `spring-ai-alibaba-nl2sql-management/src/main/resources/application.yml`: |
| 24 | + |
| 25 | +.sql文件在spring-ai-alibaba-example项目仓库中(https://github.com/springaialibaba/spring-ai-alibaba-examples/tree/main/spring-ai-alibaba-nl2sql-example) |
| 26 | + |
| 27 | +请直接导入对应的insert.sql文件和schema.sql文件,并进行配置后启动spring-ai-alibaba-nl2sql-management |
| 28 | + |
| 29 | +```yaml |
| 30 | +chatBi: |
| 31 | + dbConfig: |
| 32 | + url: jdbc:mysql://localhost:3306/你的数据库?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8 |
| 33 | + username: 你的用户名 |
| 34 | + password: 你的密码 |
| 35 | + dialect-type: mysql |
| 36 | +``` |
| 37 | +
|
| 38 | +### 2. 配置API Key |
| 39 | +
|
| 40 | +在同一个配置文件中设置(两个部分都需要配置): |
| 41 | +
|
| 42 | +```yaml |
| 43 | +spring: |
| 44 | + ai: |
| 45 | + dashscope: |
| 46 | + api-key: 你的DashScope-API-Key |
| 47 | + openai: |
| 48 | + api-key: 你的DashScope-API-Key |
| 49 | +``` |
| 50 | +
|
| 51 | +## 如何使用 |
| 52 | +### API调用 |
| 53 | +```bash |
| 54 | +# 执行自然语言查询 |
| 55 | +GET http://localhost:8062/nl2sql/search?query=查询用户数量 |
| 56 | + |
| 57 | +# 流式聊天(实时返回结果,想要参与进入开发,一般使用该接口) |
| 58 | +GET http://localhost:8062/nl2sql/stream/search?query=你的问题 |
| 59 | + |
| 60 | +# 简单聊天接口 |
| 61 | +POST http://localhost:8062/simpleChat |
| 62 | +Content-Type: application/json |
| 63 | +``` |
| 64 | +
|
| 65 | +
|
| 66 | +## 开发集成 |
| 67 | +
|
| 68 | +如果要在自己的项目中集成NL2SQL功能: |
| 69 | +
|
| 70 | +### 1. 添加依赖 |
| 71 | +
|
| 72 | +```xml |
| 73 | +<dependency> |
| 74 | + <groupId>com.alibaba.cloud.ai</groupId> |
| 75 | + <artifactId>spring-ai-alibaba-starter-nl2sql</artifactId> |
| 76 | + <version>${spring-ai-alibaba.version}</version> |
| 77 | +</dependency> |
| 78 | +``` |
| 79 | + |
| 80 | +### 2. 代码示例 |
| 81 | + |
| 82 | +```java |
| 83 | +@RestController |
| 84 | +public class MyNl2SqlController { |
| 85 | + |
| 86 | + @Autowired |
| 87 | + private SimpleNl2SqlService simpleNl2SqlService; |
| 88 | + |
| 89 | + @PostMapping("/query") |
| 90 | + public String query(@RequestBody String question) throws Exception { |
| 91 | + return simpleNl2SqlService.nl2sql(question); |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +## 贡献指南 |
| 97 | + |
| 98 | +欢迎提交Issue和PR!详见 [Spring AI Alibaba 贡献指南](https://github.com/alibaba/spring-ai-alibaba/blob/main/CONTRIBUTING-zh.md) |
| 99 | + |
| 100 | +## 开源协议 |
| 101 | + |
| 102 | +Apache License 2.0 |
0 commit comments