Skip to main content

Treat prompt as a data type in Python

Project description

prompt-string

Treat prompt as a data type in Python

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.

Features

prompt-string provides two types:

  • P for prompt, inherits from string. Length, Slicing and concatenation are modified and support new attributes like .role.
    • p = P("You're a helpful assistant")
  • PC for prompt chain, act like list[P]. Link a series of prompt and support .messages(...)
    • pc = p1 / p2 / p3

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 three tokens:", prompt[:3])

P supports some str native methods to still return a P object:

  • .format
  • .replace
prompt = P("you're a helpful assistant. {temp}")

print(len(prompt.format(temp="End of instructions")))
print(len(prompt.replace("{temp}", "")))

🧐 Raise an issue if you think other methods should be supported

Role

from prompt_string import P

sp = P("you're a helpful assistant.", role="system")
up = P("How are you?", role="user")

print(sp.role, up.role, (sp+up).role)
print(sp + up)

print(sp.message())
  • role can be None, str for P

  • For single prompt, like sp, the role is str(e.g. system) or None

  • sp+up will concatenate two prompt string and generate a new P, whose role will be updated if the latter one has one.

    • For example, sp+up's role is user; sp+P('Hi')'s role is system
  • .message(...) return a JSON object of this prompt.

Concatenation

pc = sp / up
print(pc.roles)
print(pc.messages())

For concatenated prompts, like sp / up, the type will be converted to PC (prompt chain), PC has below things:

  • .roles, a list of roles. For example, (sp|up).roles is ['system', 'user']
  • .messages(...) pack prompts into OpenAI-Compatible messages JSON, where you can directly pass it to client.chat.completions.create(messages=...).
    • messages will assume the first role is user, then proceed in the order of user-assistant. When a prompt has a role, it will use that role. check pc.infer_role for final roles in messages.

Few promises in prompt-string

  • P inherits from string. Therefore, aside from the mentioned features, its other behaviors are just like those of a string in Python.
  • prompt-string won't add OpenAI and other AI SDKs as dependencies; it is simply a toolkit for prompts.
  • prompt-string will be super light and fast, with no heavy processes running behind the scenes.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

prompt_string-0.0.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

prompt_string-0.0.1-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file prompt_string-0.0.1.tar.gz.

File metadata

  • Download URL: prompt_string-0.0.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for prompt_string-0.0.1.tar.gz
Algorithm Hash digest
SHA256 c1c8c89006ff72bea5280d1bd2b62027a572d56a71df29a90a9dfa8590845e9c
MD5 da3489e3b20a3ca63abcfe2c5c5f70ba
BLAKE2b-256 b0a284ab2e11b587ff78dbf5ea8a71b4f60d38d69877a1339455e491ecc6a98b

See more details on using hashes here.

File details

Details for the file prompt_string-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: prompt_string-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for prompt_string-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6485f802ba7d3817a640f21e27f0f1766566fa304d4875ad700e97bc9d57ded9
MD5 f0004c1cb9e95117ca6244c9990d9ba7
BLAKE2b-256 d7ce4d67cde02928fafdc8a26fa9875c8b7fb64ebd3a7cae677f4e5d05dda136

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page