* Export pipeline to config * Export the input to config * Preliminary creating UI dynamically * Add test for config export * Try out prompt UI * Add example projects * Fix test errors * Standardize interface for retrieval * Finalize the UI demo * Update README.md * Update README * Refactor according to main * Fix typing issue * Add openai key to git-secret * Add prompt template and prompt component * Update test * update tests * revert docstring --------- Co-authored-by: trducng <trungduc1992@gmail.com> Co-authored-by: Nguyen Trung Duc (john) <john@cinnamon.is>
69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
import re
|
|
from typing import Set
|
|
|
|
|
|
class PromptTemplate:
|
|
"""
|
|
Base class for prompt templates.
|
|
"""
|
|
|
|
@staticmethod
|
|
def extract_placeholders(template: str) -> Set[str]:
|
|
"""
|
|
Extracts placeholders from a template string.
|
|
|
|
Args:
|
|
template (str): The template string to extract placeholders from.
|
|
|
|
Returns:
|
|
set[str]: A set of placeholder names found in the template string.
|
|
"""
|
|
placeholder_regex = r"{([a-zA-Z_][a-zA-Z0-9_]*)}"
|
|
|
|
placeholders = set()
|
|
for item in re.findall(placeholder_regex, template):
|
|
if item.isidentifier():
|
|
placeholders.add(item)
|
|
|
|
return placeholders
|
|
|
|
def __init__(self, template: str):
|
|
self.placeholders = self.extract_placeholders(template)
|
|
self.template = template
|
|
|
|
def populate(self, **kwargs):
|
|
"""
|
|
Populate the template with the given keyword arguments.
|
|
|
|
Args:
|
|
**kwargs: The keyword arguments to populate the template.
|
|
Each keyword corresponds to a placeholder in the template.
|
|
|
|
Returns:
|
|
str: The populated template.
|
|
|
|
Raises:
|
|
ValueError: If an unknown placeholder is provided.
|
|
|
|
"""
|
|
prompt = self.template
|
|
for placeholder, value in kwargs.items():
|
|
if placeholder not in self.placeholders:
|
|
raise ValueError(f"Unknown placeholder: {placeholder}")
|
|
prompt = prompt.replace(f"{{{placeholder}}}", value)
|
|
|
|
return prompt
|
|
|
|
def __add__(self, other):
|
|
"""
|
|
Create a new PromptTemplate object by concatenating the template of the current
|
|
object with the template of another PromptTemplate object.
|
|
|
|
Parameters:
|
|
other (PromptTemplate): Another PromptTemplate object.
|
|
|
|
Returns:
|
|
PromptTemplate: A new PromptTemplate object with the concatenated templates.
|
|
"""
|
|
return PromptTemplate(self.template + "\n" + other.template)
|