Menu

AI & Machine Learning Topic

Practical AI with OpenAI & LLMs (GPT, Claude, Gemini, etc.)

Understanding the LLM Landscape

Major Players & Their APIs

Provider Key Models Best For
OpenAI GPT-4, GPT-3.5-Turbo General purpose, most mature ecosystem
Anthropic Claude-2, Claude-Instant Safety, long contexts, reasoning
Google Gemini Pro, PaLM 2 Google ecosystem integration
Meta Llama 2 (open source) Self-hosted, customizable
Cohere Command, Embed Enterprise, embeddings

Basic API Usage Patterns

1. OpenAI API Example

python

import openai
from openai import OpenAI
import os

# Initialize client (newer SDK style)
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))

def chat_with_gpt(prompt, model="gpt-3.5-turbo"):
    response = client.chat.completions.create(
        model=model,
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt}
        ],
        temperature=0.7,
        max_tokens=500
    )
    return response.choices[0].message.content

# Usage
result = chat_with_gpt("Explain quantum computing in simple terms")
print(result)

2. Anthropic Claude Example

python

import anthropic

client = anthropic.Anthropic(api_key=os.getenv('ANTHROPIC_API_KEY'))

def chat_with_claude(prompt, model="claude-3-sonnet-20240229"):
    response = client.messages.create(
        model=model,
        max_tokens=1000,
        temperature=0,
        system="You are a helpful assistant.",
        messages=[
            {"role": "user", "content": prompt}
        ]
    )
    return response.content[0].text

3. Google Gemini Example

python

import google.generativeai as genai

genai.configure(api_key=os.getenv('GOOGLE_API_KEY'))

def chat_with_gemini(prompt, model="gemini-pro"):
    model = genai.GenerativeModel(model)
    response = model.generate_content(prompt)
    return response.text

Advanced Prompt Engineering Techniques

1. Structured Outputs (New & Powerful!)

Forcing LLMs to return structured data like JSON instead of free text.

OpenAI Function Calling / JSON Mode

python

# Method 1: Function calling (older approach)
def get_structured_data(user_query):
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": user_query}],
        functions=[
            {
                "name": "extract_person_info",
                "description": "Extract person information from text",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "name": {"type": "string"},
                        "age": {"type": "number"},
                        "email": {"type": "string"},
                        "location": {"type": "string"}
                    },
                    "required": ["name", "age"]
                }
            }
        ],
        function_call={"name": "extract_person_info"}
    )
    
    return json.loads(response.choices[0].message.function_call.arguments)

# Method 2: JSON mode (newer, simpler)
def get_json_response(user_query):
    response = client.chat.completions.create(
        model="gpt-3.5-turbo-1106",  # Models that support JSON mode
        messages=[{"role": "user", "content": user_query}],
        response_format={"type": "json_object"},
        temperature=0
    )
    return json.loads(response.choices[0].message.content)

Practical Example: Customer Support Triage

python

def analyze_customer_message(message):
    prompt = """
    Analyze this customer message and return JSON with:
    - urgency: "low", "medium", "high"
    - category: "billing", "technical", "account", "general"
    - sentiment: "positive", "neutral", "negative"
    - key_issues: array of main problems mentioned
    
    Customer message: {message}
    """.format(message=message)
    
    response = client.chat.completions.create(
        model="gpt-3.5-turbo-1106",
        messages=[{"role": "user", "content": prompt}],
        response_format={"type": "json_object"},
        temperature=0
    )
    
    analysis = json.loads(response.choices[0].message.content)
    return analysis

# Usage
message = "My bill is wrong and I've been charged twice! This is the third time this has happened. I'm very frustrated."
result = analyze_customer_message(message)
print(result)
# Output: {"urgency": "high", "category": "billing", "sentiment": "negative", "key_issues": ["double charge", "recurring problem"]}

2. Prompt Chaining (Breaking Complex Tasks into Steps)

python

