promptex


Namepromptex JSON
Version 0.1.0 PyPI version JSON
download
home_pagehttps://github.com/rinov/promptex
SummaryEfficiently manage prompts used as input for GPT
upload_time2023-07-30 18:58:28
maintainer
docs_urlNone
authorrinov
requires_python>=3.8.0
licenseMIT
keywords gpt prompt promptex
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Promptex

Promptex is a Python library built for efficient and effective management of prompts utilized in Generative Pre-trained Transformer (GPT) models. Whether you're a researcher or a developer, this tool offers an easy way to manage, integrate, and analyze your prompts.

## Key Features

- **Prompt Management**: Provides methods to set up and get prompts, allowing for flexibility to match your unique needs.
- **Data Integration**: Supports a wide array of file formats (JSON, JSON Schema, CSV, XML, YAML, HTML) for seamless integration into your data pipeline.
- **Analytics**: Equips you with the ability to gather detailed statistics about the elements in your prompts, such as text length and token count, valuable for fine-tuning your model.
- **Scalability**: Designed with large-scale projects in mind, ensuring efficiency regardless of the size of your GPT-based projects.

By offering this wide range of functionalities, Promptex provides a flexible and efficient way to work with prompts in transformer models. Join us on this journey towards making GPT prompt handling simpler, more efficient, and effective!

## Why is Prompt Management and Analysis Necessary?

Generative Pre-trained Transformer (GPT) models and the like generate output based on input prompts. Prompts serve as essential cues that instruct the model what to generate, and the selection and management of these prompts significantly impact the quality and relevance of the results.

Moreover, efficiently managing prompts used in projects or research is crucial to ensure consistency and reproducibility. Promptex addresses these challenges, and by understanding not just the meaning of the sentences, but also the characteristics of the prompts, such as their composition and token count, it provides opportunities to maximize the performance of GPT models.

Therefore, effective prompt management and analysis not only aid in producing better and more consistent results but also pave the way to harness the full potential of GPT models.

## Installation

To install:

```bash
pip install -U pip
pip install promptex
```

Alternatively, you can develop it locally:

```bash
git clone https://github.com/rinov/promptex.git
cd promptex
pip install -e .
```

## Prompt Components

In Promptex, Some of fundamental elements that make up a prompt for Generative Pre-trained Transformer (GPT) models. These elements, each with a specific role and priority, form the building blocks for creating effective prompts:

ROLE: This element indicates the role of the prompt.
INSTRUCTION: This element provides specific instructions to guide the GPT model's response.
CONSTRAINT: This element defines any constraints or limitations that the GPT model should adhere to while generating a response.
CONTEXT: This element gives the context or background information necessary for understanding the prompt.
INPUT_DATA: This element represents the specific input data that the GPT model needs to generate a response.
OUTPUT_FORMAT: This element specifies the desired format of the model's output.


## Getting Started

```python
from promptex.promptex import Promptex
from promptex.elements import *
from promptex.encoding_strategy import *


promptex = Promptex()

promptex.set_elements(
    [
        Role("You're a renowned RPG game designer."),
        Instruction(
            "Create an innovative quest for a new fantasy RPG game."
        ).add_elements(
            [
                Constraint(
                    "Ensure the quest is unique and rewarding for players."
                ),
            ]
        ),
        OutputFormat("Markdown text in English"),
    ]
)

strategy = SimpleTextEncodingStrategy()
prompt = strategy.encode(promptex)
print(prompt)

"""
### ROLE
You're a renowned RPG game designer.
###

### INSTRUCTION
Create an innovative quest for a new fantasy RPG game.
###

### CONSTRAINT
Ensure the quest is unique and rewarding for players.
###

### OUTPUT_FORMAT
Markdown text in English
###
"""
```

## Examples
For a more detailed understanding and hands-on experience, please refer to the "examples" directory.

### Manage your prompt
```python
path = "examples/test.json"

# Save the prompt to a file
promptex.save_prompt(path)

# Load the prompt from a file
promptex = promptex.load_prompt(path)
```

