Skip to main content

A Python library for managing and organizing prompts for Large Language Models (LLMs)

Project description

Promptsy

Promptsy is a Python library designed for managing and organizing prompts for language models in a structured way. It provides a convenient method to store, retrieve, and format prompts using YAML files.

Features

  • Save prompts to YAML files with a specified name and directory structure
  • Load prompts from YAML files by name
  • Format prompts with dynamic values using keyword arguments
  • Automatically create directories for prompts if they don't exist
  • List all available prompts in the specified base directory
  • Colorized error messages for better visibility
  • Enhance prompts using OpenAI's language model
  • Generate few-shot examples for prompts using the FewShotPromptGenerator, allowing for improved context and response generation based on specified expected outputs.

Installation

To install Promptsy, you can use pip:

pip install promptsy

Configuration

Before using the PromptEnhancer, you need to set up your OpenAI API key. You can do this by setting the OPENAI_API_KEY environment variable. Here’s how to set it:

On Windows

set OPENAI_API_KEY=your_api_key_here

On macOS/Linux

export OPENAI_API_KEY=your_api_key_here

Replace your_api_key_here with your actual OpenAI API key.

Usage

Here's a basic example of how to use Promptsy:

from promptsy.prompt import Prompt

# Create an instance of Prompt
prompt = Prompt(
    name="hello_world",
    description="An example prompt",
    template="Hello, {name}!"
)

# Save the prompt using the Prompt's save method
prompt.save()  # Não é necessário passar o PromptManager

# Load the prompt using the Prompt's load method
loaded_prompt = Prompt.load("hello_world")  # Não é necessário passar o PromptManager

print(loaded_prompt)  # Output: hello_world: An example prompt
print(loaded_prompt.format(name="Taylor Swift"))  # Output: Hello, Taylor Swift!

Prompt Manager

The PromptManager class is responsible for managing the storage and retrieval of prompts. It provides methods for saving prompts to YAML files, loading prompts from YAML files, and listing all available prompts.

Initialization

manager = PromptManager(base_directory='prompts')
  • base_directory (optional): The base directory where the prompts will be stored. Defaults to 'prompts'.

Saving a Prompt

prompt.save(name)  # O método save agora é chamado diretamente no objeto Prompt

Loading a Prompt

loaded_prompt = Prompt.load(name)  # O método load agora é chamado diretamente na classe Prompt
  • name (str): The name of the prompt to load.
  • Returns: The loaded prompt data.

Listing Prompts

prompts = manager.list_prompts()
  • Returns: A list of prompt names.

Prompt Enhancer

The PromptEnhancer class allows you to enhance prompts using OpenAI's language model. It generates improved versions of prompts based on the original template.

Initialization

enhancer = PromptEnhancer(api_key='your_api_key_here')
  • api_key (str, optional): OpenAI API key. If None, it tries to retrieve from the environment variable.
  • model_name (str, optional): The name of the model to be used (default: "gpt-4o-mini").

Enhancing a Prompt

enhanced_prompt = enhancer.enhance_prompt(prompt)
  • prompt (Prompt): A Prompt object containing the original prompt template.
  • Returns: A new Prompt object with the enhanced template.

Example Usage of Prompt Enhancer

Here’s an example of how to use the PromptEnhancer with the prompt_toddlers_story_time:

from promptsy.prompt import Prompt
from promptsy.prompt_manager import PromptManager
from promptsy.prompt_enhancer import PromptEnhancer

# Create an instance of PromptManager
manager = PromptManager()

# Create an instance of Prompt for toddlers story time
prompt_toddlers_story_time = Prompt(
    name="toddlers_story_time",
    description="Write a bedtime story for toddlers",
    template="You are a bedtime story teller for toddlers. Write a story for a toddler about a {animal} that goes on an adventure to {place}."
)

# Save the original prompt
prompt_toddlers_story_time.save()

# Create an instance of PromptEnhancer
enhancer = PromptEnhancer()

# Enhance the prompt
enhanced_prompt = enhancer.enhance_prompt(prompt_toddlers_story_time)

print("Enhanced Prompt:")
print(enhanced_prompt.template)

Saving Enhanced Prompts

The enhanced prompts are automatically saved in the enhanced_prompts directory.

Example Usage of FewShotPromptGenerator

Here’s an example of how to use the FewShotPromptGenerator to generate few-shot examples for a sentiment analysis prompt:

from promptsy.auto_few_shot_generator import FewShotPromptGenerator
from promptsy.prompt import Prompt

# Create an instance of Prompt for sentiment analysis
sentiment_analysis_prompt = Prompt(
    name="sentiment_analysis",
    description="Generate a sentiment analysis prompt",
    template="You are a sentiment analysis prompt generator. Generate a prompt for sentiment analysis for the following text: {text}"
)

# Create an instance of FewShotPromptGenerator
few_shot_generator = FewShotPromptGenerator(model_name="gpt-4o-mini")

# Generate few-shot examples using the sentiment analysis prompt
num_examples = 3
expected_outputs = ["negative", "positive", "neutral"]
sentiment_analysis_prompt_with_examples = few_shot_generator.generate_examples(
    prompt_initial=sentiment_analysis_prompt,
    num_examples=num_examples,
    expected_outputs=expected_outputs,
    return_examples=False
)

print("Formatted Prompt with Few-Shot Examples:")
print(sentiment_analysis_prompt_with_examples.template)

# Optionally, you can call the OpenAI API with the generated prompt
final_prompt = sentiment_analysis_prompt_with_examples.format(text="I am so happy today, but I am tired!")
print(final_prompt)

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License.

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

promptsy-0.1.3.6.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

promptsy-0.1.3.6-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file promptsy-0.1.3.6.tar.gz.

File metadata

  • Download URL: promptsy-0.1.3.6.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.7

File hashes

Hashes for promptsy-0.1.3.6.tar.gz
Algorithm Hash digest
SHA256 3043427bef7ce03134b12526af6eda81d000d11885c4476d377617b11c97a259
MD5 17778cccd998ef8836bc24ee621e5244
BLAKE2b-256 7fa828db8ff2fa79aa7c68ea52d5e40ca2228071967b1de1a80b28641eb2e2ff

See more details on using hashes here.

File details

Details for the file promptsy-0.1.3.6-py3-none-any.whl.

File metadata

  • Download URL: promptsy-0.1.3.6-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.7

File hashes

Hashes for promptsy-0.1.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f457684728864bda4cef42ed76750791c79d73585ab6917ac427ab2e25329f16
MD5 dbe1b0ffae29275e89d88c6378dcdc53
BLAKE2b-256 c02862a1c25e698f1fd9dcd0e6490f6695ac37f183e0a5b25d609af63ceb1c65

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