Overview
FewShotPydanticPrompt extends PydanticPrompt with dynamic example selection using embedding-based similarity retrieval from an ExampleStore.
Description
FewShotPydanticPrompt is a dataclass that combines PydanticPrompt with a dynamic few-shot example retrieval system. Instead of using a fixed set of examples, it maintains an ExampleStore that retrieves the most relevant examples for each input based on embedding similarity. The module defines an abstract ExampleStore interface and a concrete InMemoryExampleStore implementation that uses cosine similarity over embeddings to find the top-k most relevant examples above a configurable similarity threshold. When generate_multiple is called, the prompt first queries the example store for the best matching examples, sets them as the current examples, tracks usage via Ragas analytics, and then delegates to the parent PydanticPrompt class for actual generation. The from_pydantic_prompt class method provides a convenient factory to convert an existing PydanticPrompt into a FewShotPydanticPrompt by migrating its static examples into an InMemoryExampleStore backed by the provided embeddings model.
Usage
Use this class when you have a large pool of prompt examples and want to dynamically select the most relevant ones for each input, improving prompt quality through targeted few-shot learning rather than using a fixed example set.
Code Reference
Source Location
Signature
class ExampleStore(ABC):
@abstractmethod
def get_examples(self, data: BaseModel, top_k: int = 5) -> t.Sequence[t.Tuple[BaseModel, BaseModel]]:
@abstractmethod
def add_example(self, input: BaseModel, output: BaseModel):
@dataclass
class InMemoryExampleStore(ExampleStore):
embeddings: BaseRagasEmbeddings
@dataclass
class FewShotPydanticPrompt(PydanticPrompt, t.Generic[InputModel, OutputModel]):
example_store: ExampleStore
top_k_for_examples: int = 5
threshold_for_examples: float = 0.7
Import
from ragas.prompt.few_shot_pydantic_prompt import (
FewShotPydanticPrompt,
ExampleStore,
InMemoryExampleStore,
)
I/O Contract
Inputs (FewShotPydanticPrompt)
| Name |
Type |
Required |
Description
|
| example_store |
ExampleStore |
Yes |
The store that holds and retrieves examples based on similarity
|
| top_k_for_examples |
int |
No |
Maximum number of examples to retrieve per query; defaults to 5
|
| threshold_for_examples |
float |
No |
Minimum cosine similarity threshold for example retrieval; defaults to 0.7
|
Inputs (InMemoryExampleStore)
| Name |
Type |
Required |
Description
|
| embeddings |
BaseRagasEmbeddings |
Yes |
Embedding model used to encode examples and compute similarity
|
Outputs
| Name |
Type |
Description
|
| generate_multiple return |
List[OutputModel] |
A list of output model instances generated using dynamically selected few-shot examples
|
| get_examples return |
Sequence[Tuple[BaseModel, BaseModel]] |
A sequence of (input, output) example pairs ranked by similarity
|
Key Methods
| Method |
Description
|
add_example(input, output) |
Adds a new example pair to the underlying example store
|
generate_multiple(llm, data, n, ...) |
Retrieves relevant examples from the store, tracks analytics, then delegates to PydanticPrompt for generation
|
from_pydantic_prompt(pydantic_prompt, embeddings) |
Class method that converts an existing PydanticPrompt into a FewShotPydanticPrompt by migrating its examples into an InMemoryExampleStore
|
InMemoryExampleStore.get_nearest_examples(...) |
Static method that computes cosine similarity and returns indices of top-k examples above the threshold
|
Usage Examples
Basic Usage
from ragas.prompt.few_shot_pydantic_prompt import (
FewShotPydanticPrompt,
InMemoryExampleStore,
)
# Create an example store with embeddings
example_store = InMemoryExampleStore(embeddings=my_embeddings)
# Add examples
example_store.add_example(input=my_input_model, output=my_output_model)
# Create the few-shot prompt
prompt = FewShotPydanticPrompt(
example_store=example_store,
top_k_for_examples=3,
threshold_for_examples=0.6,
)
# Generate with dynamic few-shot examples
results = await prompt.generate_multiple(llm=my_llm, data=my_input, n=1)
Converting from PydanticPrompt
from ragas.prompt.few_shot_pydantic_prompt import FewShotPydanticPrompt
# Convert an existing PydanticPrompt to use dynamic few-shot selection
few_shot_prompt = FewShotPydanticPrompt.from_pydantic_prompt(
pydantic_prompt=existing_prompt,
embeddings=my_embeddings,
)
# The existing examples are now in the InMemoryExampleStore
# and will be dynamically selected based on input similarity
results = await few_shot_prompt.generate_multiple(llm=my_llm, data=my_input)
Related Pages