Skip to main content

A library for creating smarter prompts for LLMs by adding priority to components.

Project description

PriomptiPy

PriomptiPy (priority + prompt + python) is a Python-based prompting library that brings prioritized prompting from the Anysphere team's JavaScript library Priompt. Adapted by the Quarkle dev team, PriomptiPy integrates priority-based context management into Python applications, especially useful for AI-enabled agent and chatbot development.

Motivation

It aims to simplify the process of managing and rendering prompts in AI-driven interactions, making it easier to focus on the most relevant context when hydrating the prompts from multiple sources.

Installation

To install PriomptiPy, simply use pip:

pip install priomptipy

Examples

Consider a scenario where you want to manage a conversation with a user:

Priomptipy Example

from priomptipy import SystemMessage, UserMessage, AssistantMessage, Scope, Empty, render

messages = [
    SystemMessage("You are Quarkle, an AI Developmental Editor"),
    Scope([
        UserMessage("Hello Quarkle, how are you?"),
        AssistantMessage("Hello, I am doing well. How can I help you?")
    ], absolute_priority=5),
    Scope([
        UserMessage("Write me a haiku on the number 17"),
        AssistantMessage("Seventeen whispers, In life's mosaic unseen, Quiet steps of time.")
    ], absolute_priority=10),
    UserMessage("Okay nice, now give me a title for it"),
    Empty(token_count=10)
]

render_options = {"token_limit": 80, "tokenizer": "cl100k_base"}
result = await render(messages, render_options)
print(result['prompt'])

In this dummy example, SystemMessage, UserMessage, and AssistantMessage are used to structure the conversation. Scope allows prioritizing certain parts of the conversation, ensuring the most relevant messages are included within the token limit. We always include the SystemMessage and the final UserMessage. And we reserve some tokens in the end for an LLM response.

Principles

PriomptiPy operates on the principle of prioritized content rendering. Each element in a prompt can be assigned a priority using Scope, determining its importance in the overall context. This system allows for dynamic and efficient management of conversation flow, particularly in RAG applications where context space is limited but text abound.

Components

These are logical components of PriomptiPy. They work the same way as in the original library.

  • Scope: Groups messages and assigns priorities, dictating which messages should be rendered first.
  • Empty: Reserves space in the prompt, useful for ensuring there's room for AI-generated content.
  • Isolate: A section of the prompt with its own token limit. Useful when you want to include limited information from multiple sources.
  • First: Sufficiently high child is selected for inclusion, while subsequent children are excluded. This feature is beneficial for creating fallback mechanisms, such as displaying a message like "(result omitted)" when the output exceeds a certain length.
  • Capture: Capture the output and parse it right within the prompt. Implemented but not functional yet.

And these are the message components that are used to build the content to send to AI models -

  • SystemMessage: Represents system-level information.
  • UserMessage/AssistantMessage: Denotes messages from the user or the AI assistant.
  • Function: Encapsulates a callable function within the prompt. The callable feature isn't fully supported yet.

Caveats

While PriomptiPy enhances prompt management, it requires careful consideration of priorities to avoid overcomplicating prompts. It's crucial to balance the use of priorities to maintain efficient and cache-friendly prompts.

  • Runnable function calling and capturing isn't supported yet. Will look to support this in the future.
  • Just like the JS library, we haven't solved for cacheing yet. Would benefit from some help here.
  • Will add more examples shortly, in the meantime, kindly take a look at the Tests for some sample usage.
  • Would appreciate any support with maintaining and developing this library and keeping it in sync with the awesome Priompt library. There could be bugs in our current implementation as well, so use with caution.

Contributions

This library was co-authored by Tanmay Gupta and Samarth Makhija, the founders of Quarkle. We warmly welcome contributions to PriomptiPy. The project is open-source under the MIT license, encouraging a collaborative and innovative community.

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

priomptipy-0.18.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

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

priomptipy-0.18-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file priomptipy-0.18.tar.gz.

File metadata

  • Download URL: priomptipy-0.18.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for priomptipy-0.18.tar.gz
Algorithm Hash digest
SHA256 058eaebbd79410b2c0797d483dfe2f019075c924494ce9c7a70f07f03103c38d
MD5 08e3fd636ed94e8979adc43b9cd486e7
BLAKE2b-256 52ae9ced593b82f4aaa3d0138aab0af98bc3e17c0806210a9426203347bcb4ce

See more details on using hashes here.

File details

Details for the file priomptipy-0.18-py3-none-any.whl.

File metadata

  • Download URL: priomptipy-0.18-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.14

File hashes

Hashes for priomptipy-0.18-py3-none-any.whl
Algorithm Hash digest
SHA256 854cac758a8364f752a9cf77df2c79857c69ecf3dbda948711a848705bd83f77
MD5 5cb6c422b42da738aa3abc74345f0c21
BLAKE2b-256 613ca92d6d95b84c79468c4018d87aa9b0ed0233ded739f1dcb3acb761c60bda

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