Generation
DocumentAdapter
module-attribute
Converts one of your documents to a DocumentLike; see generate_with_docs.
TestsetGenerator
dataclass
TestsetGenerator(
llm: InstructorBaseRagasLLM,
embedding_model: BaseRagasEmbedding,
knowledge_graph: KnowledgeGraph = KnowledgeGraph(),
persona_list: Optional[List[Persona]] = None,
llm_context: Optional[str] = None,
)
Generates an evaluation dataset based on given scenarios and parameters.
Attributes:
| Name | Type | Description |
|---|---|---|
llm |
InstructorBaseRagasLLM
|
The language model to use for the generation process: the output of
|
embedding_model |
BaseRagasEmbedding
|
The embedding model used to build the knowledge graph, such as the
output of |
knowledge_graph |
KnowledgeGraph, default empty
|
The knowledge graph to use for the generation process. |
llm_context |
Optional[str], default None
|
Additional context to provide to the LLM when generating responses. This context will be used to guide how the LLM generates queries and answers. |
generate_with_docs
generate_with_docs(
documents: Sequence[Any],
testset_size: int,
transforms: Optional[Transforms] = None,
transforms_llm: Optional[InstructorBaseRagasLLM] = None,
transforms_embedding_model: Optional[
BaseRagasEmbedding
] = None,
query_distribution: Optional[QueryDistribution] = None,
run_config: Optional[RunConfig] = None,
callbacks: Optional[Callbacks] = None,
token_usage_parser: Optional[TokenUsageParser] = None,
with_debugging_logs=False,
raise_exceptions: bool = True,
return_executor: bool = False,
document_adapter: Optional[DocumentAdapter] = None,
num_personas: int = 3,
batch_size: Optional[int] = None,
) -> Union[Testset, Executor]
Generates an evaluation dataset from whole documents.
transforms split, enrich and link the documents into a knowledge
graph, then queries are generated from it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
Sequence
|
The source documents: objects with |
required |
testset_size
|
int
|
The number of test samples to generate |
required |
transforms
|
Optional[Transforms]
|
Custom transforms to apply to the documents, by default None |
None
|
transforms_llm
|
Optional[InstructorBaseRagasLLM]
|
LLM to use for transforms if different from instance LLM, by default None |
None
|
transforms_embedding_model
|
Optional[BaseRagasEmbedding]
|
Embedding model to use for transforms if different from instance model, by default None |
None
|
query_distribution
|
Optional[QueryDistribution]
|
Distribution of query types to generate, by default None |
None
|
run_config
|
Optional[RunConfig]
|
Configuration for the generation run, by default None |
None
|
callbacks
|
Optional[Callbacks]
|
Callbacks to use during generation, by default None |
None
|
token_usage_parser
|
Optional[TokenUsageParser]
|
Parse the LLMResult object and return a TokenUsage object. This is used to calculate the cost of the generation process. |
None
|
with_debugging_logs
|
bool
|
Whether to include debug logs, by default False |
False
|
raise_exceptions
|
bool
|
Whether to raise exceptions during generation, by default True |
True
|
return_executor
|
bool
|
If True, returns the Executor instance instead of running generation. The returned executor can be used to cancel execution by calling executor.cancel(). To get results, call executor.results(). Default is False. |
False
|
document_adapter
|
Optional[Callable[[Any], DocumentLike]]
|
Converts each document that is not a string to an object with
|
None
|
num_personas
|
int
|
The number of personas to generate or use, as in :meth: |
3
|
batch_size
|
Optional[int]
|
How large generation batches should be, as in :meth: |
None
|
Returns:
| Type | Description |
|---|---|
Testset or Executor
|
If return_executor is False, returns the generated evaluation dataset. If return_executor is True, returns the Executor instance for cancellable execution. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
TypeError
|
If an LLM is not an |
Source code in src/ragas/testset/synthesizers/generate.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
generate_with_chunks
generate_with_chunks(
chunks: Sequence[Any],
testset_size: int,
transforms: Optional[Transforms] = None,
transforms_llm: Optional[InstructorBaseRagasLLM] = None,
transforms_embedding_model: Optional[
BaseRagasEmbedding
] = None,
query_distribution: Optional[QueryDistribution] = None,
run_config: Optional[RunConfig] = None,
callbacks: Optional[Callbacks] = None,
token_usage_parser: Optional[TokenUsageParser] = None,
with_debugging_logs=False,
raise_exceptions: bool = True,
return_executor: bool = False,
document_adapter: Optional[DocumentAdapter] = None,
num_personas: int = 3,
batch_size: Optional[int] = None,
) -> Union[Testset, Executor]
Generates an evaluation dataset based on provided pre-chunked documents.
This method allows users to skip the internal chunking process by providing
documents that are already chunked. The input documents are treated as
NodeType.CHUNK directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunks
|
Sequence
|
The chunks: strings, objects with |
required |
testset_size
|
int
|
The number of test samples to generate |
required |
transforms
|
Optional[Transforms]
|
Custom transforms to apply to the chunks, by default None |
None
|
transforms_llm
|
Optional[InstructorBaseRagasLLM]
|
LLM to use for transforms if different from instance LLM, by default None |
None
|
transforms_embedding_model
|
Optional[BaseRagasEmbedding]
|
Embedding model to use for transforms if different from instance model, by default None |
None
|
query_distribution
|
Optional[QueryDistribution]
|
Distribution of query types to generate, by default None |
None
|
run_config
|
Optional[RunConfig]
|
Configuration for the generation run, by default None |
None
|
callbacks
|
Optional[Callbacks]
|
Callbacks to use during generation, by default None |
None
|
token_usage_parser
|
Optional[TokenUsageParser]
|
Parse the LLMResult object and return a TokenUsage object. |
None
|
with_debugging_logs
|
bool
|
Whether to include debug logs, by default False |
False
|
raise_exceptions
|
bool
|
Whether to raise exceptions during generation, by default True |
True
|
return_executor
|
bool
|
If True, returns the Executor instance instead of running generation. |
False
|
document_adapter
|
Optional[Callable[[Any], DocumentLike]]
|
Converts each chunk that is not a string, as in
:meth: |
None
|
num_personas
|
int
|
The number of personas to generate or use, as in :meth: |
3
|
batch_size
|
Optional[int]
|
How large generation batches should be, as in :meth: |
None
|
Returns:
| Type | Description |
|---|---|
Testset or Executor
|
If return_executor is False, returns the generated evaluation dataset. If return_executor is True, returns the Executor instance. |
Source code in src/ragas/testset/synthesizers/generate.py
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 | |
generate
generate(
testset_size: int,
query_distribution: Optional[QueryDistribution] = None,
num_personas: int = 3,
run_config: Optional[RunConfig] = None,
batch_size: Optional[int] = None,
callbacks: Optional[Callbacks] = None,
token_usage_parser: Optional[TokenUsageParser] = None,
with_debugging_logs=False,
raise_exceptions: bool = True,
return_executor: bool = False,
) -> Union[Testset, Executor]
Generate an evaluation dataset based on given scenarios and parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
testset_size
|
int
|
The number of samples to generate. |
required |
query_distribution
|
Optional[QueryDistribution]
|
A list of tuples containing scenario simulators and their probabilities. If None, default simulators will be used. |
None
|
num_personas
|
int
|
The number of personas to generate or use from the persona_list. |
3
|
run_config
|
Optional[RunConfig]
|
Configuration for running the generation process. |
None
|
batch_size
|
Optional[int]
|
How large should batches be. If set to None (default), no batching is done. |
None
|
callbacks
|
Optional[Callbacks]
|
Langchain style callbacks to use for the generation process. You can use this to log the generation process or add other metadata. |
None
|
token_usage_parser
|
Optional[TokenUsageParser]
|
Parse the LLMResult object and return a TokenUsage object. This is used to calculate the cost of the generation process. |
None
|
with_debugging_logs
|
bool
|
If True, enable debug logging for various components. |
False
|
raise_exceptions
|
bool
|
If True, raise exceptions during the generation process. |
True
|
return_executor
|
bool
|
If True, returns the Executor instance instead of running generation. The returned executor can be used to cancel execution by calling executor.cancel(). To get results, call executor.results(). |
False
|
Returns:
| Type | Description |
|---|---|
Testset or Executor
|
If return_executor is False, returns a dataset containing the generated TestsetSamples. If return_executor is True, returns the Executor instance for cancellable execution. |
Notes
This function performs the following steps: 1. Set up scenarios and debug logging if required. 2. Generate scenarios using an Executor. 3. Calculate split values for different scenario types. 4. Generate samples for each scenario. 5. Compile the results into a Testset.