pyrompt.util#

Utility functions and convenience helpers for pyrompt.

Functions

quick_setup(project_name[, with_templates, ...])

Quick setup for a new pyrompt project.

import_from_dict(collection, prompts[, metadata])

Import prompts from a dictionary.

export_to_dict(collection[, include_metadata])

Export collection to a dictionary.

validate_template(template_str[, ...])

Validate a template string.

merge_collections(target, *sources[, ...])

Merge multiple collections into a target collection.

list_available_engines()

List all available template engines with their details.

render_template_file(file_path[, output_path])

Render a template file directly.

create_project_structure(project_name[, ...])

Create a complete project structure for prompt management.

get_stats(collection)

Get statistics about a collection.

pyrompt.util.create_project_structure(project_name, base_path=None, include_examples=True)[source]#

Create a complete project structure for prompt management.

Creates directories and example files for a new pyrompt project.

Parameters:
  • project_name (str) – Name of the project

  • base_path (Optional[str]) – Optional base path

  • include_examples (bool) – Whether to include example prompts

Return type:

str

Returns:

Path to created project directory

Example

>>> path = create_project_structure('my_ai_app')
>>> print(f"Project created at: {path}")
pyrompt.util.export_to_dict(collection, include_metadata=True)[source]#

Export collection to a dictionary.

Parameters:
  • collection – PromptCollection or TemplateCollection

  • include_metadata (bool) – Whether to include metadata

Return type:

Dict[str, Any]

Returns:

Dict with ‘prompts’ and optionally ‘metadata’ keys

Example

>>> import tempfile
>>> from pyrompt import PromptCollection
>>> prompts = PromptCollection('my_project', base_path=tempfile.mkdtemp())
>>> prompts['system'] = 'You are helpful.'
>>> export_to_dict(prompts, include_metadata=False)
{'prompts': {'system': 'You are helpful.'}}
pyrompt.util.get_stats(collection)[source]#

Get statistics about a collection.

Parameters:

collection – PromptCollection or TemplateCollection

Return type:

Dict[str, Any]

Returns:

Dict with statistics

Example

>>> import tempfile
>>> from pyrompt import PromptCollection
>>> prompts = PromptCollection('my_project', base_path=tempfile.mkdtemp())
>>> get_stats(prompts)['total_prompts']
0
pyrompt.util.import_from_dict(collection, prompts, metadata=None)[source]#

Import prompts from a dictionary.

Parameters:
  • collection – PromptCollection or TemplateCollection

  • prompts (Dict[str, str]) – Dict mapping keys to prompt/template strings

  • metadata (Optional[Dict[str, dict]]) – Optional dict mapping keys to metadata dicts

Example

>>> import tempfile
>>> from pyrompt import PromptCollection
>>> prompts = PromptCollection('my_project', base_path=tempfile.mkdtemp())
>>> import_from_dict(prompts, {'system': 'You are helpful.', 'user': 'Hello!'})
>>> sorted(prompts)
['system', 'user']
pyrompt.util.list_available_engines()[source]#

List all available template engines with their details.

Return type:

Dict[str, Dict[str, Any]]

Returns:

Dict mapping engine name to info dict

Example

>>> engines = list_available_engines()
>>> print(engines['format']['extensions'])
['.txt', '']
pyrompt.util.merge_collections(target, *sources, conflict_strategy='skip')[source]#

Merge multiple collections into a target collection.

Parameters:
  • target – Target collection to merge into

  • *sources – Source collections to merge from

  • conflict_strategy (str) – How to handle conflicts (‘skip’, ‘overwrite’, ‘error’)

Example

>>> import tempfile
>>> from pyrompt import PromptCollection
>>> base = tempfile.mkdtemp()
>>> target = PromptCollection('merged', base_path=base)
>>> source1 = PromptCollection('source1', base_path=base)
>>> source1['a'] = 'A'
>>> merge_collections(target, source1, conflict_strategy='skip')
>>> sorted(target)
['a']
pyrompt.util.quick_setup(project_name, with_templates=True, with_metadata=False, base_path=None)[source]#

Quick setup for a new pyrompt project.

Creates both prompt and template collections with sensible defaults.

Parameters:
  • project_name (str) – Name of the project

  • with_templates (bool) – Whether to create a template collection

  • with_metadata (bool) – Whether to enable metadata

  • base_path (Optional[str]) – Optional base path

Return type:

Dict[str, Any]

Returns:

Dict with ‘prompts’ and optionally ‘templates’ keys

Example

>>> import tempfile
>>> collections = quick_setup('my_ai_project', base_path=tempfile.mkdtemp())
>>> collections['prompts']['system'] = "You are a helpful assistant."
>>> collections['templates']['greeting.txt'] = "Hello {name}!"
>>> sorted(collections)
['prompts', 'templates']
pyrompt.util.render_template_file(file_path, output_path=None, **kwargs)[source]#

Render a template file directly.

Parameters:
  • file_path (str) – Path to template file

  • output_path (Optional[str]) – Optional path to save rendered output

  • **kwargs – Template parameters

Return type:

str

Returns:

Rendered string

Example

>>> result = render_template_file(
...     'templates/greeting.txt',
...     name='Alice'
... )
pyrompt.util.validate_template(template_str, engine_name='format', required_params=None)[source]#

Validate a template string.

Parameters:
  • template_str (str) – Template content

  • engine_name (str) – Engine to use (‘format’, ‘jinja2’, ‘mustache’)

  • required_params (Optional[List[str]]) – Optional list of required parameter names

Return type:

Dict[str, Any]

Returns:

Dict with ‘valid’, ‘errors’, ‘warnings’, ‘placeholders’

Example

>>> result = validate_template("Hello {name}!", required_params=['name'])
>>> assert result['valid']
>>> result = validate_template("Hello {name}!", required_params=['name', 'age'])
>>> assert not result['valid']