Skip to main content

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.

Metadata

Release files for promplate-inspect 0.0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for promplate-inspect 0.0.2.2
File Size Uploaded
promplate_inspect-0.0.2.2.tar.gz 3.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for promplate-inspect 0.0.2.2
File Interpreter ABI Platform
promplate_inspect-0.0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 6.1 kB

Release files / promplate_inspect-0.0.2.2.tar.gz

Download URL promplate_inspect-0.0.2.2.tar.gz
Size 3.0 kB
Tags Source
SHA-256 checksum
How to use checksums
a7a47c6189836970b286c1d67f10fd4ecbd97a9e04c629add9d05fa42d615cb2
BLAKE2b-256 checksum
How to use checksums
6f5c9373b3f6aa7ab541158a7e242041f1a55a360b0585e2514aeaca9d9479a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release files / promplate_inspect-0.0.2.2-py3-none-any.whl

Download URL promplate_inspect-0.0.2.2-py3-none-any.whl
Size 3.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0d98f545bdfcbf19d062f4b44c76a4a457bca0221eea126ac8f446150b951444
BLAKE2b-256 checksum
How to use checksums
a48574a6a363f9765e14db9900ef89b6b43e7164e8a8572382c10e9b52cae95c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.0.2.2 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page