OpenAI / LLM APIs

API exploration for large language models - both cloud (OpenAI) and local (Ollama).

OpenAI API

Base URL

api.openai.com/v1/

Auth

Bearer token (API key)

Format

JSON

Models

gpt-4, gpt-4-turbo, gpt-3.5-turbo

Chat Completion

curl -s "https://api.openai.com/v1/chat/completions" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4",
    "messages": [
      {"role": "system", "content": "You are a network engineer."},
      {"role": "user", "content": "Explain 802.1X EAP-TLS flow."}
    ],
    "temperature": 0.7
  }' | jq '.choices[0].message.content'

List Models

curl -s "https://api.openai.com/v1/models" \
  -H "Authorization: Bearer $OPENAI_API_KEY" | jq '.data[].id'

Ollama API (Local)

Base URL

localhost:11434/api/

Auth

None (local only)

Format

JSON (streaming NDJSON)

Models

qwen2.5-coder, llama3, mistral, etc.

Generate (Non-Streaming)

curl -s "http://localhost:11434/api/generate" \
  -d '{
    "model": "qwen2.5-coder:32b",
    "prompt": "Write a bash function to check certificate expiry",
    "stream": false
  }' | jq -r '.response'

Chat

curl -s "http://localhost:11434/api/chat" \
  -d '{
    "model": "qwen2.5-coder:32b",
    "messages": [
      {"role": "user", "content": "Generate a D2 diagram for Vault PKI"}
    ],
    "stream": false
  }' | jq -r '.message.content'

List Local Models

curl -s "http://localhost:11434/api/tags" | jq '.models[].name'

Model Info

curl -s "http://localhost:11434/api/show" \
  -d '{"name": "qwen2.5-coder:32b"}' | jq

Custom Modelfiles

Location: ~/.ollama/Modelfiles/

Heredoc Generator

# Custom heredoc modelfile
cat > ~/.ollama/Modelfiles/heredoc.modelfile << 'EOF'
FROM qwen2.5-coder:32b
PARAMETER num_ctx 32768
PARAMETER num_predict 8192
PARAMETER temperature 0.1

SYSTEM """You generate ONLY heredoc blocks ready to paste into a terminal.
Format: cat > /path << 'EOF' ... EOF
No explanations, no markdown fences.
"""
EOF

# Create model
ollama create heredoc -f ~/.ollama/Modelfiles/heredoc.modelfile

Comparison

Aspect OpenAI Ollama

Location

Cloud

Local

Cost

Per token

Free (hardware)

Privacy

Data sent to OpenAI

Stays local

Speed

Fast (GPUs)

Depends on hardware

Models

GPT-4, etc.

Open source

Environment Setup

# OpenAI
export OPENAI_API_KEY="sk-..."

# Ollama (no auth needed for local)
# Just ensure ollama is running: ollama serve

Learnings

LLM API Gotchas
  • OpenAI: API key from platform.openai.com

  • Ollama: Must run ollama serve or use systemd service

  • Streaming responses are NDJSON - one JSON per line

  • stream: false for single JSON response

  • Custom modelfiles live in ~/.ollama/Modelfiles/

  • Create model: ollama create <name> -f <modelfile>