pky_ceshi
政务便民 RAG 系统的模块化封装,基于 LangChain + Chroma,把 11 个检索增强(RAG)环节拆成可单独调用的函数/类:
- 稠密向量编码(DashScope OpenAI 兼容 Embedding)
- BM25 稀疏检索 + jieba 中文分词
- 四路多路召回(EnsembleRetriever)
- 查询改写(Query Rewrite)
- 空上下文兜底(Null Guard)
- 文档切分(RecursiveCharacterTextSplitter)
- 入库去重检测
- 加权 RRF 融合
- RAG 提示模板
- LCEL 链式流水线
- 文档入库 / 列表 / 删除 / 问答等主流程封装
安装
pip install pky_ceshi
需要 Python 3.11 及以上。首次运行会在包目录下创建 data/chroma_db 与 data/documents 存放向量库和原始文档。
环境变量
在 .env 或进程环境中配置(代码启动时用 python-dotenv 自动读取 .env):
| 变量 | 用途 |
|---|---|
DASHSCOPE_API_KEY |
Embedding 接口密钥 |
DASHSCOPE_BASE_URL |
Embedding 接口地址(DashScope OpenAI 兼容地址) |
AGNES_API_KEY |
对话模型接口密钥 |
AGNES_BASE_URL |
对话模型接口地址 |
模型名在模块内固定:Embedding 为 qwen3.7-text-embedding,对话模型为 agnes-2.5-flash。
最小用法
import modular_rag as rag
db = rag.init_db() # 初始化向量库(Chroma,集合 govai)
# 把 data/documents 下的文本文件切分入库(重复文件自动跳过)
print(rag.document_insert(db, "政务便民指南.txt"))
print(rag.document_list(db)) # 查看已入库文件
# 问答:先做查询改写,再向量 + BM25 双路召回、RRF 融合后交给 LLM
answer = rag.query_from_knowledge(db, "如何提取公积金")
print(answer)
# 删除文档(同时删除向量与本地原文)
print(rag.document_delete(db, "政务便民指南.txt"))
其他常用接口
from modular_rag import (
DashScopeEmbedding, BM25RetrieverBuilder, build_ensemble_retriever,
rewrite_query, guard_empty_context, split_documents, rrf_merge, build_rag_chain,
)
emb = DashScopeEmbedding()
vec = emb.embed_query("医保报销比例") # 单条向量
docs = split_documents("长文本……", "a.txt") # 递归切分,块大小 1024 / 重叠 100
注意:document_insert 依据 data/documents 目录下的文件名读取文件;Embedding 与对话模型都需要可用的外部 API 才能实际返回结果。
Metadata
Release files for pky-ceshi 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pky_ceshi-0.1.0.tar.gz | 9.3 kB | Without provenance | Details for pky_ceshi-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pky_ceshi-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for pky_ceshi-0.1.0-py3-none-any.whl |
Total release size: 18.9 kB
Release files / pky_ceshi-0.1.0.tar.gz
| Download URL | pky_ceshi-0.1.0.tar.gz |
|---|---|
| Size | 9.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e784387806c1bfebbe8e56867f2b2df4219eb71470475fb77774d236059f2b22
|
|
BLAKE2b-256 checksum How to use checksums |
8740bb2532aa7f3f8266b24ca9263ceb1799d4d1182454999967b45facff971a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.15
|
Release files / pky_ceshi-0.1.0-py3-none-any.whl
| Download URL | pky_ceshi-0.1.0-py3-none-any.whl |
|---|---|
| Size | 9.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7e4972531fe6a9657d2114a77251c854f05f72437a9d04f2ce3eec4ef552e5e8
|
|
BLAKE2b-256 checksum How to use checksums |
e8872df4060d6cb6ec3d053dad18b5a8b7e31dd02939d66059fd9e3291987fb7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.15
|
Security for pky-ceshi 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.