def multi_step_research(topic):
    # Step 1: Generate research questions
    questions_prompt = f"""
    Generate 3 key research questions about: {topic}
    Return as JSON: {{"questions": ["q1", "q2", "q3"]}}
    """
    
    questions_response = client.chat.completions.create(
        model="gpt-3.5-turbo-1106",
        messages=[{"role": "user", "content": questions_prompt}],
        response_format={"type": "json_object"}
    )
    questions = json.loads(questions_response.choices[0].message.content)["questions"]
    
    # Step 2: Research each question
    research_results = []
    for question in questions:
        research_prompt = f"""
        Provide a comprehensive answer to: {question}
        Include key facts and sources if known.
        """
        
        research_response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "user", "content": research_prompt}],
            temperature=0.3
        )
        research_results.append({
            "question": question,
            "answer": research_response.choices[0].message.content
        })
    
    # Step 3: Synthesize findings
    synthesis_prompt = f"""
    Based on this research: {research_results}
    Create a comprehensive report about: {topic}
    """
    
    final_response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": synthesis_prompt}],
        temperature=0.5
    )
    
    return {
        "research_questions": questions,
        "detailed_research": research_results,
        "final_report": final_response.choices[0].message.content
    }

Building Practical Applications

1. AI-Powered Customer Service Agent

python

class CustomerServiceAgent:
    def __init__(self):
        self.conversation_history = []
    
    def add_to_history(self, role, content):
        self.conversation_history.append({"role": role, "content": content})
    
    def respond_to_customer(self, customer_message):
        # Add customer message to history
        self.add_to_history("user", customer_message)
        
        system_prompt = """
        You are a customer service agent for a tech company.
        Be empathetic, helpful, and concise.
        If you need specific information, ask clarifying questions.
        If the issue is complex, suggest escalating to a human agent.
        """
        
        response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "system", "content": system_prompt},
                *self.conversation_history[-6:]  # Last 3 exchanges for context
            ],
            temperature=0.3
        )
        
        agent_response = response.choices[0].message.content
        self.add_to_history("assistant", agent_response)
        
        return agent_response
    
    def analyze_conversation(self):
        """Analyze the conversation for insights"""
        analysis_prompt = f"""
        Analyze this customer conversation: {self.conversation_history}
        Return JSON with:
        - customer_sentiment: trend over conversation
        - main_issues: array of key problems
        - resolution_status: "resolved", "escalation_needed", "ongoing"
        - agent_effectiveness: rating 1-10
        """
        
        response = client.chat.completions.create(
            model="gpt-3.5-turbo-1106",
            messages=[{"role": "user", "content": analysis_prompt}],
            response_format={"type": "json_object"}
        )
        
        return json.loads(response.choices[0].message.content)

# Usage
agent = CustomerServiceAgent()
response1 = agent.respond_to_customer("My order hasn't arrived yet!")
print(response1)
response2 = agent.respond_to_customer("It was supposed to be here 3 days ago")
print(response2)
analysis = agent.analyze_conversation()
print(analysis)

2. Content Generation Pipeline

python

class ContentCreator:
    def generate_blog_idea(self, topic):
        prompt = f"Generate 5 unique blog post ideas about {topic}"
        response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.8
        )
        return response.choices[0].message.content
    
    def outline_blog_post(self, idea):
        prompt = f"Create a detailed outline for a blog post: {idea}"
        response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.5
        )
        return response.choices[0].message.content
    
    def write_section(self, outline, section):
        prompt = f"""
        Based on this outline: {outline}
        Write the section about: {section}
        Make it engaging and informative, 200-300 words.
        """
        response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.7
        )
        return response.choices[0].message.content
    
    def generate_seo_metadata(self, content):
        prompt = f"""
        For this content: {content[:500]}...
        Generate SEO metadata including:
        - Meta title (60 chars)
        - Meta description (160 chars) 
        - 5 relevant keywords
        Return as JSON.
        """
        response = client.chat.completions.create(
            model="gpt-3.5-turbo-1106",
            messages=[{"role": "user", "content": prompt}],
            response_format={"type": "json_object"},
            temperature=0
        )
        return json.loads(response.choices[0].message.content)

Advanced Patterns & Best Practices

1. Retry Logic & Error Handling

python

import time
from tenacity import retry, stop_after_attempt, wait_exponential

