本文档描述 AgentOS 开源版的核心业务表。实际建表定义以 python_project_back/db_models/db_models.py 中的 SQLAlchemy ORM 模型为准。
开源版从零建库建议使用:
cd python_project_back
Copy-Item .env.example .env
python scripts/init_db.py
scripts/init_db.py 会调用 Base.metadata.create_all() 创建当前完整表结构。alembic/versions 目录保留项目演进过程中的增量迁移,适合后续二次开发继续维护 schema。
数据库建议使用 PostgreSQL,并安装 pgvector 扩展:
CREATE EXTENSION IF NOT EXISTS vector;
| 表名 |
作用 |
users |
用户账号、角色、登录基础信息 |
knowledge_bases |
知识库空间,支持个人/公共知识库 |
files |
上传文件元数据 |
document_chunks |
文件切片与向量,RAG 检索核心表 |
conversations |
对话会话 |
messages |
对话消息 |
data_sources |
Text2SQL 外部数据源配置 |
wiki_categories |
Wiki 分类 |
wiki_entries |
Wiki 条目正文 |
wiki_entry_chunks |
Wiki 条目语义检索切片 |
wiki_entry_versions |
Wiki 条目版本快照 |
resource_permissions |
资源授权 |
user_menu_permissions |
用户菜单功能权限 |
llm_configs |
LLM、Embedding、Reranker 模型配置 |
mcp_servers |
MCP 服务配置 |
text2sql_queries |
Text2SQL 查询历史 |
upload_tasks |
批量上传任务记录 |
task_records |
文档解析、切片、向量化任务记录 |
audit_logs |
审计日志 |
feedbacks |
回答质量反馈 |
workflow_executions |
Agent 工作流执行记录 |
table_metadata |
数据字典:表和字段注释 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
用户 ID |
username |
String(50) |
用户名,唯一 |
phone |
String(20) |
手机号,唯一,可空 |
email |
String(100) |
邮箱,唯一,可空 |
hashed_password |
String(255) |
加密后的密码 |
is_active |
Boolean |
是否启用 |
role |
String(20) |
admin / user |
created_at |
TIMESTAMP |
创建时间 |
updated_at |
TIMESTAMP |
更新时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
知识库 ID |
name |
String(100) |
知识库名称 |
description |
String/Text |
描述 |
user_id |
Integer FK |
创建者 |
is_public |
Boolean |
是否公共知识库 |
created_at |
TIMESTAMP |
创建时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
文件 ID |
kb_id |
Integer FK |
所属知识库 |
file_name |
String(255) |
原始文件名 |
file_path |
String(512) |
MinIO 路径或访问地址 |
file_type |
String(50) |
文件类型 |
file_size |
Integer |
文件大小,字节 |
user_id |
Integer FK |
上传用户 |
status |
String(20) |
uploaded / parsing / finished / failed |
md_path |
String(512) |
解析后的 Markdown 中间文件路径 |
error_msg |
Text |
处理失败原因 |
page_count |
Integer |
页数 |
chunk_count |
Integer |
切片数 |
created_at |
TIMESTAMP |
上传时间 |
updated_at |
TIMESTAMP |
更新时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
切片 ID |
file_id |
Integer FK |
所属文件 |
parent_chunk_id |
Integer FK |
父切片,用于 Small-to-Big 检索 |
content |
String/Text |
切片正文 |
embedding |
Vector |
向量 |
chunk_index |
Integer |
切片序号 |
chunk_type |
String(20) |
parent / child |
heading_path |
String(500) |
Markdown 标题路径 |
token_count |
Integer |
近似 token 数 |
milvus_id |
String(100) |
外部向量库 ID,预留 |
es_doc_id |
String(100) |
Elasticsearch 文档 ID,预留 |
page_num |
Integer |
来源页码 |
created_at |
TIMESTAMP |
创建时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
会话 ID |
user_id |
Integer FK |
所属用户 |
kb_id |
Integer FK |
绑定知识库,可空 |
conversation_type |
String(20) |
chat / agent / rag |
title |
String(255) |
会话标题 |
history_summary |
Text |
长历史摘要缓存 |
summary_updated_at |
TIMESTAMP |
摘要更新时间 |
created_at |
TIMESTAMP |
创建时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
消息 ID |
conversation_id |
Integer FK |
所属会话 |
role |
String(20) |
user / assistant |
content |
String/Text |
消息内容 |
used_tokens |
Integer |
token 消耗 |
created_at |
TIMESTAMP |
创建时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
数据源 ID |
user_id |
Integer FK |
创建者 |
name |
String(100) |
数据源名称 |
db_type |
String(20) |
mysql / postgresql / dm / sqlite / mssql / oracle |
host |
String(255) |
主机 |
port |
Integer |
端口 |
username |
String(100) |
用户名 |
password |
String(255) |
密码,生产环境建议加密 |
database |
String(100) |
数据库名 |
schema_name |
String(100) |
Schema |
description |
String(500) |
描述 |
is_active |
Boolean |
是否启用 |
created_at |
TIMESTAMP |
创建时间 |
updated_at |
TIMESTAMP |
更新时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
分类 ID |
user_id |
Integer FK |
创建者 |
name |
String(100) |
分类名称 |
description |
String(500) |
描述 |
sort_order |
Integer |
排序 |
is_deleted |
Boolean |
是否删除 |
created_at |
TIMESTAMP |
创建时间 |
updated_at |
TIMESTAMP |
更新时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
条目 ID |
user_id |
Integer FK |
创建者 |
category_id |
Integer FK |
分类 ID |
title |
String(200) |
标题 |
summary |
String(500) |
摘要 |
content |
Text |
正文 |
tags |
JSON |
标签列表 |
status |
String(20) |
draft / published / archived |
is_deleted |
Boolean |
是否删除 |
created_at |
TIMESTAMP |
创建时间 |
updated_at |
TIMESTAMP |
更新时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
切片 ID |
entry_id |
Integer FK |
Wiki 条目 |
user_id |
Integer FK |
所属用户 |
content |
Text |
切片内容 |
chunk_index |
Integer |
切片序号 |
heading_path |
String(500) |
标题路径 |
token_count |
Integer |
近似 token 数 |
embedding |
Vector |
向量 |
embedding_provider |
String(50) |
向量模型提供方 |
embedding_model |
String(100) |
向量模型名称 |
created_at |
TIMESTAMP |
创建时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
授权 ID |
resource_type |
String(30) |
knowledge_base / data_source / wiki_entry |
resource_id |
Integer |
资源 ID |
owner_user_id |
Integer FK |
资源拥有者 |
grantee_user_id |
Integer FK |
被授权用户 |
permission |
String(20) |
read / write / admin |
is_active |
Boolean |
是否启用 |
created_at |
TIMESTAMP |
创建时间 |
updated_at |
TIMESTAMP |
更新时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
配置 ID |
user_id |
Integer FK |
用户 ID |
config_type |
String(20) |
llm / embedding / reranker |
provider |
String(50) |
openai / qwen / ollama / vllm / local |
model_name |
String(100) |
模型名称 |
api_key |
String(255) |
API Key |
base_url |
String(255) |
Base URL |
max_tokens |
Integer |
最大 token |
temperature |
Float |
温度 |
timeout |
Integer |
超时时间 |
extra_params |
JSON |
额外参数 |
is_default |
Boolean |
是否默认配置 |
is_active |
Boolean |
是否启用 |
is_deleted |
Boolean |
是否删除 |
created_at |
TIMESTAMP |
创建时间 |
updated_at |
TIMESTAMP |
更新时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
查询 ID |
user_id |
Integer FK |
用户 ID |
data_source_id |
Integer FK |
数据源 ID |
natural_language |
Text |
用户问题 |
generated_sql |
Text |
模型生成 SQL |
executed_sql |
Text |
实际执行 SQL |
schema_context |
JSON |
Schema 上下文 |
result_summary |
Text |
查询结果摘要 |
full_result |
JSON |
完整查询结果 |
llm_summary |
Text |
LLM 分析总结 |
status |
String(20) |
success / failed / review |
error_msg |
Text |
错误信息 |
execution_time_ms |
Integer |
执行耗时 |
created_at |
TIMESTAMP |
创建时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
任务 ID |
user_id |
Integer FK |
用户 ID |
task_type |
String(50) |
任务类型 |
resource_type |
String(50) |
资源类型 |
resource_id |
Integer |
资源 ID |
file_id |
Integer FK |
文件 ID |
kb_id |
Integer FK |
知识库 ID |
stage |
String(30) |
pending / parsing / chunking / embedding / indexing |
status |
String(20) |
pending / running / success / failed |
progress |
Integer |
进度百分比 |
payload |
JSON |
输入参数 |
result |
JSON |
输出结果 |
error_msg |
Text |
失败原因 |
retry_count |
Integer |
已重试次数 |
max_retries |
Integer |
最大重试次数 |
started_at |
TIMESTAMP |
开始时间 |
finished_at |
TIMESTAMP |
结束时间 |
created_at |
TIMESTAMP |
创建时间 |
updated_at |
TIMESTAMP |
更新时间 |
| 字段 |
类型 |
说明 |
id |
Integer PK |
ID |
user_id |
Integer FK |
用户 ID |
data_source_id |
Integer FK |
数据源 ID |
table_name |
String(255) |
表名 |
column_name |
String(255) |
字段名,空值表示表级注释 |
comment |
Text |
用户自定义注释 |
created_at |
TIMESTAMP |
创建时间 |
updated_at |
TIMESTAMP |
更新时间 |
user_menu_permissions:用户菜单和功能权限。
mcp_servers:MCP 服务配置。
upload_tasks:批量上传任务聚合记录。
audit_logs:审计日志。
feedbacks:用户反馈。
workflow_executions:Agent 工作流执行记录。
wiki_entry_versions:Wiki 条目版本快照。