Ragas 使用预分块数据生成测试集的方法
介绍 Ragas 的 generate_with_chunks 方法,它跳过内部文档切分,直接采用用户已有的分块作为节点,并说明其对分块策略一致性和元数据保留的作用及边界情况处理。
## 使用预切数据生成测试集 当您已经有一个定义明确的块化策略时, Ragas 允许您绕过其内部文件分割机制, 直接使用自己的块。 这在下列情况下特别有用: - 你已经优化了你的块策略 对于你的具体域 - 您想要保持您的 RAG 管道与评价的一致性 - 您已有自定义元数据预处理的文档 - 您需要确保块符合特定的业务逻辑或文档结构 ## 概览 那个... `generate_with_chunks` 方法 `TestsetGenerator` 接受预切数据,并将每个块作为 `NodeType.CHUNK` 直接跳过内部分割变换。 这意味着您的块与您所提供的完全一样, 既保留内容, 也保留元数据的完整性 。 ## 如何运作 使用时 `generate_with_chunks`,拉加斯: (原始内容存档于2018-09-21). 1. **接受您的块** as- is(既无 `Document` 对象或字符串) 2. **应用提取器** 喜欢 `SummaryExtractor`, `ThemesExtractor`, `NERExtractor`,以及 `EmbeddingExtractor` 以额外的属性丰富每个块 3. **构建关系** 使用块之间 `CosineSimilarityBuilder` 财务报告和财务报告 `OverlapScoreBuilder` 4. **生成人物为** 根据内容主题 5. **创建假想** 用于不同查询类型(单跳、多跳) 6. **合成测试样本** 包括问题、背景和参考答案 ## 示例: 使用预切文档 您可以通过LangChain列表 `Document` 对象。此方法保存您块的元数据,可用于跟踪源文档或其他自定义信息。 ``` import os from langchain_core.documents import Document from ragas.testset.synthesizers.generate import TestsetGenerator from ragas.llms import llm_factory from ragas.embeddings import OpenAIEmbeddings from openai import OpenAI # Initialize OpenAI client client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY")) # Initialize generator with your preferred models generator = TestsetGenerator( llm=llm_factory("gpt-4o-mini", client=client), embedding_model=OpenAIEmbeddings(client=client), ) # Your pre-chunked documents chunks = [ Document( page_content="""The Eiffel Tower (Tour Eiffel) is a wrought-iron lattice tower on the Champ de Mars in Paris, France. It is named after the engineer Gustave Eiffel, whose company designed and built the tower. Locally nicknamed "La Dame de Fer" (French for "The Iron Lady"), it was constructed from 1887 to 1889 as the centerpiece of the 1889 World's Fair. Although initially criticized by some of France's leading artists and intellectuals for its design, it has since become a global cultural icon of France and one of the most recognizable structures in the world.""", metadata={"source": "doc1", "chunk_id": 1}, ), Document( page_content="""The tower is 330 metres (1,083 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft).""", metadata={"source": "doc1", "chunk_id": 2}, ), ] # Generate testset testset = generator.generate_with_chunks(chunks=chunks, testset_size=10) # Save to CSV output_file = "testset.csv" testset.to_csv(output_file) print(f"Testset saved to {output_file}") print(testset.to_pandas().head()) ``` ### 生成过程 在生成过程中,你会看到显示各种转化和合成阶段的进度日志: ``` Applying SummaryExtractor: 100%|████████████████████████████████| 2/2 [00:07<00:00, 3.67s/it] Applying CustomNodeFilter: 100%|█████████████████████████████| 2/2 [00:00<00:00, 2226.87it/s] Applying EmbeddingExtractor: 100%|███████████████████████████| 2/2 [00:02<00:00, 1.19s/it] Applying ThemesExtractor: 100%|██████████████████████████████| 2/2 [00:06<00:00, 3.07s/it] Applying NERExtractor: 100%|█████████████████████████████████| 2/2 [00:06<00:00, 3.10s/it] Applying CosineSimilarityBuilder: 100%|█████████████████████| 1/1 [00:00<00:00, 613.29it/s] Applying OverlapScoreBuilder: 100%|████████████████████████| 1/1 [00:00<00:00, 1491.57it/s] Generating personas: 100%|███████████████████████████████████| 2/2 [00:05<00:00, 2.77s/it] Generating Scenarios: 100%|██████████████████████████████████| 2/2 [00:08<00:00, 4.19s/it] Generating Samples: 100%|████████████████████████████████| 11/11 [00:45<00:00, 4.13s/it] Testset saved to testset.csv ``` 测试集包括不同类型的查询: - **单跳查询**:可以从一个块中解答的问题 -- **多跳查询**: 需要多个块提供信息的问题(当存在关系时) ## 示例:使用普通字符串 如果您不需要保存元数据, 您也可以直接通过普通字符串 : ``` from ragas.testset.synthesizers.generate import TestsetGenerator from ragas.llms import llm_factory from ragas.embeddings import OpenAIEmbeddings from openai import OpenAI # Initialize models client = OpenAI() generator = TestsetGenerator( llm=llm_factory("gpt-4o-mini", client=client), embedding_model=OpenAIEmbeddings(client=client), ) # Simple text chunks text_chunks = [ "Artificial Intelligence (AI) is the simulation of human intelligence by machines. It involves machine learning, natural language processing, and computer vision.", "Machine Learning is a subset of AI that enables systems to learn from data without explicit programming. Popular algorithms include neural networks and decision trees.", "Deep Learning uses neural networks with multiple layers to process complex patterns in large datasets. It powers modern applications like image recognition and language translation.", ] # Generate testset testset = generator.generate_with_chunks(chunks=text_chunks, testset_size=5) # Save to CSV output_file = "testset.csv" testset.to_csv(output_file) print(f"Testset saved to {output_file}") print(testset.to_pandas()) ``` ## 处理边缘案件 - **空内容**: 空或白空的块 `page_content` 将被自动过滤出。 - **空序列**:如果提供空块序列,则生成一个空的测试集。 11月 13, 2026 12月 22, 2025
查看原始来源