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
generate_multiple
async
generate_multiple(llm: BaseRagasLLM, data: str, n: int = 1, temperature: Optional[float] = None, stop: Optional[List[str]] = None, callbacks: Callbacks = []) -> List[str]
Generate multiple distinct text outputs based on the instruction and provided data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
BaseRagasLLM
|
The language model to use for text generation. |
required |
data
|
str
|
The data to format the instruction with. |
required |
n
|
int
|
The number of completions to generate, by default 1. |
1
|
temperature
|
Optional[float]
|
The temperature for text generation, by default None. |
None
|
stop
|
Optional[List[str]]
|
Stop sequences for text generation, by default None. |
None
|
callbacks
|
Callbacks
|
Callbacks to use during text generation, by default []. |
[]
|
Returns:
| Type | Description |
|---|---|
List[str]
|
A list containing |
Notes
- When caching is enabled, each output is uniquely cached to prevent duplicates.
- This ensures that multiple outputs for the same input are distinct.
- Previous issues where caching returned duplicate outputs have been fixed.
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: Union[BaseRagasLLM, InstructorBaseRagasLLM, BaseLanguageModel], data: InputModel, temperature: Optional[float] = None, stop: Optional[List[str]] = None, callbacks: Optional[Callbacks] = None, retries_left: int = 3) -> 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
|
retries_left
|
int
|
Number of retry attempts for an invalid LLM response |
3
|
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: Union[BaseRagasLLM, InstructorBaseRagasLLM, BaseLanguageModel], data: InputModel, n: int = 1, temperature: Optional[float] = None, stop: Optional[List[str]] = None, callbacks: Optional[Callbacks] = None, retries_left: int = 3) -> 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
|
retries_left
|
int
|
Number of retry attempts for an invalid LLM response |
3
|
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
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
adapt
async
adapt(target_language: str, llm: Union[BaseRagasLLM, InstructorBaseRagasLLM], 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.
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: Union[BaseRagasLLM, InstructorBaseRagasLLM], 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