Skip to content

Latest commit

 

History

History
71 lines (50 loc) · 2.14 KB

File metadata and controls

71 lines (50 loc) · 2.14 KB

Chat Completions quickstart

One API across multiple LLM providers: write your code once, switch models by changing a string.

1. Install

pip install aisuite               # base package, no provider SDKs
pip install 'aisuite[anthropic]'  # with one provider's SDK
pip install 'aisuite[all]'        # with every provider SDK

2. Set your API keys

You need keys only for the providers you call. The provider guides walk through obtaining a key for each one.

Set them as environment variables (tools like python-dotenv or direnv help manage them):

export OPENAI_API_KEY="your-openai-api-key"
export ANTHROPIC_API_KEY="your-anthropic-api-key"

Keys can also be passed programmatically to the Client constructor:

client = ai.Client({"openai": {"api_key": "..."}})

3. Your first completions

Model names use the format <provider>:<model-name> — aisuite routes each call to the right provider and translates parameters and responses:

import aisuite as ai
client = ai.Client()

models = ["openai:gpt-4o", "anthropic:claude-3-5-sonnet-20240620"]

messages = [
    {"role": "system", "content": "Respond in Pirate English."},
    {"role": "user", "content": "Tell me a joke."},
]

for model in models:
    response = client.chat.completions.create(
        model=model,
        messages=messages,
        temperature=0.75
    )
    print(response.choices[0].message.content)

Core parameters (temperature, max_tokens, tools, …) work provider-agnostically; aisuite maps them to each SDK's conventions.

Local models

Run fully local via Ollama — no API key required:

response = client.chat.completions.create(
    model="ollama:llama3.3",
    messages=[{"role": "user", "content": "Hello!"}],
)

Going further