Skip to main content

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

Project description

aiclient-llm

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.0.tar.gz (59.6 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.0-py3-none-any.whl (43.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiclient_llm-0.2.0.tar.gz
  • Upload date:
  • Size: 59.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aiclient_llm-0.2.0.tar.gz
Algorithm Hash digest
SHA256 641b82dda79edbf8f45e9cee0dbc2dab8a77ad78981fcc26b16e7e2d9989d8c2
MD5 c2596a7257197aa171daf05875f986de
BLAKE2b-256 4261d0815352837ed84a44410646c9befc277d00c1d97166a43c880126321ce7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiclient_llm-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 43.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for aiclient_llm-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3afb9bdcc942c0bfe9de4b465df8d2cd8567b9f8bea5f08e96a959e09d569a5f
MD5 51674313b6394b2fd38909a7e60aa147
BLAKE2b-256 a2121a8cf82abd7f66967e3d06d78e75ac59c544f9ea57b0cc36a99ca8c6b92b

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