prompt_builder¶
Class prompt builder for AdalFlow system prompt.
Classes
|
Renders a text string(prompt) from a Jinja2 template string. |
- class Prompt(template: str | None = None, prompt_kwargs: Dict[str, Any | Parameter] | None = {})[source]¶
Bases:
DataComponent
Renders a text string(prompt) from a Jinja2 template string.
In default, we use the DEFAULT_ADALFLOW_SYSTEM_PROMPT as the template.
- Parameters:
template (str, optional) – The Jinja2 template string. Defaults to DEFAULT_ADALFLOW_SYSTEM_PROMPT.
preset_prompt_kwargs (Optional[Dict], optional) – The preset prompt kwargs to fill in the variables in the prompt. Defaults to {}.
Examples
>>> from core.prompt_builder import Prompt >>> prompt = Prompt(prompt_kwargs={"task_desc_str": "You are a helpful assistant."}) >>> print(prompt) >>> prompt.print_prompt_template() >>> prompt.print_prompt(context_str="This is a context string.") >>> prompt.call(context_str="This is a context string.")
When examples_str itself is another template with variables, You can use another Prompt to render it.
>>> EXAMPLES_TEMPLATE = r''' >>> {% if examples %} >>> {% for example in examples %} >>> {{loop.index}}. {{example}} >>> {% endfor %} >>> {% endif %} >>> ''' >>> examples_prompt = Prompt(template=EXAMPLES_TEMPLATE) >>> examples_str = examples_prompt.call(examples=["Example 1", "Example 2"]) >>> # pass it to the main prompt >>> prompt.print_prompt(examples_str=examples_str)
- update_prompt_kwargs(**kwargs)[source]¶
Update the initial prompt kwargs after Prompt is initialized.
- compose_prompt_kwargs(**kwargs) Dict [source]¶
Compose the final prompt kwargs by combining the initial and the provided kwargs at runtime.
- print_prompt(**kwargs) str [source]¶
Print the rendered prompt string using the preset_prompt_kwargs and the provided kwargs.