Skip to content

LLM APIs Quick Start

OpenAI, Anthropic, DeepSeek — A Practical Guide to Calling Large Models


Why Should Social Science Students Learn LLM APIs?

Use Cases:

  • Text data analysis (news, social media, interviews)
  • Content classification and coding
  • Literature summarization and reviews
  • Survey design assistance
  • Data cleaning and annotation

OpenAI API

Installation and Configuration

bash
pip install openai
python
from openai import OpenAI

# Set API Key
client = OpenAI(api_key='your-api-key-here')

# Basic call
response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {"role": "system", "content": "You are a data analysis assistant"},
        {"role": "user", "content": "Explain what regression analysis is"}
    ]
)

print(response.choices[0].message.content)

Practical Examples

Case 1: Text Sentiment Analysis

python
def analyze_sentiment(text):
    """Use GPT for sentiment analysis"""
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a sentiment analysis expert. Answer: positive, negative, or neutral"},
            {"role": "user", "content": f"Analyze the sentiment of this text: {text}"}
        ]
    )
    return response.choices[0].message.content

# Batch analysis
reviews = [
    "This product is amazing!",
    "Poor quality, waste of money",
    "Mediocre, barely usable"
]

for review in reviews:
    sentiment = analyze_sentiment(review)
    print(f"{review}{sentiment}")

Case 2: Structured Data Extraction

python
import json

def extract_info(text):
    """Extract information from unstructured text"""
    prompt = f"""
    Extract information from the following text, return in JSON format:
    - name: Name
    - age: Age
    - occupation: Occupation
    - income_range: Income range

    Text: {text}
    """

    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}],
        response_format={"type": "json_object"}
    )

    return json.loads(response.choices[0].message.content)

text = "My name is John Smith, I'm 35 years old, work as a software engineer, and earn between $100k-150k annually"
info = extract_info(text)
print(info)

Case 3: Batch Text Classification

python
categories = ["Economy", "Politics", "Society", "Culture", "Technology"]

def classify_text(text, categories):
    prompt = f"Classify the following text into one of these categories: {', '.join(categories)}\n\nText: {text}\n\nReturn only the category name."

    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}]
    )

    return response.choices[0].message.content.strip()

# Batch news classification
news = [
    "Federal Reserve cuts interest rates by 0.25 percentage points",
    "New technology drives AI industry development",
    "Community launches volunteer service activities"
]

for article in news:
    category = classify_text(article, categories)
    print(f"{article[:40]}... → {category}")

Anthropic Claude API

python
import anthropic

client = anthropic.Anthropic(api_key='your-api-key')

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Explain what causal inference is"}
    ]
)

print(response.content[0].text)

DeepSeek API (Chinese, Affordable)

python
from openai import OpenAI  # DeepSeek is compatible with OpenAI format

client = OpenAI(
    api_key='your-deepseek-key',
    base_url='https://api.deepseek.com'
)

response = client.chat.completions.create(
    model="deepseek-chat",
    messages=[
        {"role": "user", "content": "Explain statistical significance"}
    ]
)

print(response.choices[0].message.content)

Best Practices

1. Cost Control

python
# Use cheaper models for simple tasks
def smart_call(text, task_complexity='simple'):
    if task_complexity == 'simple':
        model = 'gpt-3.5-turbo'  # Affordable
    else:
        model = 'gpt-4'  # Expensive but powerful

    response = client.chat.completions.create(
        model=model,
        messages=[{"role": "user", "content": text}],
        max_tokens=500  # Limit output length
    )
    return response.choices[0].message.content

2. Error Handling

python
import time

def safe_api_call(prompt, max_retries=3):
    for i in range(max_retries):
        try:
            response = client.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[{"role": "user", "content": prompt}]
            )
            return response.choices[0].message.content
        except Exception as e:
            print(f"Attempt {i+1} failed: {e}")
            time.sleep(2 ** i)
    return None

3. Batch Processing

python
import time

def batch_process(texts, process_func, delay=1):
    """Batch process texts (with rate limiting)"""
    results = []
    for i, text in enumerate(texts):
        print(f"Processing {i+1}/{len(texts)}")
        result = process_func(text)
        results.append(result)
        time.sleep(delay)  # Avoid rate limits
    return results

Social Science Research Applications

Application 1: Interview Data Coding

python
def code_interview(text, codebook):
    """Code interviews based on codebook"""
    prompt = f"""
    Codebook: {codebook}

    Interview content: {text}

    Please code this interview according to the codebook, return applicable codes.
    """

    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}]
    )

    return response.choices[0].message.content

Application 2: Literature Summary Generation

python
def summarize_paper(abstract):
    """Generate paper summary"""
    prompt = f"""
    Please summarize the following paper abstract in 3 sentences, including:
    1. Research question
    2. Main method
    3. Core findings

    Abstract: {abstract}
    """

    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[{"role": "user", "content": prompt}]
    )

    return response.choices[0].message.content

Practice Exercises

python
# Try using LLM APIs to complete:
# 1. Sentiment analysis of 50 tweets
# 2. Extract key information from news headlines
# 3. Generate survey question suggestions

Module 10 Complete!

Congratulations! You have mastered:

  • Scikit-learn machine learning
  • PyTorch/TensorFlow deep learning basics
  • Practical LLM API usage

Final Module: Module 11 Best Practices

Final sprint!

Released under the MIT License. Content © Author.