311 lines
7.6 KiB
Markdown
Raw Permalink 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`) - 从输入文本中提取结构化字段
- ✅ 字段配置管理 - 从数据库读取字段配置
- ✅ 支持多种AI服务提供商
- 华为大模型DeepSeek-R1-Distill-Llama-70B
- 硅基流动DeepSeek-V3.2-Exp
- ✅ 可通过配置灵活切换AI服务提供商
- ✅ 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` 文件,填入你的配置:
```env
# ========== AI服务提供商配置 ==========
# 选择使用的AI服务提供商
# 可选值: 'huawei' 或 'siliconflow'
# 默认值: 'siliconflow'
AI_PROVIDER=siliconflow
# ========== 华为大模型API配置当 AI_PROVIDER=huawei 时使用) ==========
HUAWEI_API_ENDPOINT=http://10.100.31.26:3001/v1/chat/completions
HUAWEI_API_KEY=sk-PoeiV3qwyTIRqcVc84E8E24cD2904872859a87922e0d9186
HUAWEI_MODEL=DeepSeek-R1-Distill-Llama-70B
HUAWEI_API_TIMEOUT=180
HUAWEI_API_MAX_TOKENS=12000
# ========== 硅基流动API配置当 AI_PROVIDER=siliconflow 时使用) ==========
SILICONFLOW_URL=https://api.siliconflow.cn/v1/chat/completions
SILICONFLOW_API_KEY=your_siliconflow_api_key_here
SILICONFLOW_MODEL=deepseek-ai/DeepSeek-V3.2-Exp
SILICONFLOW_API_TIMEOUT=120
SILICONFLOW_API_MAX_TOKENS=2000
# ========== 数据库配置 ==========
DB_HOST=152.136.177.240
DB_PORT=5012
DB_USER=finyx
DB_PASSWORD=6QsGK6MpePZDE57Z
DB_NAME=finyx
```
**AI服务提供商选择说明**
- **华为大模型**:设置 `AI_PROVIDER=huawei`,并配置 `HUAWEI_API_KEY``HUAWEI_API_ENDPOINT`
- **硅基流动**:设置 `AI_PROVIDER=siliconflow`(默认值),并配置 `SILICONFLOW_API_KEY`
如果配置的AI服务不完整系统会自动尝试使用另一个可用的服务。
**华为大模型API调用示例**
```bash
curl --location --request POST 'http://10.100.31.26:3001/v1/chat/completions' \
--header 'Authorization: Bearer sk-PoeiV3qwyTIRqcVc84E8E24cD2904872859a87922e0d9186' \
--header 'Content-Type: application/json' \
--data-raw '{
"model": "DeepSeek-R1-Distill-Llama-70B",
"messages": [
{
"role": "user",
"content": "介绍一下山西的营商环境,推荐适合什么行业经营"
}
],
"stream": false,
"presence_penalty": 1.03,
"frequency_penalty": 1.0,
"repetition_penalty": 1.0,
"temperature": 0.5,
"top_p": 0.95,
"top_k": 1,
"seed": 1,
"max_tokens": 8192,
"n": 2,
"best_of": 2
}'
```
### 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` 文件中正确设置了 `HUAWEI_API_KEY``HUAWEI_API_ENDPOINT`。如果华为大模型不可用请检查网络连接和API配置。
**Q: 解析结果为空?**
A: 检查输入文本是否包含足够的信息,可以尝试更详细的输入文本。
**Q: 数据库连接失败?**
A: 检查数据库配置和网络连接,确保数据库服务可访问。
## 许可证
内部项目,仅供内部使用。