Skip to main content

[WIP] Inspect a promplate.Template to get its signatures

Project description

promplate-inspect

Inspect input variables used in a promplate template.

Useful when instructing an LLM to produce structural data with certain fields, or for you to quickly understand the data structure a template expects.

pip install promplate-inspect

Usage

from promplate import Template
from promplate_inspect import find_input_variables_in_template

t = Template("""

    {#
        import a as b
        from c import d
    #}

    {{ e }}

    {{ f.strip() }}

    {{ list }}

    {#
        g += 1
        h = { **h }
    #}

    {% for i in func() %}

        {{ [ j for j in range(i) ] }}

    {% endfor %}

""")

find_input_variables_in_template(t)  # {'e', 'f', 'g', 'h'}

Note that it excludes builtin names by default. If you allow LLM to output variables like dict or list, you can specify the second parameter exclude_builtins:

find_input_variables_in_template(t, exclude_builtins=False)  # {'e', 'f', 'g', 'h', 'list'}

As you see, variables that defined explicitly in the templates are not filtered out as expected. And functions above like func and range are always excluded because LLM can never out callable instances typically.

Limitations

  1. This parser is still work in progress. If you includes any of these code in your {{ }}/{# #} tags, the result may be wrong:
    • Parameters of function. Inspecting {{ (lambda x: x + 1)(y) }} should output {'y'} but outputs {'x', 'y'} for now.
    • Similarly, variables defined in with statements and except statements are not excluded for now.
  2. This implementation use ast to parse the template. It never run the code, so these monkey patches won't be tracked:
    • Getting variables from globals()/locals()/__main__ won't be finded. Such as name won't be found in {{ locals()["name"] }}.
    • Assigning values to globals()/locals()/__main__ won't be excluded. Even if you wrote {# globals().setdefault("name", "value") #}, name will still be found in {{ name }}.
  3. For now it only find variables in the given template itself. It don't find variables needed in the components used by it.

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

promplate_inspect-0.0.2.2.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

promplate_inspect-0.0.2.2-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

Details for the file promplate_inspect-0.0.2.2.tar.gz.

File metadata

  • Download URL: promplate_inspect-0.0.2.2.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for promplate_inspect-0.0.2.2.tar.gz
Algorithm Hash digest
SHA256 a7a47c6189836970b286c1d67f10fd4ecbd97a9e04c629add9d05fa42d615cb2
MD5 c58d8bdcd0cb7495e2c9b309cee34391
BLAKE2b-256 6f5c9373b3f6aa7ab541158a7e242041f1a55a360b0585e2514aeaca9d9479a8

See more details on using hashes here.

File details

Details for the file promplate_inspect-0.0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for promplate_inspect-0.0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0d98f545bdfcbf19d062f4b44c76a4a457bca0221eea126ac8f446150b951444
MD5 b884b7624acfa492dcf5dc712b9135d0
BLAKE2b-256 a48574a6a363f9765e14db9900ef89b6b43e7164e8a8572382c10e9b52cae95c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page