Any-LLM SDK

1.13.0 · active · verified Fri Apr 17

The Any-LLM SDK provides a unified Python interface for interacting with various Large Language Model (LLM) providers through the Any-LLM gateway. It aims to abstract away provider-specific complexities, offering a consistent API for chat completions, embeddings, and more. The current version is 1.13.0, and it maintains a rapid release cadence with frequent updates and improvements.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the Any-LLM Gateway client using environment variables for the gateway URL and API key, then sends a simple chat completion request to a specified model. The `messages` types from `any_llm_sdk.types` (re-exporting Anthropic types) are used for constructing the payload.

import os
from any_llm_sdk import Gateway
from any_llm_sdk.types import messages

# Ensure these environment variables are set:
# os.environ['ANY_LLM_GATEWAY_URL'] = 'YOUR_GATEWAY_URL'
# os.environ['ANY_LLM_GATEWAY_API_KEY'] = 'YOUR_API_KEY'

try:
    gateway = Gateway(
        base_url=os.environ.get('ANY_LLM_GATEWAY_URL', 'https://gateway.any-llm.ai'),
        api_key=os.environ.get('ANY_LLM_GATEWAY_API_KEY', '')
    )

    messages_payload = [
        messages.UserMessage(content='Hello, what is the capital of France?')
    ]

    chat_completion = gateway.chat.completions.create(
        model='gpt-4o',
        messages=messages_payload,
        max_tokens=100
    )
    print(chat_completion.choices[0].message.content)

except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure ANY_LLM_GATEWAY_URL and ANY_LLM_GATEWAY_API_KEY environment variables are set.")

view raw JSON →