diff --git a/src/ragas/prompt/mixin.py b/src/ragas/prompt/mixin.py index 301797b7b..79e551298 100644 --- a/src/ragas/prompt/mixin.py +++ b/src/ragas/prompt/mixin.py @@ -53,7 +53,7 @@ def set_prompts(self, **prompts): setattr(self, key, value) async def adapt_prompts( - self, language: str, llm: BaseRagasLLM + self, language: str, llm: BaseRagasLLM, adapt_instruction: bool = False ) -> t.Dict[str, PydanticPrompt]: """ Adapts the prompts in the class to the given language and using the given LLM. @@ -67,7 +67,7 @@ async def adapt_prompts( prompts = self.get_prompts() adapted_prompts = {} for name, prompt in prompts.items(): - adapted_prompt = await prompt.adapt(language, llm) + adapted_prompt = await prompt.adapt(language, llm, adapt_instruction) adapted_prompts[name] = adapted_prompt return adapted_prompts diff --git a/src/ragas/prompt/pydantic_prompt.py b/src/ragas/prompt/pydantic_prompt.py index efeb38b20..65c1cf860 100644 --- a/src/ragas/prompt/pydantic_prompt.py +++ b/src/ragas/prompt/pydantic_prompt.py @@ -215,7 +215,7 @@ def process_output(self, output: OutputModel, input: InputModel) -> OutputModel: return output async def adapt( - self, target_language: str, llm: BaseRagasLLM + self, target_language: str, llm: BaseRagasLLM, adapt_instruction: bool = False ) -> "PydanticPrompt[InputModel, OutputModel]": """ Adapt the prompt to a new language. @@ -244,6 +244,16 @@ async def adapt( new_prompt = copy.deepcopy(self) new_prompt.examples = translated_examples new_prompt.language = target_language + + if adapt_instruction: + translated_instruction = await translate_statements_prompt.generate( + llm=llm, + data=ToTranslate( + target_language=target_language, statements=[self.instruction] + ), + ) + new_prompt.instruction = translated_instruction.statements[0] + return new_prompt def __repr__(self): @@ -422,7 +432,7 @@ class Translated(BaseModel): class TranslateStatements(PydanticPrompt[ToTranslate, Translated]): - instruction = "Translate the following statements to the target language." + instruction = "Translate the following statements to the target language. Ensure that the number of output data rows is equal to the number of input data rows." input_model = ToTranslate output_model = Translated examples = [