promptsy


Namepromptsy JSON
Version 0.1.3.6 PyPI version JSON
download
home_pagehttps://github.com/feliperafael/promptsy
SummaryA Python library for managing and organizing prompts for Large Language Models (LLMs)
upload_time2024-09-11 19:02:16
maintainerNone
docs_urlNone
authorFelipe Rafael de Souza
requires_pythonNone
licenseNone
keywords prompts llm-prompts prompt-management large-language-models
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# 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:

```bash
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

```bash
set OPENAI_API_KEY=your_api_key_here
```

### On macOS/Linux

```bash
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:

```python
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

```python
manager = PromptManager(base_directory='prompts')
```

- `base_directory` (optional): The base directory where the prompts will be stored. Defaults to `'prompts'`.

### Saving a Prompt

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

### Loading a Prompt

```python
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

```python
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

```python
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

```python
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`:

```python
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:

```python
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](https://github.com/feliperafael/promptsy).

## License

This project is licensed under the [MIT License](LICENSE).

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/feliperafael/promptsy",
    "name": "promptsy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "prompts llm-prompts prompt-management large-language-models",
    "author": "Felipe Rafael de Souza",
    "author_email": "felipe@evakub.com.br",
    "download_url": "https://files.pythonhosted.org/packages/7f/a8/28db8ff2fa79aa7c68ea52d5e40ca2228071967b1de1a80b28641eb2e2ff/promptsy-0.1.3.6.tar.gz",
    "platform": null,
    "description": "\n# Promptsy\n\nPromptsy 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.\n\n## Features\n\n- Save prompts to YAML files with a specified name and directory structure\n- Load prompts from YAML files by name\n- Format prompts with dynamic values using keyword arguments\n- Automatically create directories for prompts if they don't exist\n- List all available prompts in the specified base directory\n- Colorized error messages for better visibility\n- Enhance prompts using OpenAI's language model\n- Generate few-shot examples for prompts using the `FewShotPromptGenerator`, allowing for improved context and response generation based on specified expected outputs.\n\n## Installation\n\nTo install Promptsy, you can use pip:\n\n```bash\npip install promptsy\n```\n\n## Configuration\n\nBefore 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\u2019s how to set it:\n\n### On Windows\n\n```bash\nset OPENAI_API_KEY=your_api_key_here\n```\n\n### On macOS/Linux\n\n```bash\nexport OPENAI_API_KEY=your_api_key_here\n```\n\nReplace `your_api_key_here` with your actual OpenAI API key.\n\n## Usage\n\nHere's a basic example of how to use Promptsy:\n\n```python\nfrom promptsy.prompt import Prompt\n\n# Create an instance of Prompt\nprompt = Prompt(\n    name=\"hello_world\",\n    description=\"An example prompt\",\n    template=\"Hello, {name}!\"\n)\n\n# Save the prompt using the Prompt's save method\nprompt.save()  # N\u00e3o \u00e9 necess\u00e1rio passar o PromptManager\n\n# Load the prompt using the Prompt's load method\nloaded_prompt = Prompt.load(\"hello_world\")  # N\u00e3o \u00e9 necess\u00e1rio passar o PromptManager\n\nprint(loaded_prompt)  # Output: hello_world: An example prompt\nprint(loaded_prompt.format(name=\"Taylor Swift\"))  # Output: Hello, Taylor Swift!\n```\n\n## Prompt Manager\n\nThe `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.\n\n### Initialization\n\n```python\nmanager = PromptManager(base_directory='prompts')\n```\n\n- `base_directory` (optional): The base directory where the prompts will be stored. Defaults to `'prompts'`.\n\n### Saving a Prompt\n\n```python\nprompt.save(name)  # O m\u00e9todo save agora \u00e9 chamado diretamente no objeto Prompt\n```\n\n### Loading a Prompt\n\n```python\nloaded_prompt = Prompt.load(name)  # O m\u00e9todo load agora \u00e9 chamado diretamente na classe Prompt\n```\n\n- `name` (str): The name of the prompt to load.\n- Returns: The loaded prompt data.\n\n### Listing Prompts\n\n```python\nprompts = manager.list_prompts()\n```\n\n- Returns: A list of prompt names.\n\n## Prompt Enhancer\n\nThe `PromptEnhancer` class allows you to enhance prompts using OpenAI's language model. It generates improved versions of prompts based on the original template.\n\n### Initialization\n\n```python\nenhancer = PromptEnhancer(api_key='your_api_key_here')\n```\n\n- `api_key` (str, optional): OpenAI API key. If None, it tries to retrieve from the environment variable.\n- `model_name` (str, optional): The name of the model to be used (default: \"gpt-4o-mini\").\n\n### Enhancing a Prompt\n\n```python\nenhanced_prompt = enhancer.enhance_prompt(prompt)\n```\n\n- `prompt` (Prompt): A Prompt object containing the original prompt template.\n- Returns: A new Prompt object with the enhanced template.\n\n### Example Usage of Prompt Enhancer\n\nHere\u2019s an example of how to use the `PromptEnhancer` with the `prompt_toddlers_story_time`:\n\n```python\nfrom promptsy.prompt import Prompt\nfrom promptsy.prompt_manager import PromptManager\nfrom promptsy.prompt_enhancer import PromptEnhancer\n\n# Create an instance of PromptManager\nmanager = PromptManager()\n\n# Create an instance of Prompt for toddlers story time\nprompt_toddlers_story_time = Prompt(\n    name=\"toddlers_story_time\",\n    description=\"Write a bedtime story for toddlers\",\n    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}.\"\n)\n\n# Save the original prompt\nprompt_toddlers_story_time.save()\n\n# Create an instance of PromptEnhancer\nenhancer = PromptEnhancer()\n\n# Enhance the prompt\nenhanced_prompt = enhancer.enhance_prompt(prompt_toddlers_story_time)\n\nprint(\"Enhanced Prompt:\")\nprint(enhanced_prompt.template)\n```\n\n### Saving Enhanced Prompts\n\nThe enhanced prompts are automatically saved in the `enhanced_prompts` directory.\n\n## Example Usage of FewShotPromptGenerator\n\nHere\u2019s an example of how to use the `FewShotPromptGenerator` to generate few-shot examples for a sentiment analysis prompt:\n\n```python\nfrom promptsy.auto_few_shot_generator import FewShotPromptGenerator\nfrom promptsy.prompt import Prompt\n\n# Create an instance of Prompt for sentiment analysis\nsentiment_analysis_prompt = Prompt(\n    name=\"sentiment_analysis\",\n    description=\"Generate a sentiment analysis prompt\",\n    template=\"You are a sentiment analysis prompt generator. Generate a prompt for sentiment analysis for the following text: {text}\"\n)\n\n# Create an instance of FewShotPromptGenerator\nfew_shot_generator = FewShotPromptGenerator(model_name=\"gpt-4o-mini\")\n\n# Generate few-shot examples using the sentiment analysis prompt\nnum_examples = 3\nexpected_outputs = [\"negative\", \"positive\", \"neutral\"]\nsentiment_analysis_prompt_with_examples = few_shot_generator.generate_examples(\n    prompt_initial=sentiment_analysis_prompt,\n    num_examples=num_examples,\n    expected_outputs=expected_outputs,\n    return_examples=False\n)\n\nprint(\"Formatted Prompt with Few-Shot Examples:\")\nprint(sentiment_analysis_prompt_with_examples.template)\n\n# Optionally, you can call the OpenAI API with the generated prompt\nfinal_prompt = sentiment_analysis_prompt_with_examples.format(text=\"I am so happy today, but I am tired!\")\nprint(final_prompt)\n```\n\n## Contributing\n\nContributions 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](https://github.com/feliperafael/promptsy).\n\n## License\n\nThis project is licensed under the [MIT License](LICENSE).\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "A Python library for managing and organizing prompts for Large Language Models (LLMs)",
    "version": "0.1.3.6",
    "project_urls": {
        "Homepage": "https://github.com/feliperafael/promptsy"
    },
    "split_keywords": [
        "prompts",
        "llm-prompts",
        "prompt-management",
        "large-language-models"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c02862a1c25e698f1fd9dcd0e6490f6695ac37f183e0a5b25d609af63ceb1c65",
                "md5": "dbe1b0ffae29275e89d88c6378dcdc53",
                "sha256": "f457684728864bda4cef42ed76750791c79d73585ab6917ac427ab2e25329f16"
            },
            "downloads": -1,
            "filename": "promptsy-0.1.3.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "dbe1b0ffae29275e89d88c6378dcdc53",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 11625,
            "upload_time": "2024-09-11T19:02:14",
            "upload_time_iso_8601": "2024-09-11T19:02:14.771092Z",
            "url": "https://files.pythonhosted.org/packages/c0/28/62a1c25e698f1fd9dcd0e6490f6695ac37f183e0a5b25d609af63ceb1c65/promptsy-0.1.3.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7fa828db8ff2fa79aa7c68ea52d5e40ca2228071967b1de1a80b28641eb2e2ff",
                "md5": "17778cccd998ef8836bc24ee621e5244",
                "sha256": "3043427bef7ce03134b12526af6eda81d000d11885c4476d377617b11c97a259"
            },
            "downloads": -1,
            "filename": "promptsy-0.1.3.6.tar.gz",
            "has_sig": false,
            "md5_digest": "17778cccd998ef8836bc24ee621e5244",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 11289,
            "upload_time": "2024-09-11T19:02:16",
            "upload_time_iso_8601": "2024-09-11T19:02:16.408756Z",
            "url": "https://files.pythonhosted.org/packages/7f/a8/28db8ff2fa79aa7c68ea52d5e40ca2228071967b1de1a80b28641eb2e2ff/promptsy-0.1.3.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-09-11 19:02:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "feliperafael",
    "github_project": "promptsy",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "promptsy"
}
        
Elapsed time: 9.86849s