Skip to main content

Template-based prompt assembly with ordered slot injectors

Project description

contexting

Template-based prompt assembly. Fill {{slot}} placeholders in a base prompt using ordered injectors and a runtime context.

Installation

pip install contexting

How it works

  • Inline injection — if a slot exists in the template, the injector's output replaces it directly
  • Appended injection — if the slot is absent, the output is collected and appended after the filled template, separated by a blank line
  • Base fill — after all injectors run, remaining {{slots}} are filled directly from context (strings and numbers only)

API

from contexting import assemble, create_injector

create_injector(slot, fn)

param type description
slot str name matching {{slot}} in the template
fn Callable[[dict], str] receives the full context, returns the string to inject

assemble(base_prompt, rules, context)

param type description
base_prompt str template string with {{slot}} placeholders
rules list[Injector] ordered injectors applied before base fill
context dict passed to every injector fn; also fills remaining slots

Returns the assembled str.

Examples

Inline and appended injection

from contexting import assemble, create_injector

base_prompt = 'You are a {{role}} assistant for the language {{lang}}.'

# Inline — {{role}} exists in template, replaced directly
role_injector = create_injector('role', lambda ctx:
    'senior software engineer' if ctx.get('senior') else 'helpful coding'
)

# Appended — {{constraints}} absent, output appended after a blank line
constraints_injector = create_injector('constraints', lambda ctx:
    'Never suggest running untrusted code. Always explain risks.' if ctx.get('safe') else ''
)

lang_injector = create_injector('lang', lambda ctx:
    'javascript, node and typescript' if ctx.get('lang') == 'javascript' else 'python and java'
)

prompt = assemble(base_prompt, [role_injector, constraints_injector, lang_injector], {
    'senior': True,
    'safe': True,
    'lang': 'javascript',
})

print(prompt)
# You are a senior software engineer assistant for the language javascript, node and typescript.
#
# Never suggest running untrusted code. Always explain risks.

Base fill from context

Remaining slots not handled by any injector are filled directly from context:

prompt = assemble('Hello {{name}}, you are using {{lang}}.', [], {
    'name': 'Alice',
    'lang': 'python',
})

print(prompt)
# Hello Alice, you are using python.

Language-aware prompts

base_prompt = '{{lang_style}} Help the user with their question about {{topic}}.'

lang_style = create_injector('lang_style', lambda ctx: {
    'es': 'Respond only in Spanish. Be warm and conversational.',
    'fr': 'Respond only in French. Be formal and precise.',
}.get(ctx.get('lang'), 'Respond in English. Be clear and concise.'))

prompt = assemble(base_prompt, [lang_style], {'lang': 'es', 'topic': 'cooking'})

print(prompt)
# Respond only in Spanish. Be warm and conversational. Help the user with their question about cooking.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

contexting-0.1.1-cp38-abi3-win_amd64.whl (598.6 kB view details)

Uploaded CPython 3.8+Windows x86-64

contexting-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (684.7 kB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

contexting-0.1.1-cp38-abi3-macosx_11_0_arm64.whl (616.5 kB view details)

Uploaded CPython 3.8+macOS 11.0+ ARM64

File details

Details for the file contexting-0.1.1-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: contexting-0.1.1-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 598.6 kB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for contexting-0.1.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 fbb5129c10b9275be2c3d4dee66d63e63a1257eb628084f1d5f422880a3317d6
MD5 0dcabba509ee77dcea4ffdebeceaee41
BLAKE2b-256 50a38b17ba88ca38f66db6f77a7f3dd44fbe1f8e5868c692f10f72cb24e086fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for contexting-0.1.1-cp38-abi3-win_amd64.whl:

Publisher: release.yml on ActuallyGoodClub/contexting

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file contexting-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for contexting-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7c9452716c2546798ed6cde53e19ad34bdbe704589d328bb991c4799e054f92b
MD5 42700f36249e17bb16d19631e7645ed2
BLAKE2b-256 9d613fd4f33c56033bdde5d02a9f9102a0ec46d3db5d8debff4bee4cee887746

See more details on using hashes here.

Provenance

The following attestation bundles were made for contexting-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on ActuallyGoodClub/contexting

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file contexting-0.1.1-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for contexting-0.1.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b4248c4878cc78ccb430dae59d94373ed53eefaffe186088a8426be0e1b202f9
MD5 6167dbc615fcf9c46b4c54277b90b2c9
BLAKE2b-256 6d3dcc1bc4cc0ebdab002aefa7d6cb80650e01edff825ba8d57a8140ca9f4ece

See more details on using hashes here.

Provenance

The following attestation bundles were made for contexting-0.1.1-cp38-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on ActuallyGoodClub/contexting

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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