ai-business-write/README.md
2025-12-04 14:41:20 +08:00

258 lines
5.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 智慧监督AI文书写作服务
基于大模型的智能文书生成服务,支持从非结构化文本中提取结构化字段数据。
## 功能特性
- ✅ AI解析接口 (`/api/ai/extract`) - 从输入文本中提取结构化字段
- ✅ 字段配置管理 - 从数据库读取字段配置
- ✅ 支持硅基流动大模型DeepSeek
- 🔄 预留华为大模型接口支持
- ✅ Web测试界面 - 可视化测试解析功能
## 项目结构
```
.
├── app.py # Flask主应用
├── requirements.txt # Python依赖
├── .env.example # 环境变量配置示例
├── services/ # 服务层
│ ├── ai_service.py # AI服务大模型调用
│ └── field_service.py # 字段服务(数据库操作)
├── utils/ # 工具类
│ └── response.py # 响应格式化
└── static/ # 静态文件
└── index.html # 测试页面
```
## 快速开始
### 1. 环境准备
**Windows系统**
```bash
# 运行安装脚本
setup_env.bat
```
**Linux/Mac系统**
```bash
# 运行安装脚本
chmod +x setup_env.sh
./setup_env.sh
```
**手动安装:**
```bash
# 创建虚拟环境
python -m venv venv
# 激活虚拟环境
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activate
# 安装依赖
pip install -r requirements.txt
```
### 2. 配置环境变量
复制 `.env.example``.env` 并填入配置:
```bash
# Windows:
copy .env.example .env
# Linux/Mac:
cp .env.example .env
```
编辑 `.env` 文件填入你的API密钥
```env
# 硅基流动API配置必需
SILICONFLOW_API_KEY=your_api_key_here
SILICONFLOW_MODEL=deepseek-ai/DeepSeek-V3.2-Exp
# 数据库配置(已默认配置,如需修改可调整)
DB_HOST=152.136.177.240
DB_PORT=5012
DB_USER=finyx
DB_PASSWORD=6QsGK6MpePZDE57Z
DB_NAME=finyx
```
### 3. 启动服务
```bash
# 确保虚拟环境已激活
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activate
# 启动服务
python app.py
```
服务启动后,访问:
- **测试页面**: http://localhost:7500/
- **API接口**: http://localhost:7500/api/ai/extract
## API接口说明
### 解析接口
**接口地址**: `POST /api/ai/extract`
**请求参数**:
```json
{
"businessType": "INVESTIGATION",
"inputData": [
{
"fieldCode": "clue_info",
"fieldValue": "被举报用户名称是张三年龄30岁"
},
{
"fieldCode": "target_basic_info_clue",
"fieldValue": "张三汉族1980年5月出生山西太原人"
}
]
}
```
**响应格式**:
```json
{
"code": 0,
"data": {
"outData": [
{
"fieldCode": "target_name",
"fieldValue": "张三"
},
{
"fieldCode": "target_gender",
"fieldValue": "男"
}
]
},
"msg": "ok",
"timestamp": "1764204337101",
"errorMsg": "",
"isSuccess": true
}
```
### 获取字段配置接口
**接口地址**: `GET /api/fields?businessType=INVESTIGATION`
**响应格式**:
```json
{
"code": 0,
"data": {
"input_fields": [...],
"output_fields": [...]
},
"isSuccess": true
}
```
## 测试方法
### 方法1: 使用Web测试页面
1. 启动服务后,在浏览器访问 `http://localhost:7500/`
2. 在"输入数据"区域填写输入字段
3. 点击"开始解析"按钮
4. 查看解析结果
### 方法2: 使用curl命令
```bash
curl -X POST http://localhost:7500/api/ai/extract \
-H "Content-Type: application/json" \
-d '{
"businessType": "INVESTIGATION",
"inputData": [
{
"fieldCode": "clue_info",
"fieldValue": "被举报用户名称是张三年龄30岁某公司总经理"
}
]
}'
```
### 方法3: 使用Python脚本
```python
import requests
url = "http://localhost:7500/api/ai/extract"
data = {
"businessType": "INVESTIGATION",
"inputData": [
{
"fieldCode": "clue_info",
"fieldValue": "被举报用户名称是张三年龄30岁"
}
]
}
response = requests.post(url, json=data)
print(response.json())
```
## 错误码说明
| 错误码 | 说明 | 处理建议 |
|--------|------|----------|
| 0 | 成功 | - |
| 400 | 请求参数错误 | 检查请求参数格式 |
| 1001 | 模板不存在 | 检查businessType是否正确 |
| 2001 | AI解析超时 | 重新尝试解析 |
| 2002 | 字段识别失败 | 检查输入文本质量 |
## 开发说明
### 添加新的AI服务提供商
`services/ai_service.py` 中:
1.`__init__` 方法中添加配置读取
2.`_determine_ai_provider` 中添加判断逻辑
3. 实现对应的 `_extract_with_xxx` 方法
### 扩展业务类型支持
`services/field_service.py` 中修改 `get_output_fields_by_business_type` 方法,根据不同的 `business_type` 返回对应的字段配置。
## 注意事项
1. **API密钥安全**: 请勿将 `.env` 文件提交到版本控制系统
2. **数据库连接**: 确保数据库服务可访问
3. **网络连接**: AI服务需要访问外部API确保网络畅通
4. **字段配置**: 当前仅支持"初步核实审批表"模板,其他模板需要先在数据库中配置
## 常见问题
**Q: 提示"未配置AI服务"**
A: 检查 `.env` 文件中的 `SILICONFLOW_API_KEY` 是否已正确配置。
**Q: 解析结果为空?**
A: 检查输入文本是否包含足够的信息,可以尝试更详细的输入文本。
**Q: 数据库连接失败?**
A: 检查数据库配置和网络连接,确保数据库服务可访问。
## 许可证
内部项目,仅供内部使用。