Name | promptools JSON |
Version |
0.1.3.6
JSON |
| download |
home_page | None |
Summary | useful utilities for prompt engineering |
upload_time | 2024-10-14 04:11:43 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.8 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# promptools
> useful utilities for prompt engineering
## Installation
```sh
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.
```py
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`.
```py
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.
````py
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:
```py
[{'index': 0, 'label': 'A'}, {'index': 1, 'label': 'B'}]
```
###### Streaming JSON example
Imagine that you are trying to parse a malformed JSON:
```py
from promptools.extractors import extract_json
from pydantic import BaseModel
original_text = '{"results": [{"index": 1}, {'
print(extract_json(original_text))
```
The output will be:
```py
{'results': [{'index': 1}, {}]}
```
### openai
#### `count_token`
count number of tokens in prompt
##### Usages
```py
def count_token(prompt: str | list[str], enc: Encoding | None = None) -> int:
```
Provide your prompt / a list of prompts, get its token count. The second parameter is the `tiktoken.Encoding` instance, will default to `get_encoding("cl100k_base")` if not provided. The default `tiktoken.Encoding` instance is cached, and will not be re-created every time.
```py
def count_token(prompt: dict | list[dict], enc: Encoding | None = None) -> int:
```
Note that it can also be a single message / a list of messages. Every message should be a dict in the schema below:
```py
class Message(TypedDict):
role: str
content: str
name: NotRequired[str]
```
##### Examples
###### Plain text
```py
from tiktoken import encoding_for_model
from promptools.openai import count_token
print(count_token("hi", encoding_for_model("gpt-3.5-turbo")))
```
The output will be:
```py
1
```
##### List of plain texts
```py
from promptools.openai import count_token
print(count_token(["hi", "hello"]))
```
The output will be:
```py
2
```
###### Single message
```py
from promptools.openai import count_token
count_token({"role": "user", "content": "hi"})
```
The output will be:
```py
5
```
###### List of messages
```py
from promptools.openai import count_token
count_token([
{"role": "user", "content": "hi"},
{"role": "assistant", "content": "Hello! How can I assist you today?"},
])
```
The output will be:
```py
21
```
###### Integrating with `promplate`
```py
from promplate.prompt.chat import U, A, S
from promptools.openai import count_token
count_token([
S @ "background" > "You are a helpful assistant.",
U @ "example_user" > "hi",
A @ "example_assistant" > "Hello! How can I assist you today?",
])
```
The output will be:
```py
40
```
Raw data
{
"_id": null,
"home_page": null,
"name": "promptools",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": null,
"author": null,
"author_email": "Muspi Merol <me@promplate.dev>",
"download_url": "https://files.pythonhosted.org/packages/5d/20/0c046b4c124d9c10a664463a23ff14b2d923b9adc4ca8916ef5604bac6e9/promptools-0.1.3.6.tar.gz",
"platform": null,
"description": "# promptools\n\n> useful utilities for prompt engineering\n\n## Installation\n\n```sh\npip install promptools # or any other dependency manager you like\n```\n\nNote that the **validation** features use `pydantic>=2` as an optional dependencies. You can use `pip install promptools[validation]` to install it by the way.\n\n## API References\n\n### extractors\n\n#### `extract_json`\n\nparse JSON from raw LLM response text\n\n##### Usages\n\nDetect and parse the last JSON block from input string.\n\n```py\ndef extract_json(text: str, /, fallback: F) -> JSON | F:\n```\n\nIt will return `fallback` if it fails to detect / parse JSON.\nNote that the default value of `fallback` is `None`.\n\n```py\ndef extract_json(text: str, /, fallback: F, expect: Type[M]) -> M | F:\n```\n\nYou can provide a `pydantic.BaseModel` or a `TypeAlias` in the `expect` parameter and `pydantic` will validate it.\n\n##### Examples\n\n###### Classification example\n\nImagine that you are using LLM on a classification task.\n\n````py\nfrom promptools.extractors import extract_json\nfrom typing import TypedDict\n\nclass Item(TypedDict):\n index: int\n label: str\n\noriginal_text = \"\"\"\nThe result is:\n\n```json\n[\n {\"index\": 0, \"label\": \"A\"},\n {\"index\": 1, \"label\": \"B\"}\n]\n```\n\"\"\"\n\nprint(extract_json(original_text, [], list[Item]))\n````\n\nThe output will be:\n\n```py\n[{'index': 0, 'label': 'A'}, {'index': 1, 'label': 'B'}]\n```\n\n###### Streaming JSON example\n\nImagine that you are trying to parse a malformed JSON:\n\n```py\nfrom promptools.extractors import extract_json\nfrom pydantic import BaseModel\n\noriginal_text = '{\"results\": [{\"index\": 1}, {'\n\nprint(extract_json(original_text))\n```\n\nThe output will be:\n\n```py\n{'results': [{'index': 1}, {}]}\n```\n\n### openai\n\n#### `count_token`\n\ncount number of tokens in prompt\n\n##### Usages\n\n```py\ndef count_token(prompt: str | list[str], enc: Encoding | None = None) -> int:\n```\n\nProvide your prompt / a list of prompts, get its token count. The second parameter is the `tiktoken.Encoding` instance, will default to `get_encoding(\"cl100k_base\")` if not provided. The default `tiktoken.Encoding` instance is cached, and will not be re-created every time.\n\n```py\ndef count_token(prompt: dict | list[dict], enc: Encoding | None = None) -> int:\n```\n\nNote that it can also be a single message / a list of messages. Every message should be a dict in the schema below:\n\n```py\nclass Message(TypedDict):\n role: str\n content: str\n name: NotRequired[str]\n```\n\n##### Examples\n\n###### Plain text\n\n```py\nfrom tiktoken import encoding_for_model\nfrom promptools.openai import count_token\n\nprint(count_token(\"hi\", encoding_for_model(\"gpt-3.5-turbo\")))\n```\n\nThe output will be:\n\n```py\n1\n```\n\n##### List of plain texts\n\n```py\nfrom promptools.openai import count_token\n\nprint(count_token([\"hi\", \"hello\"]))\n```\n\nThe output will be:\n\n```py\n2\n```\n\n###### Single message\n\n```py\nfrom promptools.openai import count_token\n\ncount_token({\"role\": \"user\", \"content\": \"hi\"})\n```\n\nThe output will be:\n\n```py\n5\n```\n\n###### List of messages\n\n```py\nfrom promptools.openai import count_token\n\ncount_token([\n {\"role\": \"user\", \"content\": \"hi\"},\n {\"role\": \"assistant\", \"content\": \"Hello! How can I assist you today?\"},\n])\n```\n\nThe output will be:\n\n```py\n21\n```\n\n###### Integrating with `promplate`\n\n```py\nfrom promplate.prompt.chat import U, A, S\nfrom promptools.openai import count_token\n\ncount_token([\n S @ \"background\" > \"You are a helpful assistant.\",\n U @ \"example_user\" > \"hi\",\n A @ \"example_assistant\" > \"Hello! How can I assist you today?\",\n])\n```\n\nThe output will be:\n\n```py\n40\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "useful utilities for prompt engineering",
"version": "0.1.3.6",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "e9e7fcf6aceb58223e961b368a087306aed9efa884e3a492120bd36587dea46b",
"md5": "adb25c38d49bb5d3c3644f1d41e4b83d",
"sha256": "e15ae61afc96ab035cb393730306b349ccd2bb297ded00ae69d879de67a25773"
},
"downloads": -1,
"filename": "promptools-0.1.3.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "adb25c38d49bb5d3c3644f1d41e4b83d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 7202,
"upload_time": "2024-10-14T04:11:42",
"upload_time_iso_8601": "2024-10-14T04:11:42.042327Z",
"url": "https://files.pythonhosted.org/packages/e9/e7/fcf6aceb58223e961b368a087306aed9efa884e3a492120bd36587dea46b/promptools-0.1.3.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "5d200c046b4c124d9c10a664463a23ff14b2d923b9adc4ca8916ef5604bac6e9",
"md5": "c71dbfec447988cb00a9dd1e7e5c2e15",
"sha256": "635261e10528b78bd963eef915f3b0c595086d73a608ab1e552f97fb0ca10f31"
},
"downloads": -1,
"filename": "promptools-0.1.3.6.tar.gz",
"has_sig": false,
"md5_digest": "c71dbfec447988cb00a9dd1e7e5c2e15",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 5505,
"upload_time": "2024-10-14T04:11:43",
"upload_time_iso_8601": "2024-10-14T04:11:43.435547Z",
"url": "https://files.pythonhosted.org/packages/5d/20/0c046b4c124d9c10a664463a23ff14b2d923b9adc4ca8916ef5604bac6e9/promptools-0.1.3.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-14 04:11:43",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "promptools"
}