Prompt utils
prompt_utils
load_payloads
load_payloads(file_path)
Load JSON payload(s) from a file or directory.
This function reads JSON data from either a single file or multiple files in a directory. It supports both .json and .jsonl file formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Union[UPath, str]
|
Path to a JSON file or a directory containing JSON files. Can be a string or a Path object. |
required |
Yields:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
Each JSON object loaded from the file(s). |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified file or directory does not exist. |
JSONDecodeError
|
If there's an error parsing the JSON data. |
ValidationError
|
If the JSON data does not conform to the expected schema. |
IOError
|
If there's an error reading the file. |
Source code in llmeter/prompt_utils.py
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 | |
load_prompts
load_prompts(file_path, create_payload_fn, create_payload_kwargs={}, file_pattern=None)
Load prompts from a file or directory and create payloads.
This function reads prompts from either a single file or multiple files in a directory, and generates payloads using the provided create_payload_fn.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Union[UPath, str]
|
Path to a file or directory containing prompts. |
required |
create_payload_fn
|
Callable
|
Function to create a payload from each prompt. |
required |
create_payload_kwargs
|
Dict
|
Additional keyword arguments for create_payload_fn. Defaults to an empty dictionary. |
{}
|
file_pattern
|
Union[str, None]
|
Glob pattern for matching files in a directory. If None, matches all files. Defaults to None. |
None
|
Yields:
| Name | Type | Description |
|---|---|---|
Dict |
dict
|
Payload created from each prompt. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the specified file or directory does not exist. |
PermissionError
|
If there's insufficient permission to read the file(s). |
ValueError
|
If create_payload_fn raises a ValueError. |
Source code in llmeter/prompt_utils.py
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 | |
save_payloads
save_payloads(payloads, output_path, output_file='payload.jsonl')
Save payloads to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
payloads
|
Iterator[Dict]
|
An iterator of payloads (dicts). |
required |
output_path
|
Union[UPath, str]
|
The directory path where the output file should be saved. |
required |
output_file
|
str
|
The name of the output file. Defaults to "payloads.jsonl". |
'payload.jsonl'
|
Returns:
| Name | Type | Description |
|---|---|---|
output_file_path |
UPath
|
The path to the output file. |
Raises:
| Type | Description |
|---|---|
IOError
|
If there's an error writing to the file. |
Source code in llmeter/prompt_utils.py
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |