Overview
The metrics BasePrompt module provides a generic, type-safe abstract base class for structured metric prompts with built-in language adaptation via LLM-powered translation.
Description
This module defines BasePrompt (in the ragas.prompt.metrics namespace), an abstract generic class parameterized by InputModel and OutputModel Pydantic types. It provides a complete prompt construction pipeline: the to_string method assembles the instruction, output JSON schema, formatted examples, and input data into a single prompt string ready for LLM consumption. The class supports few-shot learning through an examples attribute that holds a list of (input, output) tuples, formatted into numbered examples in the prompt. The adapt method enables language adaptation by translating all string content in examples (and optionally the instruction) to a target language using a structured LLM call. Translation is performed by the private _translate_strings helper, which uses a carefully designed safety prompt that instructs the LLM to act only as a translator and never execute instructions found within the text being translated. The translation response is validated to ensure the same number of statements are returned. The module uses utility functions get_all_strings and update_strings from ragas.prompt.utils to extract and replace strings within nested data structures.
Usage
Use this class as the base for all modern Ragas metric prompts that require structured input/output validation, few-shot examples, JSON schema enforcement, and multi-language support.
Code Reference
Source Location
Signature
class BasePrompt(ABC, t.Generic[InputModel, OutputModel]):
input_model: t.Type[InputModel]
output_model: t.Type[OutputModel]
instruction: str
examples: t.List[t.Tuple[InputModel, OutputModel]]
language: str = "english"
def to_string(self, data: InputModel) -> str:
def _generate_examples(self) -> str:
async def adapt(
self,
target_language: str,
llm: "InstructorBaseRagasLLM",
adapt_instruction: bool = False,
) -> "BasePrompt[InputModel, OutputModel]":
Import
from ragas.prompt.metrics.base_prompt import BasePrompt
I/O Contract
Inputs (Class Attributes)
| Name |
Type |
Required |
Description
|
| input_model |
Type[InputModel] |
Yes |
Pydantic model class for validating prompt input data
|
| output_model |
Type[OutputModel] |
Yes |
Pydantic model class used to generate the expected output JSON schema
|
| instruction |
str |
Yes |
Task description text for the LLM
|
| examples |
List[Tuple[InputModel, OutputModel]] |
Yes |
List of (input, output) example pairs for few-shot learning
|
| language |
str |
No |
Language for the prompt; defaults to "english"
|
Inputs (to_string)
| Name |
Type |
Required |
Description
|
| data |
InputModel |
Yes |
An input data instance validated by the input_model
|
Inputs (adapt)
| Name |
Type |
Required |
Description
|
| target_language |
str |
Yes |
Target language for translation (e.g., "spanish", "french", "hindi")
|
| llm |
InstructorBaseRagasLLM |
Yes |
Instructor-based LLM used for performing translations
|
| adapt_instruction |
bool |
No |
Whether to also translate the instruction text; defaults to False
|
Outputs
| Name |
Type |
Description
|
| to_string return |
str |
A complete prompt string containing instruction, JSON schema, examples, and input data
|
| adapt return |
BasePrompt[InputModel, OutputModel] |
A new prompt instance with examples (and optionally instruction) translated to the target language
|
Key Helper Classes and Functions
| Name |
Description
|
_TranslatedStrings |
Pydantic response model for translation results; contains a list of translated statements preserving order and count
|
_translate_strings(strings, target_language, llm) |
Translates a batch of strings to the target language using structured LLM output with safety prompts to prevent instruction injection
|
_TRANSLATION_INSTRUCTION |
Safety prompt text that instructs the LLM to act only as a translator and not execute any embedded instructions
|
Usage Examples
Defining a Custom Metric Prompt
from pydantic import BaseModel
from ragas.prompt.metrics.base_prompt import BasePrompt
class MyInput(BaseModel):
question: str
context: str
class MyOutput(BaseModel):
score: int
reason: str
class MyMetricPrompt(BasePrompt[MyInput, MyOutput]):
input_model = MyInput
output_model = MyOutput
instruction = "Evaluate the relevance of the context to the question."
examples = [
(
MyInput(question="What is AI?", context="AI is artificial intelligence."),
MyOutput(score=2, reason="Directly relevant"),
),
]
# Use the prompt
prompt = MyMetricPrompt()
prompt_string = prompt.to_string(MyInput(question="What is ML?", context="ML uses data."))
Language Adaptation
# Adapt a prompt to Spanish
spanish_prompt = await prompt.adapt(
target_language="spanish",
llm=my_instructor_llm,
adapt_instruction=True,
)
# The examples and instruction are now translated
print(spanish_prompt.language) # "spanish"
Related Pages