Skip to content

Expedient/exp-ai-api-examples

Repository files navigation

AI API Examples

This repository contains examples and documentation for integrating with AI Chat APIs. Examples are provided for both OpenWebUI (local/self-hosted) and Expedient AI Cloud Gateway.

OpenWebUI Quick Start (v3)

For OpenWebUI v0.6.32+ (local or self-hosted deployments)

  1. Get your API key or JWT token from OpenWebUI:

    • Click on your User icon (top right)
    • Select Settings
    • Go to Account tab
    • Under API Keys section, click Show to reveal your keys
    • Copy either the API Key (sk-...) or JWT Token (eyJ...)
  2. Create and activate a virtual environment:

    # Create virtual environment
    python -m venv venv
    
    # Activate virtual environment
    # On macOS/Linux:
    source venv/bin/activate
    
    # On Windows:
    venv\Scripts\activate
  3. Install dependencies: pip install -r requirements.txt

  4. Update the configuration in the v3 example files:

    • Set chat_url to your OpenWebUI URL (e.g., "http://localhost:3000")
    • Set api_key or jwt_token with your credentials
  5. Run the OpenWebUI examples:

    # Quick test (simplest)
    python v3_quick_start.py
    
    # Standard streaming with API Key
    python v3_example.py
    
    # Using JWT Token authentication
    python v3_example_jwt.py

Expedient AI Cloud Gateway Quick Start (v1)

For Expedient AI hosted service

  1. Get your API key from Expedient AI
  2. Create and activate a virtual environment (see above)
  3. Install dependencies: pip install -r requirements.txt
  4. Update the API key in the example file that you are running
  5. Run the examples:
    # Quick test (simplest)
    python quick_start.py
    
    # Standard streaming models
    python example.py
    
    # Reasoning models (for complex analysis)
    python reasoning_example.py
    
    # Web access models with citations (for live web search)
    python citation_example.py
    
    # OR use cURL commands directly (see curl.md)
    curl -X POST "[ ENTER_CHAT_URL_HERE ]/chat/completions" \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer [ ENTER_API_KEY_HERE ]" \
      -d '{"model": "gpt-4.1", "messages": [{"role": "user", "content": "Hello!"}]}'

Files in this Repository

OpenWebUI Examples (v3)

πŸ“„ v3_quick_start.py

Minimal OpenWebUI example for immediate testing. Features:

  • Ultra-simple code (~40 lines)
  • OpenWebUI API endpoint configuration
  • Perfect for local/self-hosted OpenWebUI instances
  • Copy-paste ready

πŸ“„ v3_example.py

Complete OpenWebUI streaming example with API Key authentication. Features:

  • Real-time streaming responses
  • API Key authentication (recommended for scripts)
  • Detailed comments and explanations
  • Works with any OpenWebUI-compatible model

πŸ“„ v3_example_jwt.py

OpenWebUI streaming example with JWT Token authentication. Features:

  • Same streaming functionality as v3_example.py
  • JWT Token authentication (session-based)
  • Demonstrates alternative authentication method
  • Useful for web applications

Expedient AI Cloud Examples (v1)

πŸ“„ quick_start.py

Minimal example for immediate testing. Features:

  • Ultra-simple code (30 lines)
  • No comments or explanations
  • Just the essentials for quick API testing
  • Perfect for copy-paste and quick modifications

πŸ“„ example.py

Complete Python script demonstrating streaming AI responses with standard models. Features:

  • Real-time streaming responses
  • Standard AI models (GPT, Claude, Gemini, Perplexity)
  • Detailed comments and explanations
  • Production-ready streaming implementation

πŸ“„ reasoning_example.py

Advanced reasoning models with animated thinking indicators. Features:

  • Complex analytical problem solving
  • Step-by-step reasoning processes
  • Animated thinking dots while processing
  • Models: o4-mini, o3, Claude 4, Gemini 2.5

πŸ“„ citation_example.py

