Skip to content

Commit 5f57f52

Browse files
authored
Feature/updating prompt (#1050)
Editing prompt.py so that adaptation of testset_generator to dutch is possible; Added a output_parser for the question_answer_prompt, so that there are less nans with testset_generation
1 parent 710b384 commit 5f57f52

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/ragas/llms/prompt.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import logging
55
import os
6+
import ast
67
import typing as t
78

89
from langchain_core.messages import BaseMessage, HumanMessage
@@ -228,12 +229,14 @@ def get_all_keys(nested_json):
228229
example_dict.update(
229230
{k: v for k, v in zip(self.input_keys, example[: len(self.input_keys)])}
230231
)
231-
example_dict[self.output_key] = (
232-
json_loader._safe_load(example[-1], llm)
233-
if self.output_type.lower() == "json"
234-
else example[-1]
235-
)
236-
232+
if self.output_type.lower() == "json":
233+
example_dict[self.output_key] = json_loader._safe_load(example[-1], llm)
234+
if example_dict[self.output_key] == {}:
235+
# Extracting the dictionary part using string slicing
236+
dict_str = example[-1].split('(')[0].strip()
237+
example_dict[self.output_key ] = ast.literal_eval(dict_str)
238+
else:
239+
example_dict[self.output_key] = example[-1]
237240
if self.output_type.lower() == "json":
238241
output = example_dict[self.output_key]
239242
if isinstance(output, dict):

src/ragas/testset/prompts.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
from ragas.llms.output_parser import RagasoutputParser, get_json_format_instructions
44
from ragas.llms.prompt import Prompt
55

6+
7+
class AnswerFormat(BaseModel):
8+
answer: str
9+
verdict: int
10+
11+
12+
question_answer_parser = RagasoutputParser(pydantic_object=AnswerFormat)
13+
14+
615
reasoning_question_prompt = Prompt(
716
name="reasoning_question",
817
instruction="""Complicate the given question by rewriting question into a multi-hop reasoning question based on the provided context.
@@ -137,30 +146,28 @@
137146
question_answer_prompt = Prompt(
138147
name="answer_formulate",
139148
instruction="""Answer the question using the information from the given context. Output verdict as '1' if answer is present '-1' if answer is not present in the context.""",
149+
output_format_instruction=get_json_format_instructions(AnswerFormat),
140150
examples=[
141151
{
142152
"context": """Climate change is significantly influenced by human activities, notably the emission of greenhouse gases from burning fossil fuels. The increased greenhouse gas concentration in the atmosphere traps more heat, leading to global warming and changes in weather patterns.""",
143153
"question": "How do human activities contribute to climate change?",
144-
"answer": {
154+
"answer": AnswerFormat.parse_obj({
145155
"answer": "Human activities contribute to climate change primarily through the emission of greenhouse gases from burning fossil fuels. These emissions increase the concentration of greenhouse gases in the atmosphere, which traps more heat and leads to global warming and altered weather patterns.",
146-
"verdict": "1",
147-
},
156+
"verdict": "1",}).dict(),
148157
},
149158
{
150159
"context": """The concept of artificial intelligence (AI) has evolved over time, but it fundamentally refers to machines designed to mimic human cognitive functions. AI can learn, reason, perceive, and, in some instances, react like humans, making it pivotal in fields ranging from healthcare to autonomous vehicles.""",
151160
"question": "What are the key capabilities of artificial intelligence?",
152-
"answer": {
161+
"answer": AnswerFormat.parse_obj({
153162
"answer": "Artificial intelligence is designed to mimic human cognitive functions, with key capabilities including learning, reasoning, perception, and reacting to the environment in a manner similar to humans. These capabilities make AI pivotal in various fields, including healthcare and autonomous driving.",
154-
"verdict": "1",
155-
},
163+
"verdict": "1",}).dict(),
156164
},
157165
{
158166
"context": """The novel "Pride and Prejudice" by Jane Austen revolves around the character Elizabeth Bennet and her family. The story is set in the 19th century in rural England and deals with issues of marriage, morality, and misconceptions.""",
159167
"question": "What year was 'Pride and Prejudice' published?",
160-
"answer": {
168+
"answer": AnswerFormat.parse_obj({
161169
"answer": "The answer to given question is not present in context",
162-
"verdict": "-1",
163-
},
170+
"verdict": "-1",}).dict(),
164171
},
165172
],
166173
input_keys=["context", "question"],

0 commit comments

Comments
 (0)