ExLLM.Adapters.Anthropic (ex_llm v0.1.0)

View Source

Anthropic Claude API adapter for ExLLM.

Configuration

This adapter requires an Anthropic API key and optionally a base URL.

Using Environment Variables

# Set environment variables
export ANTHROPIC_API_KEY="your-api-key"
export ANTHROPIC_MODEL="claude-3-5-sonnet-20241022"  # optional

# Use with default environment provider
ExLLM.Adapters.Anthropic.chat(messages, config_provider: ExLLM.ConfigProvider.Env)

Using Static Configuration

config = %{
  anthropic: %{
    api_key: "your-api-key",
    model: "claude-3-5-sonnet-20241022",
    base_url: "https://api.anthropic.com/v1"  # optional
  }
}
{:ok, provider} = ExLLM.ConfigProvider.Static.start_link(config)
ExLLM.Adapters.Anthropic.chat(messages, config_provider: provider)

Example Usage

messages = [
  %{role: "user", content: "Hello, how are you?"}
]

# Simple chat
{:ok, response} = ExLLM.Adapters.Anthropic.chat(messages)
IO.puts(response.content)

# Streaming chat
{:ok, stream} = ExLLM.Adapters.Anthropic.stream_chat(messages)
for chunk <- stream do
  if chunk.content, do: IO.write(chunk.content)
end