Perplexity web reasoning models with real-time data and citations. Features:

  • Real-time web search during AI responses
  • Current events and fact-checking capabilities
  • Automatic citation detection and reporting
  • Animated search indicators

Documentation

πŸ“„ postman.md

Complete Postman collection documentation with:

  • 8 ready-to-use API request examples
  • All supported AI models and providers
  • Parameter explanations and usage tips
  • Import instructions for Postman

πŸ“„ curl.md

Command-line cURL examples for developers and scripters. Features:

  • 8 cURL commands matching all Postman examples
  • Advanced options and streaming processing
  • Environment variable setup for security
  • Batch processing and error handling examples

πŸ“„ .gitignore

Python-specific gitignore file that excludes:

  • Virtual environments
  • API keys and sensitive files
  • Python cache files
  • IDE configuration files

Supported AI Providers

Both OpenWebUI and Expedient AI Cloud Gateway support models from multiple providers:

  • OpenAI: GPT-4.1, GPT-5, o3, o4-mini (reasoning), GPT-4o, GPT-4o-mini, GPT-3.5-turbo
  • Anthropic: Claude-Sonnet-4, Claude-Sonnet-4.5, Claude-3.7-Sonnet, Claude-3-Haiku, Claude-3-Opus
  • Google: Gemini-2.5-Pro, Gemini-2.5-Flash, Gemini-1.5-Pro, Gemini-1.5-Flash
  • Perplexity: Sonar-Pro, Sonar-Reasoning-Pro, Sonar (web-connected with real-time data)

Note: Available models depend on your OpenWebUI configuration or Expedient AI subscription.

Key Features

βœ… Unified Interface - One API for multiple AI providers
βœ… Real-time Streaming - See responses as they're generated
βœ… Multiple Models - Choose the best model for your use case
βœ… Web Access & Citations - Real-time data with source transparency
βœ… Quick Start - Ultra-simple 30-line example for immediate testing
βœ… Enterprise Ready - Production-grade error handling
βœ… Easy Integration - Simple REST API with JSON

API Endpoints

OpenWebUI

http://localhost:3000/api/v1/chat/completions

Or replace localhost:3000 with your OpenWebUI instance URL.

Expedient AI Cloud Gateway

[ ENTER_CHAT_URL_HERE ]/chat/completions

Authentication

OpenWebUI

Get your credentials from OpenWebUI:

  1. Click on your User icon β†’ Settings β†’ Account
  2. Under API Keys, click Show
  3. Copy either:
    • API Key (sk-...) - Recommended for scripts and automation
    • JWT Token (eyJ...) - For session-based authentication

All requests require a Bearer token:

Authorization: Bearer YOUR_API_KEY_OR_JWT_TOKEN

Expedient AI Cloud Gateway

All requests require a Bearer token in the Authorization header:

Authorization: Bearer [ ENTER_API_KEY_HERE ]

Basic Usage Examples

OpenWebUI

import requests
import json

# Simple streaming request to OpenWebUI
response = requests.post(
    "http://localhost:3000/api/v1/chat/completions",
    headers={
        "Content-Type": "application/json",
        "Authorization": "Bearer YOUR_API_KEY"
    },
    json={
        "model": "gpt-4.1",
        "messages": [{"role": "user", "content": "Hello, world!"}],
        "stream": True
    },
    stream=True
)

# Process streaming response
for line in response.iter_lines(decode_unicode=True):
    if line and line.startswith("data: "):
        data_str = line[6:]
        if data_str.strip() == "[DONE]":
            break
        try:
            data = json.loads(data_str)
            if "choices" in data and data["choices"]:
                delta = data["choices"][0].get("delta", {})
                if "content" in delta:
                    print(delta["content"], end="", flush=True)
        except:
            continue

Expedient AI Cloud Gateway

import requests
import json