@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
def robust_llm_call(prompt, model="gpt-3.5-turbo"):
    try:
        response = client.chat.completions.create(
            model=model,
            messages=[{"role": "user", "content": prompt}],
            temperature=0.7,
            timeout=30  # 30 second timeout
        )
        return response.choices[0].message.content
    except openai.APITimeoutError:
        print("Request timed out, retrying...")
        raise
    except openai.RateLimitError:
        print("Rate limit hit, waiting...")
        time.sleep(60)
        raise
    except Exception as e:
        print(f"Unexpected error: {e}")
        raise

2. Cost Management

python

class CostAwareLLM:
    def __init__(self):
        self.token_usage = {
            "gpt-3.5-turbo": 0,
            "gpt-4": 0
        }
        # Rough cost per 1K tokens (check latest prices)
        self.costs = {
            "gpt-3.5-turbo": 0.002,
            "gpt-4": 0.03
        }
    
    def call_with_cost_tracking(self, prompt, model="gpt-3.5-turbo"):
        response = client.chat.completions.create(
            model=model,
            messages=[{"role": "user", "content": prompt}],
            temperature=0.7
        )
        
        # Track token usage
        tokens_used = response.usage.total_tokens
        self.token_usage[model] += tokens_used
        
        cost = (tokens_used / 1000) * self.costs[model]
        print(f"Used {tokens_used} tokens, cost: ${cost:.4f}")
        
        return response.choices[0].message.content
    
    def get_total_cost(self):
        total = 0
        for model, tokens in self.token_usage.items():
            cost = (tokens / 1000) * self.costs[model]
            total += cost
        return total

3. Template System for Reusable Prompts

python

from string import Template

class PromptTemplates:
    BLOG_POST = Template("""
    Write a blog post about $topic for $audience.
    Tone: $tone
    Length: $length words
    Include: $key_points
    """)
    
    EMAIL_RESPONSE = Template("""
    Write an email response to: $customer_query
    Company: $company_name  
    Key points to include: $key_points
    Tone: $tone
    """)
    
    @classmethod
    def fill_template(cls, template_name, **kwargs):
        template = getattr(cls, template_name)
        return template.substitute(**kwargs)

# Usage
prompt = PromptTemplates.fill_template(
    "BLOG_POST",
    topic="machine learning",
    audience="beginners",
    tone="friendly and educational",
    length="800",
    key_points="what is ML, common algorithms, real-world examples"
)

Integration Patterns

1. FastAPI + LLM Integration

python

from fastapi import FastAPI
from pydantic import BaseModel
import openai

app = FastAPI()

class ChatRequest(BaseModel):
    message: str
    temperature: float = 0.7
    model: str = "gpt-3.5-turbo"

class AnalysisRequest(BaseModel):
    text: str
    analysis_type: str  # "sentiment", "summary", "keywords"

@app.post("/chat")
async def chat_endpoint(request: ChatRequest):
    response = client.chat.completions.create(
        model=request.model,
        messages=[{"role": "user", "content": request.message}],
        temperature=request.temperature
    )
    return {"response": response.choices[0].message.content}

@app.post("/analyze")
async def analyze_text(request: AnalysisRequest):
    prompt = f"""
    Perform {request.analysis_type} analysis on this text: {request.text}
    Return JSON format.
    """
    
    response = client.chat.completions.create(
        model="gpt-3.5-turbo-1106",
        messages=[{"role": "user", "content": prompt}],
        response_format={"type": "json_object"}
    )
    
    return json.loads(response.choices[0].message.content)

Key Takeaways

  1. API-First Approach: Leverage powerful models without infrastructure costs

  2. Structured Outputs: Essential for building reliable applications

  3. Prompt Chaining: Break complex tasks into manageable steps

  4. Cost Management: Track usage and optimize for cost-effectiveness

  5. Error Handling: Build robust applications with retry logic

  6. Templating: Create reusable prompt patterns

  7. Context Management: Maintain conversation history effectively

This approach allows you to build sophisticated AI applications rapidly by composing LLM capabilities with your business logic and existing systems!

 

Comments (0)

No comments yet. Be the first to comment!

AlphaAI Assistant

Hello! I'm AlphaAI

What would you like to learn today?