Skip to main content

A simple Python library for parsing LLM prompts.

Project description

Prompt Parser

GitHub LICENSE Mounthly Download latest version supported python version

A simple Python library for parsing LLM prompts.

Usage

from prompt_parser import Prompt

prompt = Prompt.parse("""
---
temperature: 0.5
top_p: 0.5
top_k: 50
max_tokens: 4096
provider: openai
model: gpt-4
endpoint: chat
tools: [{
    "name": "get_weather",
    "description": "Fetches the weather in the given location",
    "strict": true,
    "parameters": {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "The location to get the weather for"
            },
            "unit": {
                "type": ["string", "null"],
                "description": "The unit to return the temperature in",
                "enum": ["F", "C"]
            }
        },
        "additionalProperties": false,
        "required": [
            "location", "unit"
        ]
    }
}]
unknown: blablah
---

<system>
Hi from system
</system>

<user>
Hi from user {custom}
</user>

<assistant>
Hi from assistant
</assistant>
""")


# getters
prompt.system  # Hi from system
# or
prompt.system_forced  # "Hi from system" -> will throw an error if not present

prompt.user  # Hi from user {custom}
# or
prompt.user_forced  # "Hi from user {custom}" -> will throw an error if not present

prompt.assistant  # Hi from assistant
# or
prompt.assistant_forced  # "Hi from assistant" -> will throw an error if not present

prompt.format_user(custom="Alex")  # "Hi from user Alex"
# check also `format_system` and `format_assistant`

# access the attributes
prompt.attributes.temperature  # 0.5
prompt.attributes.top_p  # 0.5
prompt.attributes.top_k  # 50
prompt.attributes.provider  # openai
prompt.attributes.model  # gpt-4
prompt.attributes.endpoint  # chat
prompt.attributes.max_tokens  # 4096
prompt.attributes["unknown"]  # blahblah

You can also use the Prompt.parse_from_file(path) method to parse a prompt file given its path.

You can convert back a prompt to a string

print(str(prompt))

# or store it in a file
with open("task.prompt", "w") as f:
  f.write(str(prompt))

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

prompt_parser-0.3.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

prompt_parser-0.3.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file prompt_parser-0.3.0.tar.gz.

File metadata

  • Download URL: prompt_parser-0.3.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for prompt_parser-0.3.0.tar.gz
Algorithm Hash digest
SHA256 1c53301baefcf3d4a1aa40d7df508978d0db1c337621eed5ba1feba309d92f97
MD5 5507633b5ec7a83ab275334df3eb753a
BLAKE2b-256 514504dc7ed6f123912af1660a2655661b8a332c9b2dca765e3542f3f5797b73

See more details on using hashes here.

File details

Details for the file prompt_parser-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: prompt_parser-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for prompt_parser-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b649848ef1163915bc3872d0572baa0b968feb04af3dc1404bacdb0ffd6d92d6
MD5 c16687b484c8bbc3b29ab6e36e0507db
BLAKE2b-256 43bf641100878dad3beaf728d777401a186e014feb042ca40304c6984b696c15

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