### Show stats of prompt
```python
from promptex.promptex import Promptex
from promptex.elements import *
from promptex.encoding_strategy import *


promptex = Promptex()

promptex.set_elements(
    [
        Instruction("Create an innovative quest for a new fantasy RPG game."),
        OutputFormat("Markdown text in English"),
    ]
)

text = SimpleTextEncodingStrategy().encode(promptex)
encoding_model_name = "gpt-4"
token_count = promptex.get_token_count(
    text=text, encoding_model_name=encoding_model_name
)
stats = promptex.get_stats(text=text, encoding_model_name=encoding_model_name)

print(f"Token consumption: {token_count}")

print(f"Stats: {json.dumps(stats, indent=2, ensure_ascii=False)}")


Token consumption: 27
Stats: {
  "element_count": {
    "Instruction": 1,
    "OutputFormat": 1,
    "total": 2
  },
  "text_length": {
    "Instruction": {
      "min": 0,
      "max": 54,
      "avg": 54.0
    },
    "OutputFormat": {
      "min": 0,
      "max": 24,
      "avg": 24.0
    },
    "total": 124
  },
  "token_count": {
    "Instruction": {
      "min": 0,
      "max": 11,
      "avg": 11.0
    },
    "OutputFormat": {
      "min": 0,
      "max": 4,
      "avg": 4.0
    },
    "total": 27
  }
}
```

### Encoding a prompt as JSON
```python
strategy = SimpleJsonEncodingStrategy()
prompt = strategy.encode(promptex)
print(prompt)

"""
[
  {
    "type": "role",
    "text": "You're a renowned RPG game designer."
  },
  {
    "type": "instruction",
    "text": "Create an innovative quest for a new fantasy RPG game.",
    "elements": [
      {
        "type": "constraint",
        "text": "Ensure the quest is unique and rewarding for players."
      }
    ]
  },
  {
    "type": "output_format",
    "text": "Markdown text in English"
  }
]
"""
```

### Encoding a prompt as XML
```python
strategy = SimpleXmlEncodingStrategy()
prompt = strategy.encode(promptex)
print(prompt)

"""
<?xml version="1.0" ?>
<root>
  <role>You're a renowned RPG game designer.</role>
  <instruction>
    Create an innovative quest for a new fantasy RPG game.
    <constraint>Ensure the quest is unique and rewarding for players.</constraint>
  </instruction>
  <output_format>Markdown text in English</output_format>
</root>
"""
```

### Encoding a prompt as JSON Schema
```python
strategy = SimpleXmlEncodingStrategy()
prompt = strategy.encode(promptex)
print(prompt)

"""
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "role": {
      "type": "object",
      "properties": {
        "text": {
          "type": "string"
        }
      }
    },
    "instruction": {
      "type": "object",
      "properties": {
        "text": {
          "type": "string"
        },
        "elements": {
          "type": "array",
          "items": [
            {
              "type": "object",
              "properties": {
                "text": {
                  "type": "string"
                }
              }
            }
          ]
        }
      }
    },
    "output_format": {
      "type": "object",
      "properties": {
        "text": {
          "type": "string"
        }
      }
    }
  },
  "required": [
    "role",
    "instruction",
    "output_format"
  ]
}
"""
```

### Customize encoding strategy
```python
from promptex.promptex import Promptex
from promptex.elements import *
from promptex.encoding_strategy import *

# Add a new encoding strategy
class BinaryEncodingStrategy(BaseEncodingStrategy):
    def __init__(self):
        super().__init__()

    def encode(self, promptex: Promptex) -> bytes:
        prompt = ""
        for element in promptex.elements:
            prompt += element.text
        return prompt.encode()


promptex = Promptex()

promptex.set_elements([Instruction("Create a new fantasy RPG game.")])

strategy = BinaryEncodingStrategy()
prompt = strategy.encode(promptex)
print(prompt)
```

### Setup your element type
```python

from promptex.promptex import Promptex
from promptex.elements import *
from promptex.encoding_strategy import *

# Add a new element type
class Example(Element):
    def __init__(self, text: str):
        super().__init__("example", text)


promptex = Promptex()

promptex.set_elements(
    [
        Instruction("Create an innovative quest for a new fantasy RPG game."),
        OutputFormat("Markdown text in English").add_element(
            Example("# Quest: The Lost Sword of the Kingdom")
        ),
    ]
)
```

