Skip to main content

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

Project description

aiclient-llm

PyPI version License: Apache 2.0

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, and custom middleware.
  • Examples: Runnable demo scripts for new features.

Key Features

  • 🦄 Unified Interface: Swap between OpenAI, Anthropic, Google, xAI, and Ollama seamlessly.
  • Async & Sync: Native asyncio support for high-performance apps.
  • 🚀 Prompt Caching: Native support for Anthropic Prompt Caching headers (v0.1.2).
  • 🏗️ Structured Outputs: Native strict JSON Schema support for OpenAI (v0.1.2).
  • 🛡️ Resilient: Circuit Breakers, Rate Limiters, and automatic retries (v0.1.2).
  • 🔭 Observability: Tracing and OpenTelemetry hooks (v0.1.2).
  • 🤖 Agent Primitives: Built-in ReAct loop for tool-using agents.
  • 📊 Middleware: Inspect requests, track costs, or log data.

Installation

pip install aiclient-llm

(Note: Not yet on PyPI, install from source/git)

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.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?"))

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.1.2.tar.gz (52.8 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.1.2-py3-none-any.whl (29.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiclient_llm-0.1.2.tar.gz
  • Upload date:
  • Size: 52.8 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.1.2.tar.gz
Algorithm Hash digest
SHA256 7b9181f9941ae3b63717c6da999ec2f349999513a95dcd6a548aa143c274fbb7
MD5 8bf8e2559dbb18a1869dd0268e798f48
BLAKE2b-256 07b38dd82d1cab430d71d1e6580b0f52cdab63032fd55fc1bb02c75792aa3fe3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiclient_llm-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 29.4 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.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 33219d9d053a7692ebb1e2acde14f300c7bfd8b7f59c3eafce3bae9e9a7406c7
MD5 086a00bad5a91286df5811cf17b4ce83
BLAKE2b-256 ac6f43a4968b4eabda73cd61249f2c923cdaeed57ecf3a5205140a8c76456977

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