Building with LLMs
Building AI Applications
Build real-world AI applications: chatbots, content generators, and document analyzers.
Common AI Application Patterns
1. Chatbots
Maintain conversation history and respond contextually.
2. Document Q&A
Load documents, chunk them, and answer questions based on their content.
3. Content Generation Pipelines
Automated workflows that generate, review, and refine content.
4. Classification and Extraction
Extract structured data from unstructured text.
5. Code Generation and Review
Generate, explain, or review code.
Production Considerations
- Rate limiting: Respect API rate limits, implement retries
- Cost management: Track token usage, optimize prompts
- Latency: Use streaming for better UX, consider smaller models
- Safety: Add content filters, validate outputs
- Evaluation: Test prompts systematically, use evals
Example
python
import anthropic
from typing import Optional
client = anthropic.Anthropic()
# 1. Conversational chatbot with memory
class Chatbot:
def __init__(self, system_prompt: str, model: str = "claude-3-5-haiku-20241022"):
self.system = system_prompt
self.model = model
self.history = []
def chat(self, message: str) -> str:
self.history.append({"role": "user", "content": message})
response = client.messages.create(
model=self.model,
max_tokens=1000,
system=self.system,
messages=self.history
)
assistant_reply = response.content[0].text
self.history.append({"role": "assistant", "content": assistant_reply})
return assistant_reply
def reset(self):
self.history = []
# 2. Information extractor
def extract_entities(text: str) -> dict:
prompt = f"""Extract entities from this text. Return valid JSON with:
- people: list of person names
- organizations: list of organization names
- locations: list of location names
- dates: list of dates/times mentioned
Text: {text}
Return ONLY valid JSON, no other text."""
response = client.messages.create(
model="claude-3-5-haiku-20241022",
max_tokens=500,
messages=[{"role": "user", "content": prompt}]
)
import json
try:
return json.loads(response.content[0].text)
except json.JSONDecodeError:
return {}
# 3. Document summarizer with different styles
def summarize_document(doc: str, style: str = "executive") -> str:
styles = {
"executive": "Write a 2-sentence executive summary focusing on key decisions and outcomes.",
"technical": "Provide a technical summary highlighting methods, tools, and implementation details.",
"simple": "Explain in simple terms a 10-year-old could understand."
}
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=500,
system=styles.get(style, styles["executive"]),
messages=[{"role": "user", "content": f"Summarize this:\n\n{doc}"}]
)
return response.content[0].text
# Usage example
bot = Chatbot("You are a helpful coding assistant who answers concisely.")
print(bot.chat("What is a closure in JavaScript?"))
print(bot.chat("Can you give me an example?"))Try it yourself — PYTHON