Treat prompt as a data type in Python
Project description
Prompt is essentially a string, but it should behave somewhat differently from a standard string:
📏 Length & Slicing: A prompt string should consider the length in terms of tokens, not characters, and slicing should be done accordingly.
👨 Role & Concatenation: Prompt strings should have designated roles (e.g., system, user, assistant) and should be concatenated in a specific manner.
🦆 Binding Functions: A prompt string contains logic and instructions, so having some binding functions for AI-related stuff is beneficial and necessary (e.g., convert to OpenAI Message Format).
Few promises in prompt-string:
prompt-stringinherits fromstring. Therefore, aside from the mentioned features, its other behaviors are just like those of astringin Python.prompt-stringwon't add OpenAI and other AI SDKs as dependencies; it is simply a toolkit for prompts.prompt-stringwill be super light and fast, with no heavy processes running behind the scenes.
Install
pip install prompt-string
Quick Start
Length & Slicing
from prompt_string import P
prompt = P("you're a helpful assistant.")
print("Total token size", len(prompt))
print("Decoded result of the second token", prompt[2])
print("The decoded result of first five tokens", prompt[:5])
Role & Concatenation
from prompt_string import P
sp = P("you're a helpful assistant.", "system")
up = P("How are you?", "user")
print(sp.role, up.role, (sp+up).role)
print(sp + up)
- role can be
None,str,list[str] - For single prompt, like
sp, the role isstr(e.g.system) orNone - For concatenated prompts, like
sp+up, the role islist[str](e.g.['system', 'user'])
Binding Functions
from prompt_string import P
sp = P("you're a helpful assistant.")
up = P("How are you?")
print((sp+up).messages())
messageswill return the OpenAI-Compatible messages format, where you can directly pass it toclient.chat.completions.create(messages=...)
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file prompt_string-0.0.1.dev1.tar.gz.
File metadata
- Download URL: prompt_string-0.0.1.dev1.tar.gz
- Upload date:
- Size: 3.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
99439773814568682bd28bd05f00689e7403e152fa31ec41e18f2c7270008598
|
|
| MD5 |
cdca348f48eb387c2e881f2dde8b382b
|
|
| BLAKE2b-256 |
1ccc01ca4b6644eb22e9a5d2dd228828c97e474c0186b7812886d07c6b3fcc1e
|
File details
Details for the file prompt_string-0.0.1.dev1-py3-none-any.whl.
File metadata
- Download URL: prompt_string-0.0.1.dev1-py3-none-any.whl
- Upload date:
- Size: 3.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5219ea724931ba8b134535c04186ac522c3d93c1c23d9b58742963d3366ca203
|
|
| MD5 |
942e93cb420a18f5c78e2b19ce6b832c
|
|
| BLAKE2b-256 |
2dd9d51eb7451006fa331a3d7e9bb63ae94e92fbb0c02bec03d07f563c9ee0e9
|