|
1 tuần trước cách đây | |
---|---|---|
src | 1 tuần trước cách đây | |
.env | 1 tuần trước cách đây | |
.gitignore | 1 tuần trước cách đây | |
main.py | 1 tuần trước cách đây | |
readme.md | 1 tuần trước cách đây | |
requirements.txt | 1 tuần trước cách đây | |
start.sh | 1 tuần trước cách đây | |
stop.sh | 1 tuần trước cách đây |
knowledge-tools/
├── src/ # 源代码目录
│ ├── api/ # API接口模块
│ │ └── main.py # FastAPI主应用
│ ├── agent/ # LangGraph Agent模块
│ │ └── query_agent.py # 查询生成Agent
│ ├── tools/ # 工具模块
│ │ ├── query_tool.py # 查询工具
│ │ ├── prompts.py # Prompt模板
│ │ └── scheduler.py # 任务调度器
│ ├── database/ # 数据库模块
│ │ ├── connection.py # 数据库连接管理
│ │ └── models.py # 数据库模型和DAO
│ └── models/ # 数据模型
│ └── schemas.py # Pydantic模型
├── logs/ # 日志目录
├── start.sh # 启动脚本
├── stop.sh # 停止脚本
├── main.py # 主程序入口
├── requirements.txt # Python依赖
├── app.pid # 进程ID文件
└── .env # 环境变量配置文件
# 克隆项目(如果从git仓库)
git clone <repository-url>
cd knowledge-tools
# 创建虚拟环境
python3 -m venv venv
source venv/bin/activate # Linux/Mac
# 或
venv\Scripts\activate # Windows
pip install -r requirements.txt
# 复制环境变量示例文件
cp env.example .env
# 编辑.env文件,设置你的OpenAI API密钥
vim .env
# 使用脚本启动(推荐)
./start.sh
# 或直接使用Python
python main.py
# 健康检查
curl http://localhost:8079/health
# 生成查询词(异步处理)
curl -X POST "http://localhost:8079/generate-queries" \
-H "Content-Type: application/json" \
-d '{"question": "如何学习Python编程"}'
# 查询任务状态
curl http://localhost:8079/task/1703123456789
# 运行异步处理测试(需要先创建测试脚本)
# python test_async_processing.py
/health
/generate-queries
请求体:
{
"question": "用户的问题"
}
json
{
"task_id": 1703123456789,
"queries": [],
"original_question": "原始问题",
"total_count": 0,
"status": 0
}
说明:接口立即返回任务ID,实际处理由后台定时任务完成
/task/{task_id}
响应:
{
"task_id": 1703123456789,
"question": "用户的问题",
"queries": ["查询词1", "查询词2", "..."],
"status": 2,
"status_text": "成功"
}
0
- 待执行:任务已创建,等待后台处理1
- 执行中:任务正在处理中2
- 成功:任务处理完成,已生成查询词3
- 失败:任务处理失败/tasks?status=2&limit=10
status
: 任务状态筛选(0-待执行,1-执行中,2-成功,3-失败)limit
: 限制数量json
{
"tasks": [...],
"total": 10
}
./start.sh
./stop.sh
tail -f logs/app.log
/generate-queries
接口提交问题/task/{task_id}
接口查询任务处理状态0
- 待执行: 任务已创建,等待处理1
- 执行中: 任务正在处理中2
- 成功: 任务处理完成,已生成查询词3
- 失败: 任务处理失败可以创建测试脚本验证异步处理功能:
# 创建测试脚本后运行
python test_async_processing.py
主要配置项在 .env
文件中:
# Gemini API配置
GEMINI_API_KEY=your_gemini_api_key_here
GEMINI_MODEL=gemini-1.5-pro
# 数据库配置
DB_HOST=rm-bp13g3ra2f59q49xs.mysql.rds.aliyuncs.com
DB_PORT=3306
DB_USER=wqsd
DB_PASSWORD=wqsd@2025
DB_NAME=ai_knowledge
DB_CHARSET=utf8
# 服务配置
HOST=0.0.0.0
PORT=8079
DEBUG=True
# 日志配置
LOG_LEVEL=INFO
LOG_FILE=logs/app.log
src/api/
): 负责HTTP接口和路由处理src/agent/
): 包含LangGraph Agent逻辑src/tools/
): 定义各种工具、Prompt模板和任务调度器src/database/
): 数据库连接、模型和DAO操作src/models/
): 定义数据模型和Schemasrc/tools/
目录下创建新的工具类src/tools/prompts.py
文件src/agent/query_agent.py
文件src/api/main.py
中添加新的路由src/tools/scheduler.py
文件项目使用MySQL数据库存储任务信息,需要创建以下表:
CREATE TABLE `knowledge_suggest_query` (
`question` text COMMENT '问题',
`task_id` bigint(20) NOT NULL COMMENT '任务id',
`querys` mediumtext COMMENT '产出的query词,json格式',
`status` int(11) DEFAULT NULL COMMENT '0:待执行;1:执行中;2:成功; 3:失败;',
PRIMARY KEY (`task_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='生成query词任务';