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 BasePrompt

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

Overview

The base prompt module defines the abstract BasePrompt class and a concrete StringPrompt implementation for constructing and generating text completions through LLMs in the Ragas framework.

Description

This module provides the foundational prompt abstractions for the Ragas evaluation toolkit. BasePrompt is an abstract base class that defines the interface all Ragas prompts must implement, including generate for single completions and generate_multiple for producing multiple outputs. It also provides serialization support through save and load methods that persist prompt metadata (language, version, and original hash) to JSON files. The module includes version compatibility checking when loading prompts saved with different Ragas versions. StringPrompt is a concrete implementation that formats plain text prompts and delegates generation to the LLM via StringPromptValue from LangChain. It supports both single and multiple generation, flattening multi-generation results into a list of strings. The module also defines two simple Pydantic I/O models: StringIO (wrapping a text string) and BoolIO (wrapping a boolean value), both with custom __hash__ implementations for use as dictionary keys or in sets.

Usage

Use BasePrompt as a parent class when building custom prompt types for Ragas metrics or test generation. Use StringPrompt directly when you need a simple text-in, text-out prompt without Pydantic model validation on input/output.

Code Reference

Source Location

Signature

class BasePrompt(ABC):
    def __init__(
        self,
        name: t.Optional[str] = None,
        language: str = "english",
        original_hash: t.Optional[str] = None,
    ):

class StringIO(BaseModel):
    text: str

class BoolIO(BaseModel):
    value: bool

class StringPrompt(BasePrompt):
    async def generate(
        self,
        llm: BaseRagasLLM,
        data: str,
        temperature: t.Optional[float] = None,
        stop: t.Optional[t.List[str]] = None,
        callbacks: Callbacks = [],
    ) -> str:

Import

from ragas.prompt.base import BasePrompt, StringPrompt, StringIO, BoolIO

I/O Contract

Inputs (BasePrompt.__init__)

Name Type Required Description
name str No Name of the prompt; auto-generated from class name using camel_to_snake if not provided
language str No Language for the prompt; defaults to "english"
original_hash str No Hash of the original prompt for tracking modifications

Inputs (StringPrompt.generate)

Name Type Required Description
llm BaseRagasLLM Yes The language model to use for text generation
data str Yes The text string to use as the prompt content
temperature float No The temperature for text generation
stop List[str] No Stop sequences for text generation
callbacks Callbacks No Callbacks to use during text generation; defaults to empty list

Outputs

Name Type Description
StringPrompt.generate return str The generated text from the LLM
StringPrompt.generate_multiple return List[str] A list containing n generated text outputs

Key Methods

Method Description
BasePrompt.generate(llm, data, ...) Abstract method for single completion generation
BasePrompt.generate_multiple(llm, data, n, ...) Abstract method for generating n completions
BasePrompt.save(file_path) Saves prompt metadata (version, language, hash) to a JSON file; raises FileExistsError if file already exists
BasePrompt.load(file_path) Class method to load a prompt from a JSON file with version compatibility warning

Usage Examples

Basic Usage

from ragas.prompt.base import StringPrompt

# Create a StringPrompt instance
prompt = StringPrompt(name="my_prompt", language="english")

# Generate text using an LLM
result = await prompt.generate(
    llm=my_llm,
    data="Explain the concept of faithfulness in RAG systems.",
    temperature=0.7,
)
print(result)

Multiple Generation

from ragas.prompt.base import StringPrompt

prompt = StringPrompt(name="diversity_prompt")

# Generate multiple distinct outputs
results = await prompt.generate_multiple(
    llm=my_llm,
    data="List creative uses for language models.",
    n=3,
    temperature=0.9,
)
for i, text in enumerate(results):
    print(f"Output {i+1}: {text}")

Saving and Loading

from ragas.prompt.base import StringPrompt

prompt = StringPrompt(name="my_prompt", language="french")

# Save prompt metadata
prompt.save("/path/to/my_prompt.json")

# Load prompt from file
loaded_prompt = StringPrompt.load("/path/to/my_prompt.json")
print(loaded_prompt.language)  # "french"

Related Pages

Page Connections

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