## Contribute
We welcome all contributions. Feel free to submit a pull request or open an issue on our GitHub page.

## LICENSE
Promptex is licensed under the MIT license. For more information, see the LICENSE file.



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/rinov/promptex",
    "name": "promptex",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8.0",
    "maintainer_email": "",
    "keywords": "gpt,prompt,promptex",
    "author": "rinov",
    "author_email": "rinov@rinov.jp",
    "download_url": "https://files.pythonhosted.org/packages/76/e9/ec99420e41e48bae715c7652c2fccd3f96d468684e308320052eb46a485f/promptex-0.1.0.tar.gz",
    "platform": null,
    "description": "# Promptex\n\nPromptex is a Python library built for efficient and effective management of prompts utilized in Generative Pre-trained Transformer (GPT) models. Whether you're a researcher or a developer, this tool offers an easy way to manage, integrate, and analyze your prompts.\n\n## Key Features\n\n- **Prompt Management**: Provides methods to set up and get prompts, allowing for flexibility to match your unique needs.\n- **Data Integration**: Supports a wide array of file formats (JSON, JSON Schema, CSV, XML, YAML, HTML) for seamless integration into your data pipeline.\n- **Analytics**: Equips you with the ability to gather detailed statistics about the elements in your prompts, such as text length and token count, valuable for fine-tuning your model.\n- **Scalability**: Designed with large-scale projects in mind, ensuring efficiency regardless of the size of your GPT-based projects.\n\nBy offering this wide range of functionalities, Promptex provides a flexible and efficient way to work with prompts in transformer models. Join us on this journey towards making GPT prompt handling simpler, more efficient, and effective!\n\n## Why is Prompt Management and Analysis Necessary?\n\nGenerative Pre-trained Transformer (GPT) models and the like generate output based on input prompts. Prompts serve as essential cues that instruct the model what to generate, and the selection and management of these prompts significantly impact the quality and relevance of the results.\n\nMoreover, efficiently managing prompts used in projects or research is crucial to ensure consistency and reproducibility. Promptex addresses these challenges, and by understanding not just the meaning of the sentences, but also the characteristics of the prompts, such as their composition and token count, it provides opportunities to maximize the performance of GPT models.\n\nTherefore, effective prompt management and analysis not only aid in producing better and more consistent results but also pave the way to harness the full potential of GPT models.\n\n## Installation\n\nTo install:\n\n```bash\npip install -U pip\npip install promptex\n```\n\nAlternatively, you can develop it locally:\n\n```bash\ngit clone https://github.com/rinov/promptex.git\ncd promptex\npip install -e .\n```\n\n## Prompt Components\n\nIn Promptex, Some of fundamental elements that make up a prompt for Generative Pre-trained Transformer (GPT) models. These elements, each with a specific role and priority, form the building blocks for creating effective prompts:\n\nROLE: This element indicates the role of the prompt.\nINSTRUCTION: This element provides specific instructions to guide the GPT model's response.\nCONSTRAINT: This element defines any constraints or limitations that the GPT model should adhere to while generating a response.\nCONTEXT: This element gives the context or background information necessary for understanding the prompt.\nINPUT_DATA: This element represents the specific input data that the GPT model needs to generate a response.\nOUTPUT_FORMAT: This element specifies the desired format of the model's output.\n\n\n## Getting Started\n\n```python\nfrom promptex.promptex import Promptex\nfrom promptex.elements import *\nfrom promptex.encoding_strategy import *\n\n\npromptex = Promptex()\n\npromptex.set_elements(\n    [\n        Role(\"You're a renowned RPG game designer.\"),\n        Instruction(\n            \"Create an innovative quest for a new fantasy RPG game.\"\n        ).add_elements(\n            [\n                Constraint(\n                    \"Ensure the quest is unique and rewarding for players.\"\n                ),\n            ]\n        ),\n        OutputFormat(\"Markdown text in English\"),\n    ]\n)\n\nstrategy = SimpleTextEncodingStrategy()\nprompt = strategy.encode(promptex)\nprint(prompt)\n\n\"\"\"\n### ROLE\nYou're a renowned RPG game designer.\n###\n\n### INSTRUCTION\nCreate an innovative quest for a new fantasy RPG game.\n###\n\n### CONSTRAINT\nEnsure the quest is unique and rewarding for players.\n###\n\n### OUTPUT_FORMAT\nMarkdown text in English\n###\n\"\"\"\n```\n\n## Examples\nFor a more detailed understanding and hands-on experience, please refer to the \"examples\" directory.\n\n### Manage your prompt\n```python\npath = \"examples/test.json\"\n\n# Save the prompt to a file\npromptex.save_prompt(path)\n\n# Load the prompt from a file\npromptex = promptex.load_prompt(path)\n```\n\n### Show stats of prompt\n```python\nfrom promptex.promptex import Promptex\nfrom promptex.elements import *\nfrom promptex.encoding_strategy import *\n\n\npromptex = Promptex()\n\npromptex.set_elements(\n    [\n        Instruction(\"Create an innovative quest for a new fantasy RPG game.\"),\n        OutputFormat(\"Markdown text in English\"),\n    ]\n)\n\ntext = SimpleTextEncodingStrategy().encode(promptex)\nencoding_model_name = \"gpt-4\"\ntoken_count = promptex.get_token_count(\n    text=text, encoding_model_name=encoding_model_name\n)\nstats = promptex.get_stats(text=text, encoding_model_name=encoding_model_name)\n\nprint(f\"Token consumption: {token_count}\")\n\nprint(f\"Stats: {json.dumps(stats, indent=2, ensure_ascii=False)}\")\n\n\nToken consumption: 27\nStats: {\n  \"element_count\": {\n    \"Instruction\": 1,\n    \"OutputFormat\": 1,\n    \"total\": 2\n  },\n  \"text_length\": {\n    \"Instruction\": {\n      \"min\": 0,\n      \"max\": 54,\n      \"avg\": 54.0\n    },\n    \"OutputFormat\": {\n      \"min\": 0,\n      \"max\": 24,\n      \"avg\": 24.0\n    },\n    \"total\": 124\n  },\n  \"token_count\": {\n    \"Instruction\": {\n      \"min\": 0,\n      \"max\": 11,\n      \"avg\": 11.0\n    },\n    \"OutputFormat\": {\n      \"min\": 0,\n      \"max\": 4,\n      \"avg\": 4.0\n    },\n    \"total\": 27\n  }\n}\n```\n\n### Encoding a prompt as JSON\n```python\nstrategy = SimpleJsonEncodingStrategy()\nprompt = strategy.encode(promptex)\nprint(prompt)\n\n\"\"\"\n[\n  {\n    \"type\": \"role\",\n    \"text\": \"You're a renowned RPG game designer.\"\n  },\n  {\n    \"type\": \"instruction\",\n    \"text\": \"Create an innovative quest for a new fantasy RPG game.\",\n    \"elements\": [\n      {\n        \"type\": \"constraint\",\n        \"text\": \"Ensure the quest is unique and rewarding for players.\"\n      }\n    ]\n  },\n  {\n    \"type\": \"output_format\",\n    \"text\": \"Markdown text in English\"\n  }\n]\n\"\"\"\n```\n\n### Encoding a prompt as XML\n```python\nstrategy = SimpleXmlEncodingStrategy()\nprompt = strategy.encode(promptex)\nprint(prompt)\n\n\"\"\"\n<?xml version=\"1.0\" ?>\n<root>\n  <role>You're a renowned RPG game designer.</role>\n  <instruction>\n    Create an innovative quest for a new fantasy RPG game.\n    <constraint>Ensure the quest is unique and rewarding for players.</constraint>\n  </instruction>\n  <output_format>Markdown text in English</output_format>\n</root>\n\"\"\"\n```\n\n### Encoding a prompt as JSON Schema\n```python\nstrategy = SimpleXmlEncodingStrategy()\nprompt = strategy.encode(promptex)\nprint(prompt)\n\n\"\"\"\n{\n  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"role\": {\n      \"type\": \"object\",\n      \"properties\": {\n        \"text\": {\n          \"type\": \"string\"\n        }\n      }\n    },\n    \"instruction\": {\n      \"type\": \"object\",\n      \"properties\": {\n        \"text\": {\n          \"type\": \"string\"\n        },\n        \"elements\": {\n          \"type\": \"array\",\n          \"items\": [\n            {\n              \"type\": \"object\",\n              \"properties\": {\n                \"text\": {\n                  \"type\": \"string\"\n                }\n              }\n            }\n          ]\n        }\n      }\n    },\n    \"output_format\": {\n      \"type\": \"object\",\n      \"properties\": {\n        \"text\": {\n          \"type\": \"string\"\n        }\n      }\n    }\n  },\n  \"required\": [\n    \"role\",\n    \"instruction\",\n    \"output_format\"\n  ]\n}\n\"\"\"\n```\n\n### Customize encoding strategy\n```python\nfrom promptex.promptex import Promptex\nfrom promptex.elements import *\nfrom promptex.encoding_strategy import *\n\n# Add a new encoding strategy\nclass BinaryEncodingStrategy(BaseEncodingStrategy):\n    def __init__(self):\n        super().__init__()\n\n    def encode(self, promptex: Promptex) -> bytes:\n        prompt = \"\"\n        for element in promptex.elements:\n            prompt += element.text\n        return prompt.encode()\n\n\npromptex = Promptex()\n\npromptex.set_elements([Instruction(\"Create a new fantasy RPG game.\")])\n\nstrategy = BinaryEncodingStrategy()\nprompt = strategy.encode(promptex)\nprint(prompt)\n```\n\n### Setup your element type\n```python\n\nfrom promptex.promptex import Promptex\nfrom promptex.elements import *\nfrom promptex.encoding_strategy import *\n\n# Add a new element type\nclass Example(Element):\n    def __init__(self, text: str):\n        super().__init__(\"example\", text)\n\n\npromptex = Promptex()\n\npromptex.set_elements(\n    [\n        Instruction(\"Create an innovative quest for a new fantasy RPG game.\"),\n        OutputFormat(\"Markdown text in English\").add_element(\n            Example(\"# Quest: The Lost Sword of the Kingdom\")\n        ),\n    ]\n)\n```\n\n## Contribute\nWe welcome all contributions. Feel free to submit a pull request or open an issue on our GitHub page.\n\n## LICENSE\nPromptex is licensed under the MIT license. For more information, see the LICENSE file.\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Efficiently manage prompts used as input for GPT",
    "version": "0.1.0",
    "project_urls": {
        "Homepage": "https://github.com/rinov/promptex"
    },
    "split_keywords": [
        "gpt",
        "prompt",
        "promptex"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1feb5fb5a74a993d61e6e60dbb277a87096bff1d25bbfad4c02dabeacb020b38",
                "md5": "369253859cc396572a3f843f10183130",
                "sha256": "c7f29b92128a07149fb05ae102f401b1ad32f658409915abed824d2769894017"
            },
            "downloads": -1,
            "filename": "promptex-0.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "369253859cc396572a3f843f10183130",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8.0",
            "size": 18771,
            "upload_time": "2023-07-30T18:58:26",
            "upload_time_iso_8601": "2023-07-30T18:58:26.123823Z",
            "url": "https://files.pythonhosted.org/packages/1f/eb/5fb5a74a993d61e6e60dbb277a87096bff1d25bbfad4c02dabeacb020b38/promptex-0.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "76e9ec99420e41e48bae715c7652c2fccd3f96d468684e308320052eb46a485f",
                "md5": "33123c5e23a46e985d24becee12806f7",
                "sha256": "fca1734f0a01c8ce2ec238808a7b7ad7d77d14fd79b2e27dd1d8f2e04433c5dc"
            },
            "downloads": -1,
            "filename": "promptex-0.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "33123c5e23a46e985d24becee12806f7",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8.0",
            "size": 13557,
            "upload_time": "2023-07-30T18:58:28",
            "upload_time_iso_8601": "2023-07-30T18:58:28.205629Z",
            "url": "https://files.pythonhosted.org/packages/76/e9/ec99420e41e48bae715c7652c2fccd3f96d468684e308320052eb46a485f/promptex-0.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-30 18:58:28",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "rinov",
    "github_project": "promptex",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "promptex"
}
        
Elapsed time: 0.10848s