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.

🦆 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-string 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.

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 is str(e.g. system) or None
  • For concatenated prompts, like sp+up, the role is list[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())
  • messages will return the OpenAI-Compatible messages format, where you can directly pass it to client.chat.completions.create(messages=...)

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.dev1.tar.gz (3.5 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.dev1-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

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

Hashes for prompt_string-0.0.1.dev1.tar.gz
Algorithm Hash digest
SHA256 99439773814568682bd28bd05f00689e7403e152fa31ec41e18f2c7270008598
MD5 cdca348f48eb387c2e881f2dde8b382b
BLAKE2b-256 1ccc01ca4b6644eb22e9a5d2dd228828c97e474c0186b7812886d07c6b3fcc1e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for prompt_string-0.0.1.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 5219ea724931ba8b134535c04186ac522c3d93c1c23d9b58742963d3366ca203
MD5 942e93cb420a18f5c78e2b19ce6b832c
BLAKE2b-256 2dd9d51eb7451006fa331a3d7e9bb63ae94e92fbb0c02bec03d07f563c9ee0e9

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