Skip to main content

Short description of your package

Project description

Promptist

Overview

promptist is a Python package designed for managing and rendering structured prompt templates for chat-based interactions. It provides tools to structure, render, and format prompts and conversations with the use of placeholders and specific roles like user, assistant, and system. The package is highly extensible and supports dynamic placeholders in prompt files, making it easy to manage reusable templates for different chatbot scenarios.

Installation

From PyPI:

pip install promptist

From source:

git clone https://github.com/otanadzetsotne/promptist.git
cd promptist
pip install .

Key Features

Renderer Class

The core functionality of promptist is provided by the Renderer class. It reads prompt files, replaces placeholders dynamically, and generates prompt conversations based on role-based chat structures. Below is a breakdown of its main functionalities:

  1. Initialization:

    The Renderer class requires two parameters during initialization:

    • name: The name of the prompt file to render.
    • prompts_dir: The directory where the prompt files are located.

    Example:

    renderer = Renderer(name='chat_scenario', prompts_dir='/path/to/prompts')
    
  2. Rendering Prompts:

    The render() method reads the prompt template and replaces the placeholders defined in the prompt file with the data provided. Placeholders in the prompt files follow the {type:name} format. The supported types include:

    • data: Replaces the placeholder with the value provided in the data dictionary.
    • include: Includes another prompt template specified by name.

    Example:

    rendered_prompt = renderer.render(data={"user_name": "Alice"})
    print(rendered_prompt.text)
    
  3. Chat Mode:

    The chat() method structures the rendered prompt as a conversation between different roles (system, user, assistant).

    The prompt is parsed line by line to determine which role is speaking.

    Example:

    chat_prompt = renderer.chat(data={"user_name": "Alice"})
    for message in chat_prompt.chat:
        print(message)
    

Models

The package also defines several key models to structure and handle data in promptist.models.

  • Prompt: A simple model representing a text-based prompt.
  • ChatRole: An enumeration representing different roles in a chat, such as system, user, and assistant.
  • Msg: A message model that contains the role and the content of the message.
  • PromptChat: An extension of the Prompt model that includes a chat log, represented as a list of Msg objects.

These models make it easy to validate, parse, and display chat-based prompts in a structured way.

Example of Prompt File Syntax

Prompt files are stored as .txt files within the specified directory. The placeholder format is {type:name}. Below is an example of how a prompt file might look:

chat_scenario.txt

system: Welcome to the chat service.
user: Hi, my name is {data:user_name}.
assistant: How can I assist you today, {data:user_name}?

Including Other Prompts

You can include other prompts by using the include type in placeholders. For example:

system: {include:welcome.system_welcome}
user: {data:user_message}

In this case, the welcome/system_welcome.txt file will be included where {include:welcome.system_welcome} is placed.

Placeholder Syntax

  • Data Placeholder: {data:name} replaces the placeholder with the value of name in the provided data dictionary.
  • Include Placeholder: {include:filename} includes the contents of another prompt file located in the prompts directory.

Example Usage

from promptist.renderer import Renderer

# Initialize Renderer
renderer = Renderer(name='chat_scenario', prompts_dir='./prompts')

# Render prompt with data
data = {
    'user_name': 'Alice',
}
rendered_prompt = renderer.render(data)
print(rendered_prompt.text)

# Render chat-based prompt
chat_prompt = renderer.chat(data)
for message in chat_prompt.chat:
    print(message.role, ":", message.content)

License

This project is licensed under the MIT License. For more information, see the LICENSE file.

Author

Tsotne Otanadze

For questions, issues, or contributions, feel free to contact me at: otanadzetsotne@yahoo.com.

If you use this repository in your projects, please note it in the project description or leave a link to this repository. I will be glad to any feedback and suggestions!

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

promptist-0.1.3.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

promptist-0.1.3-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file promptist-0.1.3.tar.gz.

File metadata

  • Download URL: promptist-0.1.3.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for promptist-0.1.3.tar.gz
Algorithm Hash digest
SHA256 f2d6eb863a8513726ff70ddb5c60a153c6a321e7cbfb4b739169c348a462228f
MD5 0ad86bdd21f01aca859e59816f25b09a
BLAKE2b-256 04a914b49504eab60dd51302eabff703d3479c0f1a5992fd489ccaf191089539

See more details on using hashes here.

File details

Details for the file promptist-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: promptist-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.19

File hashes

Hashes for promptist-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5ab9e03d7f858e64fa23ed59bac635d84566c5e51f41b6e928713372372d12d4
MD5 a9577fac286259d91858adb3c5930113
BLAKE2b-256 974dbd48f1d05af1e92203bb55ff64cb493def932586b06b78c1cfa7f6c2c065

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