|
| 1 | +<div align="center"> |
| 2 | + <h1><code>prompt-string</code></h1> |
| 3 | + <p><strong>Treat prompt as a data type in Python</strong></p> |
| 4 | + <p> |
| 5 | + <img src="https://img.shields.io/badge/python->=3.11-blue"> |
| 6 | + <a href="https://pypi.org/project/prompt-string/"> |
| 7 | + <img src="https://img.shields.io/pypi/v/prompt-string.svg"> |
| 8 | + </a> |
| 9 | +</div> |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +Prompt is essentially a string, but it should behave somewhat differently from a standard string: |
| 14 | + |
| 15 | +📏 **Length & Slicing**: A prompt string should consider the length in terms of tokens, not characters, and slicing should be done accordingly. |
| 16 | + |
| 17 | +👨 **Role & Concatenation**: Prompt strings should have designated roles (e.g., `system`, `user`, `assistant`) and should be concatenated in a specific manner. |
| 18 | + |
| 19 | +🦆 **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). |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +**Few promises in `prompt-string`:** |
| 24 | + |
| 25 | +- `prompt-string` inherits from `string`. Therefore, aside from the mentioned features, its other behaviors are just like those of a `string` in Python. |
| 26 | +- `prompt-string` won't add OpenAI and other AI SDKs as dependencies; it is simply a toolkit for prompts. |
| 27 | +- `prompt-string` will be super light and fast, with no heavy processes running behind the scenes. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +## Install |
| 32 | + |
| 33 | +```bash |
| 34 | +pip install prompt-string |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +## Quick Start |
| 40 | + |
| 41 | +#### Length & Slicing |
| 42 | + |
| 43 | +```python |
| 44 | +from prompt_string import P |
| 45 | + |
| 46 | +prompt = P("you're a helpful assistant.") |
| 47 | + |
| 48 | +print("Total token size", len(prompt)) |
| 49 | +print("Decoded result of the second token", prompt[2]) |
| 50 | +print("The decoded result of first five tokens", prompt[:5]) |
| 51 | +``` |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +#### Role & Concatenation |
| 56 | + |
| 57 | +```python |
| 58 | +from prompt_string import P |
| 59 | + |
| 60 | +sp = P("you're a helpful assistant.", "system") |
| 61 | +up = P("How are you?", "user") |
| 62 | + |
| 63 | +print(sp.role, up.role, (sp+up).role) |
| 64 | +print(sp + up) |
| 65 | +``` |
| 66 | + |
| 67 | +- role can be `None`, `str`, `list[str]` |
| 68 | +- For single prompt, like `sp`, the role is `str`(*e.g.* `system`) or `None` |
| 69 | +- For concatenated prompts, like `sp+up`, the role is `list[str]`(*e.g.* `['system', 'user']`) |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | +#### Binding Functions |
| 74 | + |
| 75 | +```python |
| 76 | +from prompt_string import P |
| 77 | + |
| 78 | +sp = P("you're a helpful assistant.") |
| 79 | +up = P("How are you?") |
| 80 | + |
| 81 | +print((sp+up).messages()) |
| 82 | +``` |
| 83 | + |
| 84 | +- `messages` will return the OpenAI-Compatible messages format, where you can directly pass it to `client.chat.completions.create(messages=...)` |
0 commit comments