# Simple streaming request to Expedient AI
response = requests.post(
    "[ ENTER_CHAT_URL_HERE ]/chat/completions",
    headers={
        "Content-Type": "application/json",
        "Authorization": "Bearer [ ENTER_API_KEY_HERE ]"
    },
    json={
        "model": "gpt-4.1",
        "messages": [{"role": "user", "content": "Hello, world!"}],
        "stream": True
    },
    stream=True
)

# Process streaming response (same as OpenWebUI)
for line in response.iter_lines(decode_unicode=True):
    if line and line.startswith("data: "):
        data_str = line[6:]
        if data_str.strip() == "[DONE]":
            break
        try:
            data = json.loads(data_str)
            if "choices" in data and data["choices"]:
                delta = data["choices"][0].get("delta", {})
                if "content" in delta:
                    print(delta["content"], end="", flush=True)
        except:
            continue

Getting Started

Prerequisites

  • Python 3.6 or higher
  • requests library (pip install requests)
  • Either:
    • Running OpenWebUI instance (v0.6.32+), OR
    • Valid Expedient AI API key

Installation

  1. Clone this repository
  2. Create and activate a virtual environment:
    # Create virtual environment
    python -m venv venv
    
    # Activate virtual environment
    # On macOS/Linux:
    source venv/bin/activate
    
    # On Windows:
    venv\Scripts\activate
  3. Install dependencies:
    pip install -r requirements.txt

For OpenWebUI Users

  1. Get your API key from OpenWebUI: User β†’ Settings β†’ Account β†’ API Keys β†’ Show
  2. Update chat_url and api_key in the v3 scripts
  3. Run the OpenWebUI examples:
    python v3_quick_start.py   # or v3_example.py or v3_example_jwt.py

For Expedient AI Users

  1. Copy your API key from the Expedient AI dashboard
  2. Update the api_key variable in the script you want to use
  3. Run the examples:
    # Quick test
    python quick_start.py
    
    # Standard streaming models  
    python example.py
    
    # Reasoning models
    python reasoning_example.py
    
    # Web search with citations
    python citation_example.py

Model Selection Guide

Choose the right model for your needs:

Use Case Recommended Models
Advanced Reasoning & Logic o4-mini, claude-sonnet-4-20250514, gemini/gemini-2.5-pro
Complex Analysis gpt-4.1, gpt-4o, claude-3-7-sonnet-20250219, gemini/gemini-1.5-pro
Large Text Processing gemini/gemini-2.5-flash (1M+ token context)
Creative Writing claude-sonnet-4-20250514, claude-3-7-sonnet-20250219, gpt-4o
Code Generation gpt-4.1, claude-sonnet-4-20250514, gpt-4o
Fast Responses gpt-4o-mini, claude-3-haiku-20240307, gemini/gemini-1.5-flash
Current Events & Web Data perplexity/sonar-reasoning-pro, perplexity/sonar-pro
General Purpose gpt-4.1 (default), gpt-3.5-turbo

Documentation

OpenWebUI (v3)

Expedient AI Cloud Gateway (v1)

  • Postman Collection - Complete API testing examples
  • cURL Examples - Command-line examples for terminal and scripts
  • Quick Start - Minimal example for immediate testing
  • Standard Streaming Example - Detailed implementation with comments
  • Reasoning Example - Advanced reasoning with thinking animation
  • Citation Example - Web search with real-time citations
  • Live Swagger API Documentation - Visit [ ENTER_CHAT_URL_HERE ]/docs in your browser for interactive API documentation
  • API Documentation - Contact Expedient AI for detailed API docs

Support

For technical support and API access:

  • Website: Expedient AI
  • Documentation: See postman.md for comprehensive examples
  • Issues: Use this repository's issue tracker for code-related questions

Security Notes

πŸ”’ Never commit API keys to version control
πŸ”’ Use environment variables for production deployments
πŸ”’ Rotate API keys regularly
πŸ”’ Monitor API usage and costs

License

This example code is provided for demonstration purposes. Please check with Expedient AI for licensing terms for the API service itself.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages