View Source Anthropix (Anthropix v0.0.1)

License

An up-to-date and fully-featured Elixir client library for Anthropic's REST API.

  • ✅ API client fully implementing the Anthropic API
  • 🛜 Streaming API requests
    • Stream to an Enumerable
    • Or stream messages to any Elixir process
  • 🔜 Advanced and flexible function calling workflow

This library is currently a WIP. Check back in a week or two, by which point it should be bangin!

Installation

The package can be installed by adding anthropix to your list of dependencies in mix.exs.

def deps do
  [
    {:anthropix, "0.0.1"}
  ]
end

Quickstart

TODO...

Summary

Types

Client struct

Chat message

Client response

Functions

Calling init/1 without passing an API key, creates a new Anthropix API client using the API key set in your application's config.

Calling init/2 with an API key creates a new Anthropix API client, using the given API key. Optionally, a keyword list of options can be passed through to Req.new/1.

Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.

Types

@type client() :: %Anthropix{req: Req.Request.t()}

Client struct

@type message() :: map()

Chat message

A chat message is a map/0 with the following fields:

  • :role (String.t/0) - Required. The role of the message, either user or assistant.
  • :content - Required. Message content, either a single string or an array of content blocks.
@type response() ::
  {:ok, map() | boolean() | Enumerable.t() | Task.t()} | {:error, term()}

Client response

Functions

@spec init() :: client()

Calling init/1 without passing an API key, creates a new Anthropix API client using the API key set in your application's config.

config :anthropix, :api_key, "sk-ant-your-key"

If given, a keyword list of options will be passed to Req.new/1.

Examples

iex> client = Anthropix.init()
%Anthropix{}
@spec init(keyword()) :: client()
Link to this function

init(api_key, opts \\ [])

View Source
@spec init(
  String.t(),
  keyword()
) :: client()

Calling init/2 with an API key creates a new Anthropix API client, using the given API key. Optionally, a keyword list of options can be passed through to Req.new/1.

Examples

iex> client = Anthropix.init("sk-ant-your-key", receive_timeout: :infinity)
%Anthropix{}
Link to this function

messages(client, params \\ [])

View Source
@spec messages(
  client(),
  keyword()
) :: any()

Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.

Options

  • :model (String.t/0) - Required. The model that will complete your prompt.
  • :messages (list of map/0) - Required. Input messages.
  • :system (String.t/0) - System prompt.
  • :max_tokens (integer/0) - The maximum number of tokens to generate before stopping. The default value is 4096.
  • :metadata (map/0) - A map describing metadata about the request.
  • :stop_sequences (list of String.t/0) - Custom text sequences that will cause the model to stop generating.
  • :stream - Whether to incrementally stream the response using server-sent events. The default value is false.
  • :temperature (float/0) - Amount of randomness injected into the response.
  • :top_k (integer/0) - Only sample from the top K options for each subsequent token.
  • :top_p (float/0) - Amount of randomness injected into the response.

Message structure

Each message is a map with the following fields:

  • :role (String.t/0) - Required. The role of the message, either user or assistant.
  • :content - Required. Message content, either a single string or an array of content blocks.

Examples

iex> messages = [
...>   %{role: "system", content: "You are a helpful assistant."},
...>   %{role: "user", content: "Why is the sky blue?"},
...>   %{role: "assistant", content: "Due to rayleigh scattering."},
...>   %{role: "user", content: "How is that different than mie scattering?"},
...> ]

iex> Anthropix.messages(client, [
...>   model: "claude-3-opus-20240229",
...>   messages: messages,
...> ])
{:ok, %{"content" => [%{
  "type" => "text",
  "text" => "Mie scattering affects all wavelengths similarly, while Rayleigh favors shorter ones."
}], ...}}

# Passing true to the :stream option initiates an async streaming request.
iex> Anthropix.messages(client, [
...>   model: "claude-3-opus-20240229",
...>   messages: messages,
...>   stream: true,
...> ])
{:ok, #Function<52.53678557/2 in Stream.resource/3>}