Skip to content

Commit d26a521

Browse files
authored
docs: persona generator (#1664)
1 parent 74bb47a commit d26a521

File tree

9 files changed

+427
-18
lines changed

9 files changed

+427
-18
lines changed

docs/howtos/customizations/metrics/_write_your_own_metric.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ choose_evaluator_llm.md
3434
```python
3535
from ragas.llms import llm_factory
3636

37-
evaluator_llm = llm_factory('gpt-4o')
37+
evaluator_llm = llm_factory("gpt-4o")
3838
```
3939

4040
## Aspect Critic - Simple Criteria Scoring
@@ -56,7 +56,7 @@ from ragas.metrics import AspectCritic
5656
hallucinations_binary = AspectCritic(
5757
name="hallucinations_binary",
5858
definition="Did the model hallucinate or add any information that was not present in the retrieved context?",
59-
llm=evaluator_llm
59+
llm=evaluator_llm,
6060
)
6161

6262
await hallucinations_binary.single_turn_ascore(eval_dataset[0])
@@ -93,9 +93,7 @@ Now lets init the metric with the rubric and evaluator llm and evaluate the data
9393
from ragas.metrics import RubricsScoreWithoutReference
9494

9595
hallucinations_rubric = RubricsScoreWithoutReference(
96-
name="hallucinations_rubric",
97-
llm=evaluator_llm,
98-
rubrics=rubric
96+
name="hallucinations_rubric", llm=evaluator_llm, rubrics=rubric
9997
)
10098

10199
await hallucinations_rubric.single_turn_ascore(eval_dataset[0])
@@ -125,7 +123,6 @@ For our example, we need to to use LLMs to evaluate our metric so we will subcla
125123

126124
As for the implementation, we will use the [Faithfulness][ragas.metrics.Faithfulness] metric to evaluate our metric to measure the hallucinations with the formula
127125

128-
129126
$$
130127
\text{Hallucinations} = 1 - \text{Faithfulness}
131128
$$
@@ -144,19 +141,28 @@ import typing as t
144141
from ragas.callbacks import Callbacks
145142
from ragas.dataset_schema import SingleTurnSample
146143

144+
147145
@dataclass
148146
class HallucinationsMetric(MetricWithLLM, SingleTurnMetric):
149147
# name of the metric
150148
name: str = "hallucinations_metric"
151149
# we need to define the required columns for the metric
152-
_required_columns: t.Dict[MetricType, t.Set[str]] = field(default_factory=lambda: {MetricType.SINGLE_TURN: {"user_input", "response", "retrieved_contexts"}})
150+
_required_columns: t.Dict[MetricType, t.Set[str]] = field(
151+
default_factory=lambda: {
152+
MetricType.SINGLE_TURN: {"user_input", "response", "retrieved_contexts"}
153+
}
154+
)
153155

154156
def __post_init__(self):
155157
# init the faithfulness metric
156158
self.faithfulness_metric = Faithfulness(llm=self.llm)
157159

158-
async def _single_turn_ascore(self, sample: SingleTurnSample, callbacks: Callbacks) -> float:
159-
faithfulness_score = await self.faithfulness_metric.single_turn_ascore(sample, callbacks)
160+
async def _single_turn_ascore(
161+
self, sample: SingleTurnSample, callbacks: Callbacks
162+
) -> float:
163+
faithfulness_score = await self.faithfulness_metric.single_turn_ascore(
164+
sample, callbacks
165+
)
160166
return 1 - faithfulness_score
161167
```
162168

@@ -181,12 +187,8 @@ Now let's evaluate the entire dataset with the metrics we have created.
181187
from ragas import evaluate
182188

183189
results = evaluate(
184-
eval_dataset,
185-
metrics=[
186-
hallucinations_metric,
187-
hallucinations_rubric,
188-
hallucinations_binary
189-
],
190+
eval_dataset,
191+
metrics=[hallucinations_metric, hallucinations_rubric, hallucinations_binary],
190192
)
191193
```
192194

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
## Persona's in Testset Generation
2+
3+
You can add different persona's to the testset generation process by defining the [Persona][ragas.testset.persona.Persona] class with the name and role description of the different persona's that might be relevant to your use case and you want to generate testset for.
4+
5+
For example, for the [gitlab handbook](https://about.gitlab.com/handbook/) we might want to generate testset for different persona's like a new joinee, a manager, a senior manager, etc. And hence we will define them as follows:
6+
7+
1. New Joinee: Don't know much about the company and is looking for information on how to get started.
8+
2. Manager: Wants to know about the different teams and how they collaborate with each other.
9+
3. Senior Manager: Wants to know about the company vision and how it is executed.
10+
11+
Which we can define as follows:
12+
13+
14+
```python
15+
from ragas.testset.persona import Persona
16+
17+
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.")
18+
persona_manager = Persona(name="Manager", role_description="Wants to know about the different teams and how they collaborate with each other.")
19+
persona_senior_manager = Persona(name="Senior Manager", role_description="Wants to know about the company vision and how it is executed.")
20+
21+
personas = [persona_new_joinee, persona_manager, persona_senior_manager]
22+
personas
23+
```
24+
25+
26+
27+
28+
[Persona(name='New Joinee', role_description="Don't know much about the company and is looking for information on how to get started."),
29+
Persona(name='Manager', role_description='Wants to know about the different teams and how they collaborate with each other.'),
30+
Persona(name='Senior Manager', role_description='Wants to know about the company vision and how it is executed.')]
31+
32+
33+
34+
And then you can use these persona's in the testset generation process by passing them to the [TestsetGenerator][ragas.testset.generator.TestsetGenerator] class.
35+
36+
37+
```python
38+
from ragas.testset import TestsetGenerator
39+
from ragas.testset.graph import KnowledgeGraph
40+
from ragas.llms import llm_factory
41+
42+
# Load the knowledge graph
43+
kg = KnowledgeGraph.load("../../../../experiments/gitlab_kg.json")
44+
# Initialize the Generator LLM
45+
llm = llm_factory("gpt-4o-mini")
46+
47+
# Initialize the Testset Generator
48+
testset_generator = TestsetGenerator(knowledge_graph=kg, persona_list=personas, llm=llm)
49+
# Generate the Testset
50+
testset = testset_generator.generate(testset_size=10)
51+
testset
52+
53+
```
54+
55+
56+
```python
57+
testset.to_pandas().head()
58+
```
59+
60+
61+
62+
63+
<div>
64+
<style scoped>
65+
.dataframe tbody tr th:only-of-type {
66+
vertical-align: middle;
67+
}
68+
69+
.dataframe tbody tr th {
70+
vertical-align: top;
71+
}
72+
73+
.dataframe thead th {
74+
text-align: right;
75+
}
76+
</style>
77+
<table border="1" class="dataframe">
78+
<thead>
79+
<tr style="text-align: right;">
80+
<th></th>
81+
<th>user_input</th>
82+
<th>reference_contexts</th>
83+
<th>reference</th>
84+
<th>synthesizer_name</th>
85+
</tr>
86+
</thead>
87+
<tbody>
88+
<tr>
89+
<th>0</th>
90+
<td>What the Director do in GitLab and how they wo...</td>
91+
<td>[09db4f3e-1c10-4863-9024-f869af48d3e0\n\ntitle...</td>
92+
<td>The Director at GitLab, such as the Director o...</td>
93+
<td>single_hop_specifc_query_synthesizer</td>
94+
</tr>
95+
<tr>
96+
<th>1</th>
97+
<td>Wht is the rol of the VP in GitLab?</td>
98+
<td>[56c84f1b-3558-4c80-b8a9-348e69a4801b\n\nJob F...</td>
99+
<td>The VP, or Vice President, at GitLab is respon...</td>
100+
<td>single_hop_specifc_query_synthesizer</td>
101+
</tr>
102+
<tr>
103+
<th>2</th>
104+
<td>What GitLab do for career progression?</td>
105+
<td>[ead619a5-930f-4e2b-b797-41927a04d2e3\n\nGoals...</td>
106+
<td>The Job frameworks at GitLab help team members...</td>
107+
<td>single_hop_specifc_query_synthesizer</td>
108+
</tr>
109+
<tr>
110+
<th>3</th>
111+
<td>Wht is the S-grop and how do they work with ot...</td>
112+
<td>[42babb12-b033-493f-b684-914e2b1b1d0f\n\nPeopl...</td>
113+
<td>Members of the S-group are expected to demonst...</td>
114+
<td>single_hop_specifc_query_synthesizer</td>
115+
</tr>
116+
<tr>
117+
<th>4</th>
118+
<td>How does Google execute its company vision?</td>
119+
<td>[c3ed463d-1cdc-4ba4-a6ca-2c4ab12da883\n\nof mo...</td>
120+
<td>To effectively execute the company vision, man...</td>
121+
<td>single_hop_specifc_query_synthesizer</td>
122+
</tr>
123+
</tbody>
124+
</table>
125+
</div>
126+
127+
128+
129+
## Automatic Persona Generation
130+
131+
If you want to automatically generate persona's from a knowledge graph, you can use the [generate_personas_from_kg][ragas.testset.persona.generate_personas_from_kg] function.
132+
133+
134+
135+
```python
136+
from ragas.testset.persona import generate_personas_from_kg
137+
from ragas.testset.graph import KnowledgeGraph
138+
from ragas.llms import llm_factory
139+
140+
kg = KnowledgeGraph.load("../../../../experiments/gitlab_kg.json")
141+
llm = llm_factory("gpt-4o-mini")
142+
143+
personas = generate_personas_from_kg(kg=kg, llm=llm, num_personas=5)
144+
```
145+
146+
147+
```python
148+
personas
149+
```
150+
151+
152+
153+
154+
[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.'),
155+
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.'),
156+
Persona(name='Product Pricing Analyst', role_description='Responsible for developing and analyzing pricing strategies that align with customer needs and market demands.'),
157+
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.'),
158+
Persona(name='Security Operations Engineer', role_description="Works on enhancing security logging processes and ensuring compliance within GitLab's infrastructure.")]
159+
160+

0 commit comments

Comments
 (0)