# 法律咨询案例
## 案例说明
本案例基于RagAgentTemplate，搭建了一个简单的法律咨询智能体，通过检索民法典和刑法中的相关条例并结合案件背景给出相关的法律建议。

该案例基于千问大模型和DashScope的embedding和rerank功能，使用前需要您在环境变量中配置`DASHSCOPE_API_KEY`。

## 构成
### 法律知识库
法律知识库基于agentUniverse中的[知识组件](../In-Depth_Guides/原理介绍/知识/知识.md)，通过将法律条款原文存储至ChromaDB和Sqlite中，构建方便智能体查阅检索的知识库。  
法律书籍原本：
- [民法典.pdf](../../../../examples/sample_standard_app/intelligence/agentic/knowledge/raw_knowledge_file/民法典.pdf)
- [刑法.pdf](../../../../examples/sample_standard_app/intelligence/agentic/knowledge/raw_knowledge_file/民法典.pdf)

### [知识定义](../../../../examples/sample_apps/rag_app/intelligence/agentic/knowledge/law_knowledge.yaml)
```yaml
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.intelligence.agentic.knowledge.law_knowledge'
  class: 'LawKnowledge'
```

### Reader组件
- [default_pdf_reader](../../../../agentuniverse/agent/action/knowledge/reader/file/pdf_reader.yaml)

### DocProcessor组件
- [custom_query_keyword_extractor](../../../../examples/sample_apps/rag_app/intelligence/agentic/knowledge/doc_processor/query_keyword_extractor.yaml)
- [recursive_character_text_splitter](../../../../agentuniverse/agent/action/knowledge/doc_processor/recursive_character_text_splitter.yaml)

### QueryParaphraser组件
- [custom_query_keyword_extractor](../../../../examples/sample_apps/rag_app/intelligence/agentic/knowledge/query_paraphraser/custom_query_keyword_extractor.yaml)

### RagRouter组件
- [nlu_rag_router](../../../../examples/sample_apps/rag_app/intelligence/agentic/knowledge/rag_router/nlu_rag_router.yaml)

### Store组件
- [civil_law_chroma_store](../../../../examples/sample_apps/rag_app/intelligence/agentic/knowledge/store/civil_law_chroma_store.yaml)
- [criminal_law_chroma_store](../../../../examples/sample_apps/rag_app/intelligence/agentic/knowledge/store/criminal_law_chroma_store.yaml)
- [civil_law_sqlite_store](../../../../examples/sample_apps/rag_app/intelligence/agentic/knowledge/store/civil_law_sqlite_store.yaml)
- [criminal_law_sqlite_store](../../../../examples/sample_apps/rag_app/intelligence/agentic/knowledge/store/criminal_law_sqlite_store.yaml)

为了方便您使用，我们已经将存储有相关信息的数据库文件放在[该目录下](../../../../examples/sample_apps/rag_app/db)，如果您想从头构建知识库的话，您可以运行如下代码：
```python
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.yaml](../../../../examples/sample_apps/rag_app/intelligence/agentic/agent/agent_instance/rag_agent_case/law_rag_agent.yaml): 定义了智能体的相关提示词


### 演示代码
[代码链接](../../../../examples/sample_apps/rag_app/intelligence/test/legal_advice_rag_agent.py)

## 效果演示
![演示图片](../../_picture/law_agent_demo.png)