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:Vibrantlabsai Ragas PromptMixin

From Leeroopedia
Knowledge Sources
Domains Prompt Engineering, Evaluation, Metrics
Last Updated 2026-02-12 00:00 GMT

Overview

PromptMixin is a mixin class that provides prompt management capabilities (get, set, adapt, save, and load) to any Ragas class that uses PydanticPrompt instances.

Description

PromptMixin is designed to be mixed into classes that contain PydanticPrompt instances as attributes, such as metrics (MetricWithLLM) and synthesizers (BaseSynthesizer). It provides a complete prompt lifecycle management interface. The _get_prompts private method uses Python's inspect.getmembers to discover all PydanticPrompt instances on the class, while get_prompts returns them indexed by their prompt name attribute. The set_prompts method validates that provided prompts exist and are PydanticPrompt instances before setting them on the object via setattr. The adapt_prompts method iterates through all prompts and calls each prompt's own adapt method to translate them to a given language using a provided LLM. The save_prompts and load_prompts methods handle serialization to and from JSON files in a specified directory, using the naming convention {name}_{prompt_name}_{language}.json (or {prompt_name}_{language}.json if the class name is empty). The mixin enables users to customize, persist, and share prompt configurations across evaluation runs and teams.

Usage

Use this mixin in any class that needs to expose its PydanticPrompt attributes for inspection, customization, language adaptation, or serialization. It is already mixed into Ragas metrics and synthesizer base classes.

Code Reference

Source Location

Signature

class PromptMixin:
    name: str = ""

    def get_prompts(self) -> t.Dict[str, PydanticPrompt]:
    def set_prompts(self, **prompts):
    async def adapt_prompts(
        self,
        language: str,
        llm: t.Union[BaseRagasLLM, InstructorBaseRagasLLM],
        adapt_instruction: bool = False,
    ) -> t.Dict[str, PydanticPrompt]:
    def save_prompts(self, path: str):
    def load_prompts(self, path: str, language: t.Optional[str] = None):

Import

from ragas.prompt.mixin import PromptMixin

I/O Contract

Inputs (set_prompts)

Name Type Required Description
**prompts PydanticPrompt Yes Keyword arguments mapping prompt names to PydanticPrompt instances

Inputs (adapt_prompts)

Name Type Required Description
language str Yes Target language to adapt prompts to (e.g., "spanish", "hindi")
llm Union[BaseRagasLLM, InstructorBaseRagasLLM] Yes The LLM to use for prompt adaptation/translation
adapt_instruction bool No Whether to adapt instruction text in addition to examples; defaults to False

Inputs (save_prompts / load_prompts)

Name Type Required Description
path str Yes Directory path for saving or loading prompt JSON files
language str No Language filter for loading prompts; defaults to "english" if not specified

Outputs

Name Type Description
get_prompts return Dict[str, PydanticPrompt] Dictionary mapping prompt names to their PydanticPrompt instances
adapt_prompts return Dict[str, PydanticPrompt] Dictionary mapping prompt names to their adapted (translated) PydanticPrompt instances
load_prompts return Dict[str, PydanticPrompt] Dictionary mapping prompt names to their loaded PydanticPrompt instances

Key Methods

Method Description
_get_prompts() Private method that uses inspect.getmembers to discover all PydanticPrompt attributes on the instance
get_prompts() Returns prompts indexed by their name attribute rather than their Python attribute name
set_prompts(**prompts) Validates and sets prompts on the instance; raises ValueError for unknown names or non-PydanticPrompt values
adapt_prompts(language, llm, adapt_instruction) Adapts all prompts to a target language via the LLM
save_prompts(path) Saves all prompts to JSON files in the specified directory
load_prompts(path, language) Loads prompts from JSON files in the specified directory

Usage Examples

Basic Usage

# Access prompts from a metric that uses PromptMixin
from ragas.metrics import Faithfulness

metric = Faithfulness(llm=my_llm)

# Get all prompts
prompts = metric.get_prompts()
for name, prompt in prompts.items():
    print(f"{name}: {prompt}")

# Set a custom prompt
metric.set_prompts(nli_statements_message=my_custom_prompt)

Language Adaptation

# Adapt prompts to a different language
adapted = await metric.adapt_prompts(
    language="hindi",
    llm=my_best_llm,
    adapt_instruction=True,
)

# Save adapted prompts
metric.set_prompts(**adapted)
metric.save_prompts("/path/to/prompts/")

# Load prompts later
loaded = metric.load_prompts("/path/to/prompts/", language="hindi")
metric.set_prompts(**loaded)

Related Pages

Page Connections

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