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 DynamicFewShotPrompt

From Leeroopedia
Knowledge Sources
Domains Prompts, Few-Shot Learning, Embeddings
Last Updated 2026-02-12 00:00 GMT

Overview

Implements a dynamic few-shot prompt that selects the most relevant examples at runtime using embedding-based cosine similarity, extending the base Prompt class with an in-memory example store.

Description

The dynamic_few_shot.py module provides a few-shot prompting system that dynamically selects the most relevant examples for a given input, rather than using a fixed set of examples. It contains three main classes:

SimpleExampleStore is an abstract base class defining the interface for example stores with two methods: get_examples (retrieve similar examples) and add_example (store a new example).

SimpleInMemoryExampleStore is the concrete implementation that stores examples as (input_dict, output_dict) tuples alongside their embedding vectors. When an embedding model is provided, it serializes input dictionaries to text (key-value pairs joined by newlines) and generates embedding vectors via embed_query. Retrieval uses cosine similarity computed via numpy: the query embedding is compared against all stored embeddings, and only examples exceeding the similarity_threshold are returned, sorted by similarity. When no embedding model is available, it falls back to returning the most recent examples.

DynamicFewShotPrompt extends the base Prompt class. At construction time, it creates a SimpleInMemoryExampleStore with the provided embedding model and populates it with the initial examples. The format() method takes keyword arguments, retrieves the top-k most similar examples from the store based on those kwargs, and constructs a formatted prompt string containing the instruction (with variable substitution) followed by the retrieved examples in a structured "Example N: Input/Output" format.

The class supports serialization via save() and load() methods. Saved files use JSON format (with optional gzip compression) and store the instruction, examples, embeddings, configuration parameters, and metadata about the response_model and embedding_model. The load() classmethod requires the embedding_model and response_model to be provided separately since they cannot be serialized.

The from_prompt() classmethod provides a convenient way to convert an existing Prompt instance into a DynamicFewShotPrompt by supplying an embedding model.

Usage

Import this module when you need few-shot prompting where the examples should be dynamically selected based on similarity to the current input, rather than using a static set of examples. This is particularly useful when you have a large pool of examples and want to select the most relevant ones for each specific input.

Code Reference

Source Location

Signature

class SimpleExampleStore(ABC):
    @abstractmethod
    def get_examples(self, data: t.Dict, top_k: int = 5) -> t.List[t.Tuple[t.Dict, t.Dict]]: ...
    @abstractmethod
    def add_example(self, input: t.Dict, output: t.Dict) -> None: ...

class SimpleInMemoryExampleStore(SimpleExampleStore):
    def __init__(self, embedding_model=None): ...
    def add_example(self, input: t.Dict, output: t.Dict) -> None: ...
    def get_examples(self, data: t.Dict, top_k: int = 5, threshold: float = 0.7) -> t.List[t.Tuple[t.Dict, t.Dict]]: ...

class DynamicFewShotPrompt(Prompt):
    def __init__(
        self,
        instruction: str,
        examples: t.Optional[t.List[t.Tuple[t.Dict, t.Dict]]] = None,
        response_model: t.Optional[BaseModel] = None,
        embedding_model: t.Optional[BaseEmbedding] = None,
        max_similar_examples: int = 3,
        similarity_threshold: float = 0.7,
    ): ...
    def format(self, **kwargs) -> str: ...
    def add_example(self, input: t.Dict, output: t.Dict) -> None: ...
    @classmethod
    def from_prompt(cls, prompt: Prompt, embedding_model: BaseEmbedding, max_similar_examples: int = 3, similarity_threshold: float = 0.7) -> "DynamicFewShotPrompt": ...
    def save(self, path: str, include_embeddings: bool = True) -> None: ...
    @classmethod
    def load(cls, path: str, response_model=None, embedding_model=None) -> "DynamicFewShotPrompt": ...

Import

from ragas.prompt.dynamic_few_shot import DynamicFewShotPrompt
from ragas.prompt.dynamic_few_shot import SimpleExampleStore, SimpleInMemoryExampleStore

I/O Contract

DynamicFewShotPrompt Constructor Inputs

Name Type Required Description
instruction str Yes The prompt instruction template with placeholders (e.g., {response}, {expected_answer})
examples List[Tuple[Dict, Dict]] or None No Initial list of (input_dict, output_dict) pairs for the example store
response_model BaseModel or None No Pydantic model for response validation
embedding_model BaseEmbedding or None No Embedding model for similarity calculations; if None, falls back to recency-based selection
max_similar_examples int No (default 3) Maximum number of similar examples to include in the formatted prompt
similarity_threshold float No (default 0.7) Minimum cosine similarity threshold (0.0-1.0) for including examples

format() Inputs

Name Type Required Description
**kwargs Any Yes Keyword arguments used to fill instruction placeholders and as the query for example retrieval

format() Outputs

Name Type Description
return str The fully formatted prompt string with instruction and dynamically selected examples

save() Inputs

Name Type Required Description
path str Yes File path to save to; use .gz extension for gzip compression
include_embeddings bool No (default True) Whether to include pre-computed embeddings in the saved file

load() Inputs

Name Type Required Description
path str Yes File path to load from; supports .gz compressed files
response_model BaseModel or None No Pydantic model for response validation (required if original had one)
embedding_model BaseEmbedding or None No Embedding model for similarity calculations (required for similarity-based retrieval)

load() Outputs

Name Type Description
return DynamicFewShotPrompt The loaded and configured prompt instance

Usage Examples

Basic Dynamic Few-Shot Prompt

from ragas.prompt.dynamic_few_shot import DynamicFewShotPrompt
from ragas.embeddings.base import embedding_factory

# Create an embedding model
embedder = embedding_factory("openai", model="text-embedding-3-small", client=openai_client)

# Define examples
examples = [
    ({"question": "What is Python?"}, {"answer": "A programming language"}),
    ({"question": "What is Java?"}, {"answer": "A programming language by Oracle"}),
    ({"question": "What is the sun?"}, {"answer": "A star at the center of our solar system"}),
]

# Create prompt
prompt = DynamicFewShotPrompt(
    instruction="Answer the following question: {question}",
    examples=examples,
    embedding_model=embedder,
    max_similar_examples=2,
    similarity_threshold=0.5,
)

# Format with dynamic example selection
result = prompt.format(question="What is JavaScript?")
# The prompt will include the most similar examples (likely the programming language ones)

Convert From Existing Prompt

from ragas.prompt.simple_prompt import Prompt
from ragas.prompt.dynamic_few_shot import DynamicFewShotPrompt

base_prompt = Prompt(
    instruction="Evaluate: {response}",
    examples=[
        ({"response": "Good answer"}, {"score": "5"}),
        ({"response": "Bad answer"}, {"score": "1"}),
    ],
)

dynamic_prompt = DynamicFewShotPrompt.from_prompt(
    prompt=base_prompt,
    embedding_model=embedder,
    max_similar_examples=3,
)

Save and Load

# Save prompt (embeddings and examples are serialized)
prompt.save("my_prompt.json")

# Save with compression
prompt.save("my_prompt.json.gz")

# Load (must re-provide embedding_model and response_model)
loaded_prompt = DynamicFewShotPrompt.load(
    "my_prompt.json",
    embedding_model=embedder,
)

Related Pages

Page Connections

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