Skip to main content

PromptHandler

Focus on helping people not on managing prompts.

Manages Tokens (within limit), removes older message automatically, Summarizes

Installation

pip install prompthandler

Usage

An example code to make the model chat with you in terminal

from prompthandler import Prompthandler
model = Prompthandler()
model.add_system("You are now user's girlfriend so take care of him",to_head=True) # Makes this to go on to the head. Head is not rolled so it stays the same
model.add_user("Hi")
model.chat() # you can chat with it in terminal

For more examples see examples.ipynb Example Projects my silicon version github_repo

Behind the scenes

models.py

openai_chat_gpt class

This class represents the interaction with the GPT-3.5-turbo model (or other OpenAI models). It provides methods for generating completions for given messages and managing the conversation history.

Attributes:

  • api_key (str): The OpenAI API key.
  • model (str): The name of the OpenAI model to use.
  • MAX_TOKEN (int): The maximum number of tokens allowed for the generated completion.
  • temperature (float): The temperature parameter controlling the randomness of the output.

Methods:

  1. __init__(self, api_key=None, MAX_TOKEN=4096, temperature=0, model="gpt-3.5-turbo-0613"): Initializes the OpenAI chat model with the provided settings.

  2. get_completion_for_message(self, message, temperature=None): Generates a completion for a given message using the specified OpenAI model.

    • message (list): List of messages representing the conversation history.
    • temperature (float): Control the randomness of the output. If not provided, it uses the default temperature.

    Returns a tuple containing the completion generated by the model and the number of tokens used by the completion.

prompts.py

PromptHandler class

This class represents a conversation prompt history for interacting with the GPT-3.5-turbo model (or other OpenAI models). It extends the openai_chat_gpt class and provides additional methods for handling prompts, headers, and body messages.

Attributes (in addition to openai_chat_gpt attributes):

  • headers (list): List of header messages in the conversation history.
  • body (list): List of body messages in the conversation history.
  • head_tokens (int): Total tokens used in the headers.
  • body_tokens (int): Total tokens used in the body.
  • tokens (int): Total tokens used in the entire message history.

Methods (in addition to openai_chat_gpt methods):

  1. __init__(self, MAX_TOKEN=4096, api_key=None, temperature=0, model="gpt-3.5-turbo-0613"): Initializes the PromptHandler with the specified settings.

  2. get_completion(self, message='', update_history=True, temperature=None): Generates a completion for the conversation history.

    • message (str): The user's message to be added to the history.
    • update_history (bool): Flag to update the conversation history.
    • temperature (float): Control the randomness of the output.

    Returns a tuple containing the completion generated by the model and the number of tokens used by the completion.

  3. chat(self, update_history=True, temperature=None): Starts a conversation with the model. Accepts terminal input and prints the model's responses.

    • update_history (bool): Flag to update the conversation history.
    • temperature (float): Control the randomness of the output.
  4. update_messages(self): Combines the headers and body messages into a single message history.

    Returns the combined list of messages representing the conversation history.

  5. update_tokens(self): Updates the count of tokens used in the headers, body, and entire message history.

    Returns a tuple containing the total tokens used, tokens used in headers, and tokens used in the body.

  6. calibrate(self, MAX_TOKEN=None): Calibrates the message history by removing older messages if the total token count exceeds MAX_TOKEN.

    • MAX_TOKEN (int): The maximum number of tokens allowed for the generated completion.
  7. add(self, role, content, to_head=False): Adds a message to the message history.

    • role (str): The role of the message (user, assistant, etc.).
    • content (str): The content of the message.
    • to_head (bool): Specifies whether the message should be appended to the headers list. If False, it will be appended to the body list.

    Returns the last message in the message history.

  8. append(self, content_list): Appends a list of messages to the message history.

    • content_list (list): List of messages to be appended.
  9. get_last_message(self): Returns the last message in the message history.

    Returns the last message as a dictionary containing the role and content of the message.

  10. get_token_for_message(self, messages, model_name="gpt-3.5-turbo-0613"): Returns the number of tokens used by a list of messages.

  • messages (list): List of messages to count tokens for.
  • model_name (str): The name of the OpenAI model used for token encoding.

Returns the number of tokens used by the provided list of messages.

Metadata

Release files for prompthandler 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prompthandler 0.3.3
File Size Uploaded Provenance
prompthandler-0.3.3.tar.gz 6.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for prompthandler 0.3.3
File Interpreter ABI Platform
prompthandler-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 14.8 kB

Release files / prompthandler-0.3.3.tar.gz

Download URL prompthandler-0.3.3.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
2ca8177b4ef21215067e56769b511668ef6d766269d8e80dff0eadc8416db90c
BLAKE2b-256 checksum
How to use checksums
f129efeafb854477075fd947ecafe14c6f6466dd42671c53989f2c4638e4ed34
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Release files / prompthandler-0.3.3-py3-none-any.whl

Download URL prompthandler-0.3.3-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f5ce6d1183bd91efaae3ebfa33a8647a27f92fce487c3303e5f77bb9b11d8af8
BLAKE2b-256 checksum
How to use checksums
e0ca962dce5b40ff66eda4313e644174d51f805c9989c7c5c4cb5dd17aa2fa6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.17

Security for prompthandler 0.3.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page