面向非英语语料的合成测试集生成
该教程演示如何用 ragas 从西班牙语维基百科语料生成合成查询测试集,包括加载文档、配置人物与变换、初始化生成器,并将查询提示适配到目标语言。
## 非英语测试集生成 ## 非英国体格的合成测试生成 在这个笔记本里,你会学会如何使合成测试数据生成 适应非英语的物理设置。为了这个教程,我正在从西班牙文维基百科的文章中生成西班牙语的查询。 ### 下载和装入工具箱 ``` ! git clone https://huggingface.co/datasets/vibrantlabsai/Sample_non_english_corpus ``` ``` Cloning into 'Sample_non_english_corpus'... remote: Enumerating objects: 12, done.[K remote: Counting objects: 100% (8/8), done.[K remote: Compressing objects: 100% (8/8), done.[K remote: Total 12 (delta 0), reused 0 (delta 0), pack-reused 4 (from 1)[K Unpacking objects: 100% (12/12), 11.43 KiB | 780.00 KiB/s, done. ``` ``` from langchain_community.document_loaders import DirectoryLoader, TextLoader path = "Sample_non_english_corpus/" loader = DirectoryLoader(path, glob="**/*.txt") docs = loader.load() ``` ``` /opt/homebrew/Caskroom/miniforge/base/envs/ragas/lib/python3.9/site-packages/requests/__init__.py:102: RequestsDependencyWarning: urllib3 (1.26.20) or chardet (5.2.0)/charset_normalizer (None) doesn't match a supported version! warnings.warn("urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn't match a supported " ``` ``` len(docs) ``` ``` 6 ``` ### 初始化所需模式 ``` from ragas.llms import LangchainLLMWrapper from ragas.embeddings import OpenAIEmbeddings from langchain_openai import ChatOpenAI import openai generator_llm = LangchainLLMWrapper(ChatOpenAI(model="gpt-4o-mini")) openai_client = openai.OpenAI() generator_embeddings = OpenAIEmbeddings(client=openai_client) ``` ``` /opt/homebrew/Caskroom/miniforge/base/envs/ragas/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm ``` ### 设置人物和变换 您可以使用此程序自动创建人物 [笔记本](<https://docs.ragas.io/en/stable/howtos/customizations/testgenerator/_persona_generator/>)。为了简单起见,我正在使用一个预先定义的人,两个基本变换和简单的查询分布。 ``` from ragas.testset.persona import Persona personas = [ Persona( name="curious student", role_description="A student who is curious about the world and wants to learn more about different cultures and languages", ), ] ``` ``` from ragas.testset.transforms.extractors.llm_based import NERExtractor from ragas.testset.transforms.splitters import HeadlineSplitter transforms = [HeadlineSplitter(), NERExtractor()] ``` ### 初始化测试生成器 ``` from ragas.testset import TestsetGenerator generator = TestsetGenerator( llm=generator_llm, embedding_model=generator_embeddings, persona_list=personas ) ``` ### 装入和调整查询 在这里,我们加载所需的查询类型,并适应目标语言. ``` from ragas.testset.synthesizers.single_hop.specific import ( SingleHopSpecificQuerySynthesizer, ) distribution = [ (SingleHopSpecificQuerySynthesizer(llm=generator_llm), 1.0), ] for query, _ in distribution: prompts = await query.adapt_prompts("spanish", llm=generator_llm) query.set_prompts(**prompts) ``` ### 生成 ``` dataset = generator.generate_with_langchain_docs( docs[:], testset_size=5, transforms=transforms, query_distribution=distribution, ) ``` ``` Applying HeadlineSplitter: 0%| | 0/6 [00:00<?, ?it/s]unable to apply transformation: 'headlines' property not found in this node unable to apply transformation: 'headlines' property not found in this node unable to apply transformation: 'headlines' property not found in this node unable to apply transformation: 'headlines' property not found in this node unable to apply transformation: 'headlines' property not found in this node unable to apply transformation: 'headlines' property not found in this node Generating Scenarios: 100%|██████████| 1/1 [00:07<00:00, 7.75s/it] Generating Samples: 100%|██████████| 5/5 [00:03<00:00, 1.65it/s] ``` ``` eval_dataset = dataset.to_evaluation_dataset() ``` ``` print("Query:", eval_dataset[0].user_input) print("Reference:", eval_dataset[0].reference) ``` ``` Query: Quelles sont les caractéristiques du Bronx en tant que borough de New York? Reference: Le Bronx est l'un des cinq arrondissements de New York, qui est la plus grande ville des États-Unis. Bien que le contexte ne fournisse pas de détails spécifiques sur le Bronx, il mentionne que New York est une ville cosmopolite avec de nombreux quartiers ethniques, ce qui pourrait inclure des caractéristiques culturelles variées présentes dans le Bronx. ``` 就是这样,你可以按照你的要求定制测试生成程序。 12月 16, 2025 12月 16, 2025
查看原始来源