useful utilities for prompt engineering
Project description
promptools
useful utilities for prompt engineering
Installation
pip install promptools # or any other dependency manager you like
Note that the validation features use pydantic>=2
as an optional dependencies. You can use pip install promptools[validation]
to install it by the way.
API References
extractors
extract_json
parse JSON from raw LLM response text
Usages
Detect and parse the last JSON block from input string.
def extract_json(text: str, /, fallback: F) -> JSON | F:
It will return fallback
if it fails to detect / parse JSON.
Note that the default value of fallback
is None
.
def extract_json(text: str, /, fallback: F, expect: Type[M]) -> M | F:
You can provide a pydantic.BaseModel
or a TypeAlias
in the expect
parameter and pydantic
will validate it.
Examples
Classification example
Imagine that you are using LLM on a classification task.
from promptools.extractors import extract_json
from typing import TypedDict
class Item(TypedDict):
index: int
label: str
original_text = """
The result is:
```json
[
{"index": 0, "label": "A"},
{"index": 1, "label": "B"}
]
```
"""
print(extract_json(original_text, [], list[Item]))
The output will be:
[Item(index=0, label='A'), Item(index=1, label='B')]
The output will be:
[{'index': 0, 'label': 'A'}, {'index': 1, 'label': 'B'}]
Streaming JSON example
Imagine that you are trying to parse a malformed JSON:
from promptools.extractors import extract_json
from pydantic import BaseModel
original_text = '{"results": [{"index": 1}, {'
print(extract_json(original_text))
The output will be:
{'results': [{'index': 1}, {}]}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for promptools-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a899dd32a576b940c69f2918970f1b74dd377429d5485eb199850a7105f1d7b4 |
|
MD5 | 0cb64748c14a9ce55c7fd82ae45ac2d6 |
|
BLAKE2b-256 | bc50ffc08b6e164dd1dff94b6c47fef8ea81e2046f345fac05b642caa1735fd4 |