View Source Anthropix (Anthropix v0.0.1)
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
@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, eitheruser
orassistant
.: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{}
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{}
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 ofmap/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 is4096
.:metadata
(map/0
) - A map describing metadata about the request.:stop_sequences
(list ofString.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 isfalse
.: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, eitheruser
orassistant
.: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>}