Text generation mastery
Now let's combine everything into real applications. Here are three patterns you'll use constantly: customer support bots, content writers, and code explainers.
Multi-turn conversation
How conversation history grows with each exchange
It depends on the use case. For anything that needs to be accurate and repeatable, like customer support or data extraction, use low temperature (0.2-0.3) with strict system instructions. For creative tasks like writing marketing copy, use high temperature (0.7-0.9) with looser instructions. Let us see both patterns in action.
# Customer Support Bot: low temp, strict personality
support_system = """You are a support bot for 'Green Bites' restaurant.
Hours: Mon-Fri 11am-9pm, Sat-Sun 10am-10pm
Location: 123 Main St, San Francisco
We are 100% plant-based. Be helpful and friendly."""
questions = ["What are your hours?", "Do you have vegan options?"]
for q in questions:
response = generate_text(q,
system_message=support_system,
temperature=0.2 # Consistent answers!
)
print(f"Q: {q}\nA: {response}\n")Low temperature + detailed system instructions = reliable support bot. Answers are consistent every time.
Next, let us flip the approach. Instead of consistency, we want creative variety. A content writer bot uses high temperature to produce different captions each time.
# Content Writer: high temp for creative variety
writer_system = "You are a creative social media copywriter. Write short, catchy captions. Keep it under 200 words."
request = "Write an Instagram caption for our new Tropical Paradise smoothie bowl (mango, pineapple, coconut)"
# Generate 3 different options
for i in range(3):
caption = generate_text(request,
system_message=writer_system,
temperature=0.8 # Creative variety!
)
print(f"Option {i+1}: {caption}\n")High temperature + creative system instructions = diverse content options. Each run produces unique copy.
Finally, a code explainer. This pattern uses low temperature again because explanations need to be accurate and consistent, similar to customer support.
# Code Explainer: low temp for accurate explanations
explainer_system = "You are a patient coding tutor. Explain code in simple terms step-by-step."
code_to_explain = """
def calculate_discount(price, discount_percent):
discount_amount = price * (discount_percent / 100)
return price - discount_amount
"""
explanation = generate_text(
f"Explain this function:\n{code_to_explain}",
system_message=explainer_system,
temperature=0.3
)
print(explanation)Code explanation needs accuracy, so we use low temperature. The system instruction sets a patient, beginner-friendly tone.
You can change the system instruction between API calls. Each call is independent, so you could use a strict support personality for answering questions and switch to a creative personality for upselling. In more advanced setups, you would use routing logic to pick the right personality based on what the user is asking.
Ordering exercise: Steps to build a chatbot
Loading practice…
Fill in the blanks: Complete the generate_text() function
Loading practice…
Timed quiz: Text generation speed round
Loading practice…
Validation checklist: Text generation checklist
Loading practice…