pyrompt.util#
Utility functions and convenience helpers for pyrompt.
Functions
|
Quick setup for a new pyrompt project. |
|
Import prompts from a dictionary. |
|
Export collection to a dictionary. |
|
Validate a template string. |
|
Merge multiple collections into a target collection. |
List all available template engines with their details. |
|
|
Render a template file directly. |
|
Create a complete project structure for prompt management. |
|
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:
- Return type:
- 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:
- 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:
- 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:
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.
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:
- Return type:
- 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:
- Return type:
- 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:
- Return type:
- 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']