notesProject/AI 中台知识库模块/知识库.md

知识库

AI 中台项目知识库模块是一个企业级 AI 知识库系统,能够为个人、团队和智能体提供 RAG (Retrieval-Augmented Generation) 能力,支持高效的知识构建与精准问答。

该系统分为几个核心模块。首先是文档上传和解析模块,支持用户上传多种格式的文档,如 PDF、Word、文本等。使用 Apache Tika 来解析这些文档的内容并对这些文档进行切块,为后续的向量化做准备。

然后是向量化模块,使用 embedding 技术,将分块的文档进行向量化,为后续的检索和 RAG 提供语义理解能力。

接下来是知识检索模块,实现了混合检索算法,即支持全文检索,也支持语义检索。

亮点

  • 支持各种常见的文档格式,使用 Apache Tika 增强文本解析能力
  • 通过组织标签提供权限隔离能力
  • 支持混合检索(全文检索 + 向量语义检索)
  • 支持 RAG

模块

用户管理模块

为了让知识库能够嵌入到公司各个系统中,并考虑到公司现有权限系统是基于组织架构的,因此设计了一套基于“组织标签”的用户管理模块。

文档上传与解析模块

文档上传基于分片上传技术,使用 MinIO 存储文档,使用 Redis BitMap 存储分片信息,通过断点续传的能力,有效地支持大文档上传和应对网络波动状况。

文档解析基于 Apache Tika 开源库,支持常见各种格式的文档内容提取,并结合 HanLP 的自然语言处理能力,实现了基于语义的智能文本块切分。

知识检索模块

  • 混合检索=全文检索 + 向量语义检索
  • RAG

全文检索

利用 pgroonga 插件为 Postgresql 提供全文检索能力,相比 Postgresql 原生的 tsvector,pgroonga 在多语言、性能、实时更新、排序以及模糊搜索上表现更优异。

首先启用插件,并创建表 memos 用作测试:

-- Enable the extension
CREATE EXTENSION IF NOT EXISTS pgroonga;

-- Create a table for test
create table memos ( id serial primary key, content text);

-- Create index using pgroonga
create index ix_memos_content ON memos USING pgroonga(content);

接着,向表中插入一些数据:

insert into memos(content)
values
  ('PostgreSQL is a relational database management system.'),
  ('Groonga is a fast full text search engine that supports all languages.'),
  ('PGroonga is a PostgreSQL extension that uses Groonga as index.'),
  ('There is groonga command.');

测试环境中,为了强制数据库使用 pgroonga 的索引,必须禁用顺序扫描(对于数据量小的表来说,全表扫描比索引更快):

-- For testing only. Don't do this in production
set enable_seqscan = off;

最后,通过操作符 &@~ 测试全文检索的结果:

select * from memos where content &@~ 'groonga';
select * from memos where content &@~ 'postgres pgroonga';
select * from memos where content &@~ 'postgres OR pgroonga';
select * from memos where content &@~ 'postgres -pgroonga';

具体规则可以直接查看 官方文档,不再赘述。

向量语义检索

通过文本模型将文档分块 embedding 成向量,存储到 Postgresql 中,并由 pgvector 提供向量检索支持。

CREATE EXTENSION IF NOT EXISTS vector;  
  
CREATE INDEX ON document USING hnsw (embedding vector_cosine_ops);

混合检索

混合检索顾名思义就是检索结果中,既包含全文检索,又包含向量语义检索。混合检索的关键是通过算法将这两类检索的结果融合在一起,而最常用的算法为 RRF

create or replace function hybrid_search(
  query_text text,
  query_embedding vector(512),
  match_count int,
  full_text_weight float = 1,
  semantic_weight float = 1,
  rrf_k int = 50
)
returns setof documents
language sql
as $$
with full_text as (
  select
    id,
    row_number() over(ORDER BY pgroonga_score(d.tableoid, d.ctid) DESC) as rank_ix
  from
    documents
  where
    fts &@~ query_text
  order by rank_ix
  limit least(match_count, 30) * 2
),
semantic as (
  select
    id,
    row_number() over (order by embedding <=> query_embedding) as rank_ix
  from
    documents
  order by rank_ix
  limit least(match_count, 30) * 2
)
select
  documents.*
from
  full_text
  full outer join semantic
    on full_text.id = semantic.id
  join documents
    on coalesce(full_text.id, semantic.id) = documents.id
order by
  coalesce(1.0 / (rrf_k + full_text.rank_ix), 0.0) * full_text_weight +
  coalesce(1.0 / (rrf_k + semantic.rank_ix), 0.0) * semantic_weight
  desc
limit
  least(match_count, 30)
$$;

文档管理与组织模块

基于组织标签进行数据权限划分。

聊天记录模块

存储时间戳、对话角色(user 或 assistant)以及对话内容。

大纲
文件