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.2.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.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: promptist-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 74a96219ea3abf6cf8f29c96591d84d11a65af927aec886290bcc54a0eed47e8
MD5 c7edff568c80a1e9b28582592a587911
BLAKE2b-256 b98dbbb983fd2915d9ee17e8cb1c9fe375c8dba932ed3c9f905964d9d3f83b8b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: promptist-0.1.2-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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b0feea1720a8847486ad1d88a98fee77403723701588ab462d9db474ba2c7851
MD5 eafa393996b0bbe8e4ac24eccd4a8827
BLAKE2b-256 4fad9de183b2faca820d066bbfaa3fb7d7be8d21ca97c69d37cf9fe00531e586

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