本案例基于RagPlanner,搭建了一个简单的法律咨询智能体,通过检索民法典和刑法中的相关条例并结合案件背景给出相关的法律建议。
该案例基于千问大模型和DashScope的embedding和rerank功能,使用前需要您在环境变量中配置DASHSCOPE_API_KEY
。
法律知识库基于agentUniverse中的知识组件,通过将法律条款原文存储至ChromaDB和Sqlite中,构建方便智能体查阅检索的知识库。
法律书籍原本:
name: "law_knowledge"
description: "中国民法与刑法相关的知识库"
stores:
- "civil_law_chroma_store"
- "criminal_law_chroma_store"
- "civil_law_sqlite_store"
- "criminal_law_sqlite_store"
query_paraphrasers:
- "custom_query_keyword_extractor"
insert_processors:
- "recursive_character_text_splitter"
rag_router: "nlu_rag_router"
post_processors:
- "dashscope_reranker"
readers:
pdf: "default_pdf_reader"
metadata:
type: 'KNOWLEDGE'
module: 'sample_standard_app.app.core.knowledge.law_knowledge'
class: 'LawKnowledge'
为了方便您使用,我们已经将存储有相关信息的数据库文件放在该目录下,如果您想从头构建知识库的话,您可以运行如下代码:
from agentuniverse.base.agentuniverse import AgentUniverse
from agentuniverse.agent.action.knowledge.knowledge_manager import KnowledgeManager
if __name__ == '__main__':
AgentUniverse().start(config_path='../../config/config.toml', core_mode=True)
civil_store_list = ["civil_law_sqlite_store", "civil_law_chroma_store"]
criminal_store_list = ["criminal_law_sqlite_store", "criminal_law_chroma_store"]
law_knowledge = KnowledgeManager().get_instance_obj("law_knowledge")
law_knowledge.insert_knowledge(
source_path="../resources/刑法.pdf",
stores=criminal_store_list
)
law_knowledge.insert_knowledge(
source_path="../resources/民法典.pdf",
stores=civil_store_list
)
该智能体涉及如下两个文件:
- law_rag_agent.py: 定义了智能体的输入和输出
- law_rag_agent.yaml: 定义了智能体的相关提示词