Skip to main content

A minimal, generic client for AI models (OpenAI, Anthropic, Google, xAI) with middleware support.

Project description

aiclient-llm

AIClient Banner

PyPI version Python Versions License: Apache 2.0 Downloads

A minimal, unified, and resilient Python client for modern LLMs.

Supports OpenAI, Anthropic (Claude 3), Google (Gemini), and xAI (Grok) with a single, consistent interface.

Documentation

  • Getting Started: Installation, Configuration, Basic Usage.
  • Features Guide: Agents, Multimodal, Local LLMs (Ollama), Structured Output.
  • Middleware: Cost tracking, logging, resilience, and custom middleware.
  • Memory: Conversation history management and persistence.
  • Testing: Mock providers and testing utilities.
  • Error Handling: Exception types and debugging.
  • Examples: Runnable demo scripts for new features.

Key Features

  • 🦄 Unified API: Works with OpenAI, Anthropic, Google Gemini, and Ollama.
  • Streaming Support: Real-time responses with a simple iterator interface.
  • 👁️ Multimodal (Vision): Send images (paths, URLs, base64) to vision-capable models.
  • 🚀 Prompt Caching: Native support for Anthropic Prompt Caching headers.
  • 🏗️ Structured Outputs: Native strict JSON Schema support for OpenAI.
  • 🛡️ Resilient: Circuit Breakers, Rate Limiters, and automatic retries.
  • 🔭 Observability: Tracing and OpenTelemetry hooks.
  • 🤖 Agent Primitives: Built-in ReAct loop for tool-using agents.
  • 🔌 Model Context Protocol (MCP): Connect to 16K+ external tools (GitHub, Postgres, filesystem).
  • 📊 Middleware: Inspect requests, track costs, or log data.
  • 🧠 Memory Management: Built-in conversation history with token-aware truncation
  • 🧪 Testing Utilities: Mock providers for deterministic unit tests
  • 📦 Batch Processing: Efficiently process thousands of requests concurrently
  • 🛡️ Type-Safe Errors: Specific exception types for better error handling

Installation

pip install aiclient-llm

Quick Start

Basic Chat

from aiclient import Client

client = Client(
    api_key_openai="sk-...", 
    api_key_anthropic="sk-ant-..."
)

# Call OpenAI
response = client.chat("gpt-4o").generate("Hello!")
print(response.text)

# Call Claude
response = client.chat("claude-3-opus-20240229").generate("Hello!")
print(response.text)

Multimodal (Vision)

from aiclient.data_types import UserMessage, Text, Image

msg = UserMessage(content=[
    Text(text="What's in this image?"),
    Image(path="./image.png") # Handles base64 automatically
])

response = client.chat("gpt-4o").generate([msg])
print(response.text)

Agents (Tool Use)

from aiclient.agent import Agent

def get_weather(location: str):
    return "Sunny in " + location

agent = Agent(
    model=client.chat("gpt-4o"),
    tools=[get_weather]
)

print(agent.run("Weather in SF?"))

MCP Integration 🔌

Connect to external tools using the Model Context Protocol.

agent = Agent(
    model=client.chat("gpt-4o"),
    mcp_servers={
        "filesystem": {
            "command": "npx",
            "args": ["-y", "@modelcontextprotocol/server-filesystem", "./workspace"]
        }
    }
)

# Agent can now use file system tools!
print(agent.run("List all Python files in the current directory"))

Local LLMs (Ollama) 🏠

Use the provider:model syntax to route requests to local models (e.g., via Ollama).

# Connects to http://localhost:11434/v1 by default
client.chat("ollama:llama3").generate("Why is the sky blue?")

# Connect to custom URL (e.g. LMStudio)
client = Client(ollama_base_url="http://localhost:1234/v1")
client.chat("ollama:mistral").generate("Hi")

Streaming

for chunk in client.chat("gpt-4o").stream("Write a poem"):
    print(chunk.text, end="", flush=True)

Configuration

Retries

# Retries up to 3 times with backoff
client = Client(max_retries=3, retry_delay=1.0)

License

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiclient_llm-0.2.2.tar.gz (218.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiclient_llm-0.2.2-py3-none-any.whl (44.6 kB view details)

Uploaded Python 3

File details

Details for the file aiclient_llm-0.2.2.tar.gz.

File metadata

  • Download URL: aiclient_llm-0.2.2.tar.gz
  • Upload date:
  • Size: 218.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for aiclient_llm-0.2.2.tar.gz
Algorithm Hash digest
SHA256 f1965e790a3f3cf4bd77bc862fd8a7035709284de062de5f511d417609857d60
MD5 c7c085c45648d3f8f73ed51b775a04db
BLAKE2b-256 8227f6c7d65aa65d8dc9ddbfeb3eeb32507bc045ec10f5ebb99e2b7afd356d62

See more details on using hashes here.

File details

Details for the file aiclient_llm-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: aiclient_llm-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 44.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.14

File hashes

Hashes for aiclient_llm-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 700bf397e255344451ad99f7b61a65d4530d6afceeed6ab24472f7a971727854
MD5 d257492fc8033b6c86a005b666813710
BLAKE2b-256 482a4f4f987eaec28c1a0bdb70db2ec17d828619e4d1e89a5ed272e790b6c9e9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page