Build prompts with templates.
"""Build prompts with templates.
This example shows:
- Creating reusable prompt templates
- Using loops and conditionals in templates
- Few-shot learning patterns
"""
import talu
prompt = talu.PromptTemplate("""
You are a {{ role }} assistant.
User: {{ question }}
Assistant:""")
print(prompt(role="helpful", question="What is Python?"))
print(prompt(role="concise", question="Explain recursion"))
classifier = talu.PromptTemplate("""
Classify the sentiment:
{% for ex in examples %}
Text: {{ ex.text }}
Sentiment: {{ ex.label }}
{% endfor %}
Text: {{ query }}
Sentiment:""")
print(classifier(
examples=[
{"text": "I love this!", "label": "positive"},
{"text": "Terrible experience", "label": "negative"},
],
query="This exceeded expectations!",
))
list_prompt = talu.PromptTemplate("""
Summarize these items:
{% for item in items %}
- {{ item }}
{% endfor %}
Summary:""")
print(list_prompt(items=["apples", "bananas", "pears"]))
conditional = talu.PromptTemplate("""
{% if tone == "formal" %}
You are a formal assistant.
{% else %}
You are a casual assistant.
{% endif %}
User: {{ question }}
Assistant:""")
print(conditional(tone="casual", question="Explain gravity in one sentence."))
rendered = prompt(role="helpful", question="Explain recursion in one sentence.")
with open("/tmp/talu_04_prompt_templates_rendered.txt", "w") as f:
f.write(rendered)
print("Saved prompt to /tmp/talu_04_prompt_templates_rendered.txt")