Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
黄南光
/
wiki_knowledge
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 4a54c312
authored
Jun 24, 2026
by
黄南光
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
表结构加入索引,注释,接口返回修改
1 parent
2c09a158
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
118 additions
and
94 deletions
scripts/rag_slim/rebuild_rag_schema.sql
src/database/models.py
src/rag/service.py
scripts/rag_slim/rebuild_rag_schema.sql
View file @
4a54c31
...
@@ -39,8 +39,11 @@ CREATE TABLE rag_documents (
...
@@ -39,8 +39,11 @@ CREATE TABLE rag_documents (
index_started_at
DATETIME
NULL
,
index_started_at
DATETIME
NULL
,
indexed_at
DATETIME
NULL
,
indexed_at
DATETIME
NULL
,
INDEX
idx_rag_documents_status
(
status
),
INDEX
idx_rag_documents_status
(
status
),
INDEX
idx_rag_documents_hash
(
content_hash
)
INDEX
idx_rag_documents_hash
(
content_hash
),
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8mb4
COLLATE
=
utf8mb4_unicode_ci
;
INDEX
idx_rag_documents_name_status
(
name
,
status
),
INDEX
idx_rag_documents_created_at
(
created_at
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8mb4
COLLATE
=
utf8mb4_unicode_ci
COMMENT
=
'RAG 文档表,记录进入独立 RAG 索引的源文件及索引统计信息'
;
-- 精简切片表:只保留检索/邻居扩展/定位/UI 真正用到的列;
-- 精简切片表:只保留检索/邻居扩展/定位/UI 真正用到的列;
-- 其余结构化标签统一进 metadata_json。检索过滤/打分走 Qdrant payload,不读这些列。
-- 其余结构化标签统一进 metadata_json。检索过滤/打分走 Qdrant payload,不读这些列。
...
@@ -73,13 +76,15 @@ CREATE TABLE rag_chunks (
...
@@ -73,13 +76,15 @@ CREATE TABLE rag_chunks (
created_at
DATETIME
NULL
DEFAULT
CURRENT_TIMESTAMP
,
created_at
DATETIME
NULL
DEFAULT
CURRENT_TIMESTAMP
,
UNIQUE
KEY
uq_rag_chunks_chunk_id
(
chunk_id
),
UNIQUE
KEY
uq_rag_chunks_chunk_id
(
chunk_id
),
INDEX
idx_rag_chunks_document_level
(
document_id
,
chunk_level
),
INDEX
idx_rag_chunks_document_level
(
document_id
,
chunk_level
),
INDEX
idx_rag_chunks_document_level_index
(
document_id
,
chunk_level
,
chunk_index
),
INDEX
idx_rag_chunks_parent
(
parent_chunk_id
),
INDEX
idx_rag_chunks_parent
(
parent_chunk_id
),
INDEX
idx_rag_chunks_status
(
status
),
INDEX
idx_rag_chunks_status
(
status
),
INDEX
idx_rag_chunks_content_kind
(
content_kind
),
INDEX
idx_rag_chunks_content_kind
(
content_kind
),
INDEX
idx_rag_chunks_business
(
business_category
),
INDEX
idx_rag_chunks_business
(
business_category
),
INDEX
idx_rag_chunks_brand
(
brand_series
),
INDEX
idx_rag_chunks_brand
(
brand_series
),
INDEX
idx_rag_chunks_source_loc
(
page_start
,
page_end
)
INDEX
idx_rag_chunks_source_loc
(
page_start
,
page_end
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8mb4
COLLATE
=
utf8mb4_unicode_ci
;
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8mb4
COLLATE
=
utf8mb4_unicode_ci
COMMENT
=
'RAG 切片表,保存文档切片内容、层级关系、定位信息和轻量检索字段'
;
CREATE
TABLE
rag_index_runs
(
CREATE
TABLE
rag_index_runs
(
id
INT
NOT
NULL
AUTO_INCREMENT
PRIMARY
KEY
,
id
INT
NOT
NULL
AUTO_INCREMENT
PRIMARY
KEY
,
...
@@ -100,7 +105,8 @@ CREATE TABLE rag_index_runs (
...
@@ -100,7 +105,8 @@ CREATE TABLE rag_index_runs (
created_at
DATETIME
NULL
DEFAULT
CURRENT_TIMESTAMP
,
created_at
DATETIME
NULL
DEFAULT
CURRENT_TIMESTAMP
,
INDEX
idx_rag_index_runs_document
(
document_id
),
INDEX
idx_rag_index_runs_document
(
document_id
),
INDEX
idx_rag_index_runs_status
(
status
)
INDEX
idx_rag_index_runs_status
(
status
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8mb4
COLLATE
=
utf8mb4_unicode_ci
;
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8mb4
COLLATE
=
utf8mb4_unicode_ci
COMMENT
=
'RAG 索引任务运行记录表,保存每次索引执行的统计和错误信息'
;
CREATE
TABLE
rag_query_logs
(
CREATE
TABLE
rag_query_logs
(
id
INT
NOT
NULL
AUTO_INCREMENT
PRIMARY
KEY
,
id
INT
NOT
NULL
AUTO_INCREMENT
PRIMARY
KEY
,
...
@@ -123,5 +129,7 @@ CREATE TABLE rag_query_logs (
...
@@ -123,5 +129,7 @@ CREATE TABLE rag_query_logs (
error_message
LONGTEXT
NULL
,
error_message
LONGTEXT
NULL
,
created_at
DATETIME
NULL
DEFAULT
CURRENT_TIMESTAMP
,
created_at
DATETIME
NULL
DEFAULT
CURRENT_TIMESTAMP
,
INDEX
idx_rag_query_logs_session
(
session_id
),
INDEX
idx_rag_query_logs_session
(
session_id
),
INDEX
idx_rag_query_logs_status
(
status
)
INDEX
idx_rag_query_logs_status
(
status
),
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8mb4
COLLATE
=
utf8mb4_unicode_ci
;
INDEX
idx_rag_query_logs_session_status_created
(
session_id
,
status
,
created_at
)
)
ENGINE
=
InnoDB
DEFAULT
CHARSET
=
utf8mb4
COLLATE
=
utf8mb4_unicode_ci
COMMENT
=
'RAG 查询日志表,记录用户查询、改写、检索过程、引用和回答结果'
;
src/database/models.py
View file @
4a54c31
...
@@ -72,35 +72,38 @@ class RagFile(Base):
...
@@ -72,35 +72,38 @@ class RagFile(Base):
__table_args__
=
(
__table_args__
=
(
Index
(
"idx_rag_documents_status"
,
"status"
),
Index
(
"idx_rag_documents_status"
,
"status"
),
Index
(
"idx_rag_documents_hash"
,
"content_hash"
),
Index
(
"idx_rag_documents_hash"
,
"content_hash"
),
Index
(
"idx_rag_documents_name_status"
,
"name"
,
"status"
),
Index
(
"idx_rag_documents_created_at"
,
"created_at"
),
{
"comment"
:
"RAG 文档表,记录进入独立 RAG 索引的源文件及索引统计信息"
},
)
)
id
=
Column
(
String
(
36
),
primary_key
=
True
,
default
=
lambda
:
str
(
uuid
.
uuid4
()))
id
=
Column
(
String
(
36
),
primary_key
=
True
,
default
=
lambda
:
str
(
uuid
.
uuid4
())
,
comment
=
"文档主键 UUID"
)
name
=
Column
(
String
(
255
),
nullable
=
False
)
name
=
Column
(
String
(
255
),
nullable
=
False
,
comment
=
"源文件名称"
)
format
=
Column
(
"file_type"
,
String
(
50
),
nullable
=
False
)
format
=
Column
(
"file_type"
,
String
(
50
),
nullable
=
False
,
comment
=
"源文件类型或扩展名"
)
size
=
Column
(
BigInteger
,
nullable
=
False
,
default
=
0
)
size
=
Column
(
BigInteger
,
nullable
=
False
,
default
=
0
,
comment
=
"源文件大小,单位字节"
)
word_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
word_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"文档解析后的估算字数"
)
status
=
Column
(
String
(
50
),
nullable
=
False
,
default
=
"Pending"
)
status
=
Column
(
String
(
50
),
nullable
=
False
,
default
=
"Pending"
,
comment
=
"索引状态:Pending/Ingesting/Indexed/Failed 等"
)
source_path
=
Column
(
String
(
1024
),
nullable
=
False
)
source_path
=
Column
(
String
(
1024
),
nullable
=
False
,
comment
=
"源文件在本地或对象存储中的路径"
)
content_hash
=
Column
(
String
(
128
),
nullable
=
True
)
content_hash
=
Column
(
String
(
128
),
nullable
=
True
,
comment
=
"源文件内容哈希,用于去重或变更检测"
)
metadata_json
=
Column
(
Text
,
nullable
=
True
)
metadata_json
=
Column
(
Text
,
nullable
=
True
,
comment
=
"文档级扩展元数据 JSON"
)
parser_version
=
Column
(
String
(
50
),
nullable
=
True
)
parser_version
=
Column
(
String
(
50
),
nullable
=
True
,
comment
=
"解析器版本"
)
chunker_version
=
Column
(
String
(
50
),
nullable
=
True
)
chunker_version
=
Column
(
String
(
50
),
nullable
=
True
,
comment
=
"切片器版本"
)
block_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
block_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"解析得到的原始块数量"
)
chunk_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
chunk_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"切片总数"
)
parent_chunk_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
parent_chunk_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"父级切片数量"
)
child_chunk_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
child_chunk_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"子级切片数量"
)
index_duration_ms
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
index_duration_ms
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"最近一次索引耗时,单位毫秒"
)
# VLM 整页解析的资源消耗(落库统计)
# VLM 整页解析的资源消耗(落库统计)
vlm_page_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
vlm_page_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"VLM 整页解析页数"
)
vlm_total_tokens
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
vlm_total_tokens
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"VLM 解析消耗的 token 总数"
)
vlm_duration_ms
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
vlm_duration_ms
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"VLM 解析耗时,单位毫秒"
)
error_code
=
Column
(
String
(
100
),
nullable
=
True
)
error_code
=
Column
(
String
(
100
),
nullable
=
True
,
comment
=
"索引失败时的错误码"
)
error_detail_json
=
Column
(
Text
,
nullable
=
True
)
error_detail_json
=
Column
(
Text
,
nullable
=
True
,
comment
=
"索引失败时的结构化错误详情 JSON"
)
error_message
=
Column
(
Text
,
nullable
=
True
)
error_message
=
Column
(
Text
,
nullable
=
True
,
comment
=
"索引失败时的错误摘要"
)
created_at
=
Column
(
DateTime
,
default
=
datetime
.
now
)
created_at
=
Column
(
DateTime
,
default
=
datetime
.
now
,
comment
=
"记录创建时间"
)
updated_at
=
Column
(
DateTime
,
default
=
datetime
.
now
,
onupdate
=
datetime
.
now
)
updated_at
=
Column
(
DateTime
,
default
=
datetime
.
now
,
onupdate
=
datetime
.
now
,
comment
=
"记录更新时间"
)
index_started_at
=
Column
(
DateTime
,
nullable
=
True
)
index_started_at
=
Column
(
DateTime
,
nullable
=
True
,
comment
=
"最近一次索引开始时间"
)
indexed_at
=
Column
(
DateTime
,
nullable
=
True
)
indexed_at
=
Column
(
DateTime
,
nullable
=
True
,
comment
=
"最近一次索引成功完成时间"
)
chunks
=
relationship
(
"RagChunk"
,
back_populates
=
"file"
,
cascade
=
"all, delete-orphan"
,
lazy
=
"noload"
,
primaryjoin
=
"RagFile.id == RagChunk.file_id"
,
foreign_keys
=
"[RagChunk.file_id]"
)
chunks
=
relationship
(
"RagChunk"
,
back_populates
=
"file"
,
cascade
=
"all, delete-orphan"
,
lazy
=
"noload"
,
primaryjoin
=
"RagFile.id == RagChunk.file_id"
,
foreign_keys
=
"[RagChunk.file_id]"
)
index_runs
=
relationship
(
"RagIndexRun"
,
back_populates
=
"file"
,
cascade
=
"all, delete-orphan"
,
lazy
=
"noload"
,
primaryjoin
=
"RagFile.id == RagIndexRun.file_id"
,
foreign_keys
=
"[RagIndexRun.file_id]"
)
index_runs
=
relationship
(
"RagIndexRun"
,
back_populates
=
"file"
,
cascade
=
"all, delete-orphan"
,
lazy
=
"noload"
,
primaryjoin
=
"RagFile.id == RagIndexRun.file_id"
,
foreign_keys
=
"[RagIndexRun.file_id]"
)
...
@@ -115,40 +118,42 @@ class RagChunk(Base):
...
@@ -115,40 +118,42 @@ class RagChunk(Base):
__table_args__
=
(
__table_args__
=
(
UniqueConstraint
(
"chunk_id"
,
name
=
"uq_rag_chunks_chunk_id"
),
UniqueConstraint
(
"chunk_id"
,
name
=
"uq_rag_chunks_chunk_id"
),
Index
(
"idx_rag_chunks_document_level"
,
"document_id"
,
"chunk_level"
),
Index
(
"idx_rag_chunks_document_level"
,
"document_id"
,
"chunk_level"
),
Index
(
"idx_rag_chunks_document_level_index"
,
"document_id"
,
"chunk_level"
,
"chunk_index"
),
Index
(
"idx_rag_chunks_parent"
,
"parent_chunk_id"
),
Index
(
"idx_rag_chunks_parent"
,
"parent_chunk_id"
),
Index
(
"idx_rag_chunks_status"
,
"status"
),
Index
(
"idx_rag_chunks_status"
,
"status"
),
Index
(
"idx_rag_chunks_content_kind"
,
"content_kind"
),
Index
(
"idx_rag_chunks_content_kind"
,
"content_kind"
),
Index
(
"idx_rag_chunks_business"
,
"business_category"
),
Index
(
"idx_rag_chunks_business"
,
"business_category"
),
Index
(
"idx_rag_chunks_brand"
,
"brand_series"
),
Index
(
"idx_rag_chunks_brand"
,
"brand_series"
),
Index
(
"idx_rag_chunks_source_loc"
,
"page_start"
,
"page_end"
),
Index
(
"idx_rag_chunks_source_loc"
,
"page_start"
,
"page_end"
),
{
"comment"
:
"RAG 切片表,保存文档切片内容、层级关系、定位信息和轻量检索字段"
},
)
)
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
,
comment
=
"切片自增主键"
)
chunk_id
=
Column
(
String
(
64
),
nullable
=
False
)
chunk_id
=
Column
(
String
(
64
),
nullable
=
False
,
comment
=
"业务切片 ID,同时对应向量 payload 中的 chunk_id"
)
file_id
=
Column
(
"document_id"
,
String
(
36
),
nullable
=
False
)
file_id
=
Column
(
"document_id"
,
String
(
36
),
nullable
=
False
,
comment
=
"所属 RAG 文档 ID"
)
parent_chunk_id
=
Column
(
String
(
64
),
nullable
=
True
)
parent_chunk_id
=
Column
(
String
(
64
),
nullable
=
True
,
comment
=
"父级切片 ID,用于父子层级检索"
)
chunk_level
=
Column
(
String
(
20
),
nullable
=
False
)
# parent / child
chunk_level
=
Column
(
String
(
20
),
nullable
=
False
,
comment
=
"切片层级:parent 或 child"
)
chunk_index
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
chunk_index
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"切片在文档中的顺序"
)
content
=
Column
(
Text
,
nullable
=
False
)
content
=
Column
(
Text
,
nullable
=
False
,
comment
=
"切片文本内容"
)
content_hash
=
Column
(
String
(
128
),
nullable
=
False
)
content_hash
=
Column
(
String
(
128
),
nullable
=
False
,
comment
=
"切片内容哈希"
)
token_estimate
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
token_estimate
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"切片 token 估算值"
)
title_path
=
Column
(
String
(
1024
),
nullable
=
True
)
title_path
=
Column
(
String
(
1024
),
nullable
=
True
,
comment
=
"标题层级路径"
)
source_name
=
Column
(
String
(
255
),
nullable
=
False
)
source_name
=
Column
(
String
(
255
),
nullable
=
False
,
comment
=
"来源文件名或来源标题"
)
page_start
=
Column
(
Integer
,
nullable
=
True
)
page_start
=
Column
(
Integer
,
nullable
=
True
,
comment
=
"切片起始页码"
)
page_end
=
Column
(
Integer
,
nullable
=
True
)
page_end
=
Column
(
Integer
,
nullable
=
True
,
comment
=
"切片结束页码"
)
row_start
=
Column
(
Integer
,
nullable
=
True
)
row_start
=
Column
(
Integer
,
nullable
=
True
,
comment
=
"表格类内容起始行号"
)
row_end
=
Column
(
Integer
,
nullable
=
True
)
row_end
=
Column
(
Integer
,
nullable
=
True
,
comment
=
"表格类内容结束行号"
)
prev_chunk_id
=
Column
(
String
(
64
),
nullable
=
True
)
prev_chunk_id
=
Column
(
String
(
64
),
nullable
=
True
,
comment
=
"前一个相邻切片 ID"
)
next_chunk_id
=
Column
(
String
(
64
),
nullable
=
True
)
next_chunk_id
=
Column
(
String
(
64
),
nullable
=
True
,
comment
=
"后一个相邻切片 ID"
)
status
=
Column
(
String
(
50
),
nullable
=
False
,
default
=
"active"
)
status
=
Column
(
String
(
50
),
nullable
=
False
,
default
=
"active"
,
comment
=
"切片状态:active/deleted 等"
)
brand_series
=
Column
(
String
(
100
),
nullable
=
True
)
brand_series
=
Column
(
String
(
100
),
nullable
=
True
,
comment
=
"品牌或产品系列标签"
)
business_category
=
Column
(
String
(
100
),
nullable
=
True
)
business_category
=
Column
(
String
(
100
),
nullable
=
True
,
comment
=
"业务分类标签"
)
content_kind
=
Column
(
String
(
50
),
nullable
=
True
)
content_kind
=
Column
(
String
(
50
),
nullable
=
True
,
comment
=
"内容类型,如 text/table/image 等"
)
section_title
=
Column
(
String
(
255
),
nullable
=
True
)
section_title
=
Column
(
String
(
255
),
nullable
=
True
,
comment
=
"切片所在章节标题"
)
parse_status
=
Column
(
String
(
50
),
nullable
=
True
)
parse_status
=
Column
(
String
(
50
),
nullable
=
True
,
comment
=
"切片解析状态或质量标记"
)
qdrant_point_id
=
Column
(
String
(
64
),
nullable
=
True
)
qdrant_point_id
=
Column
(
String
(
64
),
nullable
=
True
,
comment
=
"对应的 Qdrant point ID"
)
metadata_json
=
Column
(
Text
,
nullable
=
True
)
metadata_json
=
Column
(
Text
,
nullable
=
True
,
comment
=
"切片扩展元数据 JSON"
)
created_at
=
Column
(
DateTime
,
default
=
datetime
.
now
)
created_at
=
Column
(
DateTime
,
default
=
datetime
.
now
,
comment
=
"记录创建时间"
)
file
=
relationship
(
"RagFile"
,
back_populates
=
"chunks"
,
primaryjoin
=
"RagChunk.file_id == RagFile.id"
,
foreign_keys
=
[
file_id
])
file
=
relationship
(
"RagFile"
,
back_populates
=
"chunks"
,
primaryjoin
=
"RagChunk.file_id == RagFile.id"
,
foreign_keys
=
[
file_id
])
...
@@ -158,24 +163,25 @@ class RagIndexRun(Base):
...
@@ -158,24 +163,25 @@ class RagIndexRun(Base):
__table_args__
=
(
__table_args__
=
(
Index
(
"idx_rag_index_runs_document"
,
"document_id"
),
Index
(
"idx_rag_index_runs_document"
,
"document_id"
),
Index
(
"idx_rag_index_runs_status"
,
"status"
),
Index
(
"idx_rag_index_runs_status"
,
"status"
),
{
"comment"
:
"RAG 索引任务运行记录表,保存每次索引执行的统计和错误信息"
},
)
)
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
,
comment
=
"索引运行记录自增主键"
)
file_id
=
Column
(
"document_id"
,
String
(
36
),
nullable
=
False
)
file_id
=
Column
(
"document_id"
,
String
(
36
),
nullable
=
False
,
comment
=
"所属 RAG 文档 ID"
)
status
=
Column
(
String
(
50
),
nullable
=
False
)
status
=
Column
(
String
(
50
),
nullable
=
False
,
comment
=
"本次索引运行状态"
)
parser_version
=
Column
(
String
(
50
),
nullable
=
True
)
parser_version
=
Column
(
String
(
50
),
nullable
=
True
,
comment
=
"本次使用的解析器版本"
)
chunker_version
=
Column
(
String
(
50
),
nullable
=
True
)
chunker_version
=
Column
(
String
(
50
),
nullable
=
True
,
comment
=
"本次使用的切片器版本"
)
block_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
block_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"本次解析得到的原始块数量"
)
parent_chunk_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
parent_chunk_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"本次生成的父级切片数量"
)
child_chunk_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
child_chunk_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"本次生成的子级切片数量"
)
vector_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
vector_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"本次写入或更新的向量数量"
)
duration_ms
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
duration_ms
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"本次索引耗时,单位毫秒"
)
vlm_page_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
vlm_page_count
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"本次 VLM 整页解析页数"
)
vlm_total_tokens
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
vlm_total_tokens
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"本次 VLM 解析消耗 token 数"
)
error_code
=
Column
(
String
(
100
),
nullable
=
True
)
error_code
=
Column
(
String
(
100
),
nullable
=
True
,
comment
=
"本次索引失败时的错误码"
)
error_detail_json
=
Column
(
Text
,
nullable
=
True
)
error_detail_json
=
Column
(
Text
,
nullable
=
True
,
comment
=
"本次索引失败时的结构化错误详情 JSON"
)
error_message
=
Column
(
Text
,
nullable
=
True
)
error_message
=
Column
(
Text
,
nullable
=
True
,
comment
=
"本次索引失败时的错误摘要"
)
created_at
=
Column
(
DateTime
,
default
=
datetime
.
now
)
created_at
=
Column
(
DateTime
,
default
=
datetime
.
now
,
comment
=
"记录创建时间"
)
file
=
relationship
(
"RagFile"
,
back_populates
=
"index_runs"
,
primaryjoin
=
"RagIndexRun.file_id == RagFile.id"
,
foreign_keys
=
[
file_id
])
file
=
relationship
(
"RagFile"
,
back_populates
=
"index_runs"
,
primaryjoin
=
"RagIndexRun.file_id == RagFile.id"
,
foreign_keys
=
[
file_id
])
...
@@ -185,25 +191,27 @@ class RagQueryLog(Base):
...
@@ -185,25 +191,27 @@ class RagQueryLog(Base):
__table_args__
=
(
__table_args__
=
(
Index
(
"idx_rag_query_logs_session"
,
"session_id"
),
Index
(
"idx_rag_query_logs_session"
,
"session_id"
),
Index
(
"idx_rag_query_logs_status"
,
"status"
),
Index
(
"idx_rag_query_logs_status"
,
"status"
),
Index
(
"idx_rag_query_logs_session_status_created"
,
"session_id"
,
"status"
,
"created_at"
),
{
"comment"
:
"RAG 查询日志表,记录用户查询、改写、检索过程、引用和回答结果"
},
)
)
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
)
id
=
Column
(
Integer
,
primary_key
=
True
,
autoincrement
=
True
,
comment
=
"查询日志自增主键"
)
session_id
=
Column
(
String
(
100
),
nullable
=
True
)
session_id
=
Column
(
String
(
100
),
nullable
=
True
,
comment
=
"会话 ID"
)
query_text
=
Column
(
Text
,
nullable
=
False
)
query_text
=
Column
(
Text
,
nullable
=
False
,
comment
=
"用户原始查询"
)
rewritten_query
=
Column
(
Text
,
nullable
=
True
)
rewritten_query
=
Column
(
Text
,
nullable
=
True
,
comment
=
"查询改写后的文本"
)
intent_result_json
=
Column
(
Text
,
nullable
=
True
)
intent_result_json
=
Column
(
Text
,
nullable
=
True
,
comment
=
"意图识别结果 JSON"
)
retrieval_plan_json
=
Column
(
Text
,
nullable
=
True
)
retrieval_plan_json
=
Column
(
Text
,
nullable
=
True
,
comment
=
"检索计划 JSON"
)
query_plan_json
=
Column
(
Text
,
nullable
=
True
)
query_plan_json
=
Column
(
Text
,
nullable
=
True
,
comment
=
"查询规划 JSON"
)
filters_json
=
Column
(
Text
,
nullable
=
True
)
filters_json
=
Column
(
Text
,
nullable
=
True
,
comment
=
"实际使用的过滤条件 JSON"
)
ranked_json
=
Column
(
Text
,
nullable
=
True
)
ranked_json
=
Column
(
Text
,
nullable
=
True
,
comment
=
"排序后的候选结果 JSON"
)
sources_json
=
Column
(
Text
,
nullable
=
True
)
sources_json
=
Column
(
Text
,
nullable
=
True
,
comment
=
"回答引用来源 JSON"
)
adopted_chunk_ids_json
=
Column
(
Text
,
nullable
=
True
)
adopted_chunk_ids_json
=
Column
(
Text
,
nullable
=
True
,
comment
=
"最终采用的切片 ID 列表 JSON"
)
citation_map_json
=
Column
(
Text
,
nullable
=
True
)
citation_map_json
=
Column
(
Text
,
nullable
=
True
,
comment
=
"引用编号到切片或来源的映射 JSON"
)
answer_text
=
Column
(
Text
,
nullable
=
True
)
answer_text
=
Column
(
Text
,
nullable
=
True
,
comment
=
"最终回答文本"
)
model_name
=
Column
(
String
(
100
),
nullable
=
True
)
model_name
=
Column
(
String
(
100
),
nullable
=
True
,
comment
=
"生成回答使用的模型名称"
)
total_tokens
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
total_tokens
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"本次查询消耗 token 总数"
)
duration_ms
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
)
duration_ms
=
Column
(
Integer
,
nullable
=
False
,
default
=
0
,
comment
=
"本次查询耗时,单位毫秒"
)
status
=
Column
(
String
(
50
),
nullable
=
False
,
default
=
"Success"
)
status
=
Column
(
String
(
50
),
nullable
=
False
,
default
=
"Success"
,
comment
=
"查询状态:Success/Error 等"
)
error_message
=
Column
(
Text
,
nullable
=
True
)
error_message
=
Column
(
Text
,
nullable
=
True
,
comment
=
"查询失败时的错误信息"
)
created_at
=
Column
(
DateTime
,
default
=
datetime
.
now
)
created_at
=
Column
(
DateTime
,
default
=
datetime
.
now
,
comment
=
"记录创建时间"
)
src/rag/service.py
View file @
4a54c31
...
@@ -406,7 +406,15 @@ async def query_json(payload) -> dict:
...
@@ -406,7 +406,15 @@ async def query_json(payload) -> dict:
async
for
event
in
stream_query
(
payload
):
async
for
event
in
stream_query
(
payload
):
if
event
.
get
(
"type"
)
==
"done"
:
if
event
.
get
(
"type"
)
==
"done"
:
final_data
=
event
.
get
(
"data"
)
final_data
=
event
.
get
(
"data"
)
return
final_data
or
{}
if
final_data
:
return
{
"answer"
:
final_data
.
get
(
"answer"
,
""
),
"tokens"
:
final_data
.
get
(
"tokens"
,
0
),
"duration_ms"
:
final_data
.
get
(
"duration_ms"
,
0
),
"status"
:
"Success"
}
return
{}
_SAVE_QUERY_TIMEOUT_SECONDS
=
float
(
os
.
environ
.
get
(
"RAG_SAVE_QUERY_TIMEOUT_SECONDS"
,
"3"
))
_SAVE_QUERY_TIMEOUT_SECONDS
=
float
(
os
.
environ
.
get
(
"RAG_SAVE_QUERY_TIMEOUT_SECONDS"
,
"3"
))
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment