人物角色生成
测试集生成中的人物角色
你可以通过定义 [Persona][ragas.testset.persona.Persona] 类,包含对你的用例相关且你希望为其生成测试集的不同人物角色的名称和角色描述,从而在测试集生成过程中添加不同的人物角色。
例如,对于 [gitlab 手册](https://about.gitlab.com/handbook/),我们可能希望为不同的人物角色(如新员工、经理、高级经理等)生成测试集。因此,我们可以按如下方式定义它们:
- 新员工:对公司了解不多,正在寻找入门信息。
- 经理:想了解不同的团队以及他们如何相互协作。
- 高级经理:想了解公司愿景以及如何执行。
我们可以将其定义如下
from ragas.testset.persona import Persona
persona_new_joinee = Persona(
name="New Joinee",
role_description="Don't know much about the company and is looking for information on how to get started.",
)
persona_manager = Persona(
name="Manager",
role_description="Wants to know about the different teams and how they collaborate with each other.",
)
persona_senior_manager = Persona(
name="Senior Manager",
role_description="Wants to know about the company vision and how it is executed.",
)
personas = [persona_new_joinee, persona_manager, persona_senior_manager]
personas
[Persona(name='New Joinee', role_description="Don't know much about the company and is looking for information on how to get started."),
Persona(name='Manager', role_description='Wants to know about the different teams and how they collaborate with each other.'),
Persona(name='Senior Manager', role_description='Wants to know about the company vision and how it is executed.')]
然后,你可以将这些人物角色传递给 [TestsetGenerator][ragas.testset.generator.TestsetGenerator] 类,在测试集生成过程中使用它们。
from ragas.testset import TestsetGenerator
from ragas.testset.graph import KnowledgeGraph
from ragas.llms import llm_factory
# Load the knowledge graph
kg = KnowledgeGraph.load("../../../../experiments/gitlab_kg.json")
# Initialize the Generator LLM
llm = llm_factory("gpt-4o-mini")
# Initialize the Testset Generator
testset_generator = TestsetGenerator(knowledge_graph=kg, persona_list=personas, llm=llm)
# Generate the Testset
testset = testset_generator.generate(testset_size=10)
testset
用户输入 | 参考上下文 | 参考答案 | 合成器名称 | |
---|---|---|---|---|
0 | GitLab 的总监做什么以及他们如何工作... | [09db4f3e-1c10-4863-9024-f869af48d3e0\n\n标题... | GitLab 的总监,例如...总监 | 单跳特定查询合成器 |
1 | GitLab 副总裁的职责是什么? | [56c84f1b-3558-4c80-b8a9-348e69a4801b\n\n职位框... | GitLab 的副总裁 (VP) 负责... | 单跳特定查询合成器 |
2 | GitLab 如何助力职业发展? | [ead619a5-930f-4e2b-b797-41927a04d2e3\n\n目标... | GitLab 的职位框架帮助团队成员... | 单跳特定查询合成器 |
3 | S-group 是什么,他们如何与其他人合作... | [42babb12-b033-493f-b684-914e2b1b1d0f\n\n人员... | S-group 的成员需要展现... | 单跳特定查询合成器 |
4 | 谷歌如何执行其公司愿景? | [c3ed463d-1cdc-4ba4-a6ca-2c4ab12da883\n\n更多... | 为了有效执行公司愿景,管理人员... | 单跳特定查询合成器 |
自动人物角色生成
如果你想从知识图谱自动生成人物角色,你可以使用 [generate_personas_from_kg][ragas.testset.persona.generate_personas_from_kg] 函数。
from ragas.testset.persona import generate_personas_from_kg
from ragas.testset.graph import KnowledgeGraph
from ragas.llms import llm_factory
kg = KnowledgeGraph.load("../../../../experiments/gitlab_kg.json")
llm = llm_factory("gpt-4o-mini")
personas = generate_personas_from_kg(kg=kg, llm=llm, num_personas=5)
[Persona(name='Organizational Development Manager', role_description='Responsible for implementing job frameworks and career development strategies to enhance employee growth and clarify roles within the company.'),
Persona(name='DevSecOps Product Manager', role_description='Responsible for overseeing the development and strategy of DevSecOps solutions, ensuring alignment with company goals and user needs.'),
Persona(name='Product Pricing Analyst', role_description='Responsible for developing and analyzing pricing strategies that align with customer needs and market demands.'),
Persona(name='Site Reliability Engineer', role_description='Responsible for maintaining service reliability and performance, focusing on implementing rate limits to prevent outages and enhance system stability.'),
Persona(name='Security Operations Engineer', role_description="Works on enhancing security logging processes and ensuring compliance within GitLab's infrastructure.")]