Implementation:Explodinggradients Ragas SimplePrompt Class
| Field | Value |
|---|---|
| source | Explodinggradients_Ragas (GitHub) |
| domains | Prompts, Framework |
| last_updated | 2026-02-10 00:00 GMT |
Overview
The Prompt class provides a simple few-shot prompting system that uses Python string formatting with optional examples, response model validation, and JSON-based persistence with optional gzip compression.
Description
Prompt stores an instruction template string with f-string placeholders, an optional list of (input_dict, output_dict) example pairs for few-shot learning, and an optional Pydantic response model. The format() method fills placeholders with provided keyword arguments and appends formatted examples. The add_example() method validates that both input and output are dictionaries before appending. The save() method serializes to JSON (or gzip-compressed JSON with a .gz extension), warning that response_model cannot be serialized. The load() class method restores a prompt from file, requiring the response_model to be provided again if one was originally used. Schema validation ensures the provided model matches the saved schema.
Usage
Create a Prompt with an instruction template and optional examples, then call format() with keyword arguments to produce the final prompt string. Use save()/load() for persistence.
Code Reference
| Item | Detail |
|---|---|
| Source Location | src/ragas/prompt/simple_prompt.py L17-303
|
| Class Signature | class Prompt
|
| Key Methods | format(**kwargs), add_example(input, output), save(path), load(path, response_model)
|
| Import | from ragas.prompt.simple_prompt import Prompt
|
I/O Contract
Inputs
| Parameter | Type | Description |
|---|---|---|
instruction |
str |
Template string with {placeholder} syntax
|
examples |
Optional[List[Tuple[Dict, Dict]]] |
Few-shot (input_dict, output_dict) pairs |
response_model |
Optional[BaseModel] |
Pydantic model for response validation |
**kwargs (format) |
Any |
Values to fill template placeholders |
Outputs
| Method | Return Type | Description |
|---|---|---|
format() |
str |
Fully formatted prompt string with examples appended |
save() |
None |
Writes JSON or gzip-compressed JSON file |
load() |
Prompt |
Restored Prompt instance from file |
Usage Examples
from ragas.prompt.simple_prompt import Prompt
# Basic prompt with placeholders
prompt = Prompt("Answer the question: {question}")
formatted = prompt.format(question="What is 2+2?")
print(formatted) # "Answer the question: What is 2+2?"
# Prompt with few-shot examples
examples = [
({"question": "What is 1+1?"}, {"answer": "2"}),
({"question": "What is 3+3?"}, {"answer": "6"}),
]
prompt = Prompt("Answer: {question}", examples=examples)
formatted = prompt.format(question="What is 5+5?")
# Adding examples dynamically
prompt.add_example(
{"question": "What is 7+7?"},
{"answer": "14"},
)
# Save and load
prompt.save("my_prompt.json")
loaded = Prompt.load("my_prompt.json")
# With gzip compression
prompt.save("prompt.json.gz")
loaded = Prompt.load("prompt.json.gz")
Related Pages
- DynamicFewShotPrompt_Class - Extends Prompt with similarity-based example selection
- PydanticPrompt_Class - Structured alternative with Pydantic models
- BasePrompt_Class - Abstract base in the prompt hierarchy