AI Integration Specialist
Expert guidance for LLM integration, RAG systems, and WhatsApp Business API workflows for intelligent marketing automation.
Quick Start
LLM Integration
Basic Claude API Call
import anthropic
import os
client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[
{"role": "user", "content": "Generate a LinkedIn post about AI in marketing"}
]
)
print(message.content[0].text)
Using Prompt Templates
import json
# Load template
with open("assets/prompt-templates/content-generation.json") as f:
template = json.load(f)
# Fill template
system_prompt = template["system_prompt"].replace("{{platform}}", "linkedin")
user_prompt = template["user_prompt"].replace("{{topic}}", "AI in marketing")
# Call LLM
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
system=system_prompt,
messages=[{"role": "user", "content": user_prompt}]
)
RAG System Setup
Quick ChromaDB Setup
import chromadb
from sentence_transformers import SentenceTransformer
# Initialize
client = chromadb.Client()
collection = client.create_collection("marketing_knowledge")
embedder = SentenceTransformer("all-MiniLM-L6-v2")
# Add documents
documents = ["AI improves marketing ROI by 40%", "Social media engagement peaks at noon"]
embeddings = embedder.encode(documents).tolist()
collection.add(
documents=documents,
embeddings=embeddings,
ids=["doc1", "doc2"]
)
# Query
query = "How does AI help marketing?"
query_embedding = embedder.encode([query]).tolist()
results = collection.query(query_embeddings=query_embedding, n_results=2)
WhatsApp Approval Workflow
Send Approval Request
from assets.whatsapp_templates import approval_messages
import json
# Load template
with open("assets/whatsapp-templates/approval-messages.json") as f:
templates = json.load(f)
# Fill template
message = templates["approval_request"]
message["interactive"]["body"]["text"] = message["interactive"]["body"]["text"].replace(
"{{platform}}", "LinkedIn"
).replace("{{request_id}}", "req_123").replace("{{content}}", "Your post content here")
# Send via WhatsApp API
await whatsapp_client.send_message(to="+1234567890", message=message)
LLM Prompting
For comprehensive prompting patterns, best practices, and multi-agent orchestration, see llm-prompting.md.
System Prompt Design
system_prompt = """You are an expert social media content creator.
Your role:
1. Generate engaging, platform-appropriate content
2. Optimize for target audience engagement
3. Maintain brand voice consistency
4. Include clear calls-to-action
Guidelines:
- Be concise and impactful
- Use data-driven insights
- Follow platform best practices
- Prioritize authenticity
Output Format: JSON with content, hashtags, and CTA"""
Few-Shot Learning
few_shot_prompt = """Generate social media posts in this style:
Example 1:
Input: Product launch - AI writing tool
Output: "🚀 Excited to announce our new AI writing assistant!
Transform your content creation workflow with intelligent suggestions.
Try it free: [link] #AIWriting #ContentCreation"
Example 2:
Input: Company milestone - 10k users
Output: "🎉 We just hit 10,000 users! Thank you to our amazing community.
What feature should we build next? 👇 #Milestone #Startup"
Now generate:
Input: {your_input}
Output:"""
Multi-Agent Orchestration
# Orchestrator decides which agents to use
orchestrator_prompt = """Analyze this request and determine which specialist agents to invoke:
Request: {user_request}
Available Agents:
- ContentCreator: Generates social media posts
- Analyzer: Analyzes performance metrics
- Validator: Reviews content quality
Provide execution plan in JSON:
{
"agents": ["agent1", "agent2"],
"execution_mode": "sequential|parallel",
"reasoning": "why these agents"
}"""
RAG Systems
For complete RAG implementation patterns, vector database integration, and retrieval strategies, see rag-systems.md.
Document Processing
def chunk_text_semantic(text: str, max_chunk_size: int = 512) -> list[str]:
"""Split text at semantic boundaries"""
import re
paragraphs = text.split('\n\n')
chunks = []
current_chunk = ""
for para in paragraphs:
sentences = re.split(r'(?<=[.!?])\s+', para)
for sentence in sentences:
if len(current_chunk) + len(sentence) <= max_chunk_size:
current_chunk += sentence + " "
else:
if current_chunk:
chunks.append(current_chunk.strip())
current_chunk = sentence + " "
if current_chunk:
chunks.append(current_chunk.strip())
return chunks
Embedding Generation
from sentence_transformers import SentenceTransformer
embedder = SentenceTransformer("all-MiniLM-L6-v2")
embeddings = embedder.encode(["text1", "text2"])
RAG Pipeline
class RAGSystem:
def __init__(self, vector_store, llm_client, embedder):
self.vector_store = vector_store
self.llm = llm_client
self.embedder = embedder
def generate(self, query: str, top_k: int = 5):
# 1. Retrieve relevant documents
query_embedding = self.embedder.encode([query])[0]
docs = self.vector_store.query(query_embedding, top_k=top_k)
# 2. Construct context
context = "\n\n".join([doc.text for doc in docs])
# 3. Generate with context
prompt = f"Context:\n{context}\n\nQuestion: {query}\n\nAnswer:"
return self.llm.generate(prompt)
Feedback Loop Integration
def update_knowledge_from_feedback(post_id: str, metrics: dict):
"""Update RAG knowledge base with performance data"""
# Create learning document
doc = f"""
Platform: {metrics['platform']}
Content: {metrics['content']}
Engagement Rate: {metrics['engagement_rate']:.2%}
Success Factors: Posted at {metrics['time']}, {metrics['content_type']} content
"""
# Add to vector store with performance metadata
vector_store.add(
documents=[doc],
metadatas=[{"performance_score": metrics['engagement_rate']}]
)
WhatsApp Business API
For complete WhatsApp integration patterns, webhook handling, and approval workflows, see whatsapp-api.md.
Client Setup
import os
import httpx
class WhatsAppClient:
def __init__(self):
self.api_url = os.getenv("WHATSAPP_API_URL")
self.phone_number_id = os.getenv("WHATSAPP_PHONE_NUMBER_ID")
self.access_token = os.getenv("WHATSAPP_ACCESS_TOKEN")
async def send_message(self, to: str, message: dict):
async with httpx.AsyncClient() as client:
response = await client.post(
f"{self.api_url}/{self.phone_number_id}/messages",
headers={"Authorization": f"Bearer {self.access_token}"},
json={"messaging_product": "whatsapp", "to": to, **message}
)
return response.json()
Interactive Button Messages
async def send_approval_request(client, to: str, content: str, request_id: str):
message = {
"type": "interactive",
"interactive": {
"type": "button",
"body": {"text": f"Approve this content?\n\n{content}"},
"action": {
"buttons": [
{"type": "reply", "reply": {"id": f"approve_{request_id}", "title": "✅ Approve"}},
{"type": "reply", "reply": {"id": f"reject_{request_id}", "title": "❌ Reject"}}
]
}
}
}
return await client.send_message(to, message)
Webhook Processing
from fastapi import APIRouter, Request
router = APIRouter()
@router.post("/webhooks/whatsapp")
async def receive_webhook(request: Request):
body = await request.json()
for entry in body.get("entry", []):
for change in entry.get("changes", []):
if change.get("field") == "messages":
messages = change["value"].get("messages", [])
for message in messages:
if message["type"] == "interactive":
await process_approval_response(message)
return {"status": "ok"}
Prompt Templates
Content Generation Template
Located at assets/prompt-templates/content-generation.json:
import json
with open("assets/prompt-templates/content-generation.json") as f:
template = json.load(f)
# Access components
system_prompt = template["system_prompt"]
user_prompt = template["user_prompt"]
examples = template["few_shot_examples"]
Custom Template Creation
def create_custom_prompt(
platform: str,
topic: str,
target_audience: str,
tone: str
) -> str:
return f"""Generate a {platform} post about {topic}.
Target Audience: {target_audience}
Tone: {tone}
Requirements:
- Platform-appropriate length
- Engaging hook
- Clear call-to-action
- 2-3 relevant hashtags
Output as JSON with: content, hashtags, cta"""
WhatsApp Message Templates
Using Pre-built Templates
import json
with open("assets/whatsapp-templates/approval-messages.json") as f:
templates = json.load(f)
# Get specific template
approval_template = templates["approval_request"]
confirmation_template = templates["approval_confirmation"]
Template Variables
Replace placeholders in templates:
def fill_template(template: dict, variables: dict) -> dict:
"""Replace template variables"""
import json
template_str = json.dumps(template)
for key, value in variables.items():
template_str = template_str.replace(f"{{{{{key}}}}}", str(value))
return json.loads(template_str)
Error Handling
LLM API Retries
from tenacity import retry, stop_after_attempt, wait_exponential
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
async def call_llm_with_retry(prompt: str):
try:
return await llm_client.generate(prompt)
except Exception as e:
print(f"LLM call failed: {e}")
raise
Fallback Strategy
async def call_with_fallback(prompt: str):
"""Try Claude, fallback to GPT"""
try:
return await call_claude(prompt)
except Exception:
return await call_gpt(prompt)
Best Practices
LLM Integration
- Clear Instructions: Be explicit about what you want
- Structured Output: Request JSON for consistent parsing
- Few-Shot Examples: Show the model what you want
- Error Handling: Implement retries and fallbacks
- Cost Optimization: Cache responses, optimize token usage
RAG Systems
- Semantic Chunking: Preserve context in chunks
- Rich Metadata: Include performance data for filtering
- Hybrid Search: Combine dense and sparse retrieval
- Continuous Learning: Update knowledge base with feedback
- Evaluation: Measure retrieval quality regularly
WhatsApp Integration
- Template Pre-approval: Get templates approved by Meta
- Session Management: Use TTL for approval requests
- Idempotency: Handle duplicate webhook deliveries
- Rate Limiting: Implement retry logic
- User Experience: Use emojis and clear formatting
Integration Patterns
Complete Approval Workflow
async def approval_workflow(content: str, platform: str, approver_phone: str):
# 1. Generate content with LLM
generated = await llm_client.generate(f"Create {platform} post: {content}")
# 2. Send approval request via WhatsApp
request_id = f"req_{uuid.uuid4()}"
await send_approval_request(whatsapp_client, approver_phone, generated, request_id)
# 3. Wait for response (handled by webhook)
# 4. On approval, publish and track
# 5. Update RAG knowledge base with performance
RAG-Enhanced Content Generation
async def generate_with_rag(topic: str):
# 1. Retrieve relevant past successful posts
similar_posts = rag_system.retrieve(topic, top_k=3)
# 2. Build context-aware prompt
context = "\n".join([post.text for post in similar_posts])
prompt = f"Based on these successful posts:\n{context}\n\nGenerate new post about: {topic}"
# 3. Generate with LLM
return await llm_client.generate(prompt)
Monitoring
Track LLM Usage
class LLMMetrics:
async def track_call(self, model: str, tokens: int, latency: float):
await self.increment_counter(f"llm.calls.{model}")
await self.record_histogram(f"llm.tokens.{model}", tokens)
await self.record_histogram(f"llm.latency.{model}", latency)
Track Approval Workflow
class ApprovalMetrics:
async def track_request(self, platform: str):
await self.increment_counter(f"approvals.sent.{platform}")
async def track_response(self, action: str, response_time: float):
await self.increment_counter(f"approvals.{action}")
await self.record_histogram("approvals.response_time", response_time)
Reference Documentation
- llm-prompting.md - Comprehensive LLM prompting patterns, few-shot learning, multi-agent orchestration, and optimization techniques
- rag-systems.md - Complete RAG implementation with vector databases, chunking strategies, retrieval patterns, and feedback loops
- whatsapp-api.md - WhatsApp Business API integration, approval workflows, interactive messages, and webhook handling
When NOT to Use This Skill
- Simple single-model chatbots without RAG — if you just need a basic API call to Claude or GPT, the full integration patterns here are overkill
- Real-time streaming applications with strict latency requirements — RAG retrieval adds latency; profile first before adding vector search to the hot path
- Environments where WhatsApp Business API access is unavailable — the approval workflow patterns require verified WhatsApp Business accounts
Common Mistakes
- Chunking documents without overlap — adjacent chunks lose contextual continuity and degrade retrieval quality
- Not implementing retry logic with exponential backoff for LLM API calls — transient rate-limit errors cause unnecessary failures
- Sending unapproved WhatsApp template messages — Meta's policy requires pre-approval for template messages; using custom messages without templates triggers account suspension