Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Explodinggradients Ragas MetricBasePrompt Class

From Leeroopedia


Field Value
source Explodinggradients_Ragas (GitHub)
domains Prompts, Metrics
last_updated 2026-02-10 00:00 GMT

Overview

The BasePrompt class in ragas.prompt.metrics.base_prompt is a generic abstract base for structured metric prompts with type-safe Pydantic input/output models and built-in internationalization (i18n) support through language adaptation.

Description

BasePrompt[InputModel, OutputModel] is an abstract generic class that defines class-level attributes: input_model, output_model, instruction, examples (list of input/output tuples), and language (default "english"). The to_string method assembles a complete prompt by combining the instruction, a JSON Schema specification for the output model, formatted examples, and the serialized input data. The _generate_examples helper formats example pairs into numbered text blocks. The adapt async method translates all strings in the examples to a target language using a private _translate_strings helper that calls an InstructorBaseRagasLLM with a structured _TranslatedStrings response model and safety-focused translation instructions. Optionally, the instruction itself can also be translated. The method returns a deep copy of the prompt with updated examples and language.

Usage

Subclass BasePrompt with concrete Pydantic input/output models and an instruction. Use to_string(data) to render the complete prompt for an LLM. Call adapt(target_language, llm) to produce a translated variant.

Code Reference

Item Detail
Source Location src/ragas/prompt/metrics/base_prompt.py L78-193
Class Signature class BasePrompt(ABC, Generic[InputModel, OutputModel])
Key Methods to_string(data), _generate_examples(), adapt(target_language, llm, adapt_instruction)
Import from ragas.prompt.metrics.base_prompt import BasePrompt

I/O Contract

Inputs

Parameter Type Description
input_model (class attr) Type[InputModel] Pydantic model class for input validation
output_model (class attr) Type[OutputModel] Pydantic model class for output schema generation
instruction (class attr) str Task description for the LLM
examples (class attr) List[Tuple[InputModel, OutputModel]] Few-shot example pairs
data (to_string) InputModel Input data instance to render into the prompt
target_language (adapt) str Target language for translation (e.g., "spanish", "hindi")

Outputs

Method Return Type Description
to_string() str Complete prompt string ready for LLM submission
adapt() BasePrompt Deep copy of the prompt with translated examples and language

Usage Examples

from pydantic import BaseModel
from ragas.prompt.metrics.base_prompt import BasePrompt

class EvalInput(BaseModel):
    response: str
    reference: str

class EvalOutput(BaseModel):
    score: float
    explanation: str

class CorrectnessPrompt(BasePrompt[EvalInput, EvalOutput]):
    input_model = EvalInput
    output_model = EvalOutput
    instruction = "Evaluate the correctness of the response against the reference."
    examples = [
        (
            EvalInput(response="Paris is the capital of France.", reference="The capital of France is Paris."),
            EvalOutput(score=1.0, explanation="The response correctly identifies Paris as the capital."),
        )
    ]

prompt = CorrectnessPrompt()
prompt_text = prompt.to_string(
    EvalInput(response="Berlin is the capital of Germany.", reference="Germany's capital is Berlin.")
)

# Adapt to Spanish
spanish_prompt = await prompt.adapt("spanish", llm=instructor_llm)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment