Prompt
BasePrompt
BasePrompt(name: Optional[str] = None, language: str = 'english', original_hash: Optional[str] = None)
Bases: ABC
Source code in src/ragas/prompt/base.py
generate
abstractmethod
async
generate(llm: BaseRagasLLM, data: Any, temperature: Optional[float] = None, stop: Optional[List[str]] = None, callbacks: Callbacks = []) -> Any
Generate a single completion from the prompt.
Source code in src/ragas/prompt/base.py
generate_multiple
abstractmethod
generate_multiple(llm: BaseRagasLLM, data: Any, n: int = 1, temperature: Optional[float] = None, stop: Optional[List[str]] = None, callbacks: Callbacks = []) -> Any
Generate multiple completions from the prompt.
Source code in src/ragas/prompt/base.py
StringPrompt
StringPrompt(name: Optional[str] = None, language: str = 'english', original_hash: Optional[str] = None)
Bases: BasePrompt
A simple prompt that can be formatted with additional data using f-string syntax.
This prompt is a simpler alternative to PydanticPrompt for those who prefer a more flexible approach without the need for a Pydantic model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
instruction
|
str
|
The instruction string that can be formatted with additional data. |
required |
Examples:
>>> from ragas.prompt import string_prompt
>>> await prompt.generate(llm=llm, data={"category": "commerce"})
Source code in src/ragas/prompt/base.py
generate
async
generate(llm: BaseRagasLLM, data: str, temperature: Optional[float] = None, stop: Optional[List[str]] = None, callbacks: Callbacks = []) -> str
Generate text based on the instruction and provided data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
BaseRagasLLM
|
The language model to use for text generation. |
required |
data
|
Optional[Dict[str, Any]]
|
The data to format the instruction with, by default None. |
required |
n
|
int
|
The number of completions to generate, by default 1. |
required |
temperature
|
Optional[float]
|
The temperature for text generation, by default None. |
None
|
stop
|
Optional[List[str]]
|
The stop sequences for text generation, by default None. |
None
|
callbacks
|
Callbacks
|
The callbacks to use during text generation, by default []. |
[]
|
Returns:
Type | Description |
---|---|
str
|
The generated text. |
Source code in src/ragas/prompt/base.py
PydanticPrompt
PydanticPrompt(name: Optional[str] = None, language: str = 'english', original_hash: Optional[str] = None)
Bases: BasePrompt
, Generic[InputModel, OutputModel]
Source code in src/ragas/prompt/base.py
generate
async
generate(llm: BaseRagasLLM, data: InputModel, temperature: Optional[float] = None, stop: Optional[List[str]] = None, callbacks: Optional[Callbacks] = None) -> OutputModel
Generate a single output using the provided language model and input data.
This method is a special case of generate_multiple
where only one output is generated.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
BaseRagasLLM
|
The language model to use for generation. |
required |
data
|
InputModel
|
The input data for generation. |
required |
temperature
|
float
|
The temperature parameter for controlling randomness in generation. |
None
|
stop
|
List[str]
|
A list of stop sequences to end generation. |
None
|
callbacks
|
Callbacks
|
Callback functions to be called during the generation process. |
None
|
Returns:
Type | Description |
---|---|
OutputModel
|
The generated output. |
Notes
This method internally calls generate_multiple
with n=1
and returns the first (and only) result.
Source code in src/ragas/prompt/pydantic_prompt.py
generate_multiple
async
generate_multiple(llm: BaseRagasLLM, data: InputModel, n: int = 1, temperature: Optional[float] = None, stop: Optional[List[str]] = None, callbacks: Optional[Callbacks] = None) -> List[OutputModel]
Generate multiple outputs using the provided language model and input data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
llm
|
BaseRagasLLM
|
The language model to use for generation. |
required |
data
|
InputModel
|
The input data for generation. |
required |
n
|
int
|
The number of outputs to generate. Default is 1. |
1
|
temperature
|
float
|
The temperature parameter for controlling randomness in generation. |
None
|
stop
|
List[str]
|
A list of stop sequences to end generation. |
None
|
callbacks
|
Callbacks
|
Callback functions to be called during the generation process. |
None
|
Returns:
Type | Description |
---|---|
List[OutputModel]
|
A list of generated outputs. |
Raises:
Type | Description |
---|---|
RagasOutputParserException
|
If there's an error parsing the output. |
Source code in src/ragas/prompt/pydantic_prompt.py
adapt
async
adapt(target_language: str, llm: BaseRagasLLM, adapt_instruction: bool = False) -> 'PydanticPrompt[InputModel, OutputModel]'
Adapt the prompt to a new language.
Source code in src/ragas/prompt/pydantic_prompt.py
save
Save the prompt to a file.
Source code in src/ragas/prompt/pydantic_prompt.py
BoolIO
Bases: BaseModel
StringIO
Bases: BaseModel
PromptMixin
Mixin class for classes that have prompts. eg: BaseSynthesizer, MetricWithLLM
get_prompts
get_prompts() -> Dict[str, PydanticPrompt]
Returns a dictionary of prompts for the class.
Source code in src/ragas/prompt/mixin.py
set_prompts
Sets the prompts for the class.
Raises:
Type | Description |
---|---|
ValueError
|
If the prompt is not an instance of |
Source code in src/ragas/prompt/mixin.py
adapt_prompts
async
adapt_prompts(language: str, llm: BaseRagasLLM, adapt_instruction: bool = False) -> Dict[str, PydanticPrompt]
Adapts the prompts in the class to the given language and using the given LLM.
Notes
Make sure you use the best available LLM for adapting the prompts and then save and load the prompts using save_prompts and load_prompts methods.
Source code in src/ragas/prompt/mixin.py
save_prompts
Saves the prompts to a directory in the format of {name}_{language}.json
Source code in src/ragas/prompt/mixin.py
load_prompts
Loads the prompts from a path. File should be in the format of {name}_{language}.json