| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import string |
| from typing import Set |
|
|
|
|
| class PromptTemplate: |
| """A prompt template for creating prompts with variables. |
| |
| The `PromptTemplate` class allows users to define a template string with |
| variables represented in curly braces `{variable}`. The variable |
| names cannot contain spaces. These variables can be replaced with specific |
| values using the `assemble` method, providing flexibility in generating |
| dynamic prompts. |
| |
| Usage: |
| |
| ``` |
| template_str = "Hello, {name}! Today is {day}. How are you?" |
| prompt_template = PromptTemplate(template_str) |
| completed_prompt = prompt_template.assemble(name="John", day="Monday") |
| print(completed_prompt) |
| ``` |
| """ |
|
|
| def __init__(self, template: str): |
| """Initializes the PromptTemplate with a given template. |
| |
| Args: |
| template: The template string with variables. Variables should be |
| represented in curly braces `{variable}`. |
| """ |
| self.template = str(template) |
| self.variables = self._get_variables() |
|
|
| def _get_variables(self) -> Set[str]: |
| """Extracts and return a set of variable names from the template.""" |
| return set( |
| field_name |
| for _, field_name, _, _ in string.Formatter().parse(self.template) |
| if field_name is not None |
| ) |
|
|
| def assemble(self, **kwargs) -> "PromptTemplate": |
| """Replaces only the provided variables in the template with specific values. |
| |
| Args: |
| **kwargs: Keyword arguments where keys are placeholder names and values |
| are the replacements. |
| |
| Returns: |
| A new PromptTemplate instance with the updated template string. |
| """ |
| replaced_values = { |
| key: kwargs.get(key, "{" + key + "}") for key in self.variables |
| } |
| new_template = self.template.format(**replaced_values) |
| return PromptTemplate(new_template) |
|
|
| def __str__(self) -> str: |
| """Returns the template string.""" |
| return self.template |
|
|
| def __repr__(self) -> str: |
| """Returns a string representation of the PromptTemplate.""" |
| return f"PromptTemplate('{self.template}')" |
|
|