Skip to content

Unofficial Anthropic API client for Elixir. Integrate Claude, Anthropic's powerful language model, into your applications.

License

Notifications You must be signed in to change notification settings

lebrunel/anthropix

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Anthropix

Anthropix

Hex.pm License Build Status

Anthropix is an open-source Elixir client for the Anthropic API, providing a simple and convenient way to integrate Claude, Anthropic's powerful language model, into your applications.

  • ✅ API client fully implementing the Anthropic API
  • 🧰 Tool use (function calling)
  • ⚡ Prompt caching
  • 📦 Message batching
  • 🛜 Streaming API requests
    • Stream to an Enumerable
    • Or stream messages to any Elixir process

Installation

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

def deps do
  [
    {:anthropix, "~> 0.4"}
  ]
end

Quickstart

Note

Beta features

Anthropic frequently ship new features under a beta flag, requiring headers to be added to your requests to take advantage of the feature. This library currently enables the following beta headers by default:

  • prompt-caching-2024-07-31
  • message-batches-2024-09-24

If required, beta headers can be customised with init/2.

For more examples, refer to the Anthropix documentation.

Initiate a client.

See Anthropix.init/2.

client = Anthropix.init(api_key)

Chat with Claude

See Anthropix.chat/2.

messages = [
  %{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?"},
]

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

Streaming

A streaming request can be initiated by setting the :stream option.

When :stream is true a lazy t:Enumerable.t/0 is returned which can be used with any Stream functions.

{:ok, stream} = Anthropix.chat(client, [
  model: "claude-3-opus-20240229",
  messages: messages,
  stream: true,
])
# {:ok, #Function<52.53678557/2 in Stream.resource/3>}

stream
|> Stream.each(&update_ui_with_chunk/1)
|> Stream.run()
# :ok

Because the above approach builds the t:Enumerable.t/0 by calling receive, using this approach inside GenServer callbacks may cause the GenServer to misbehave. Setting the :stream option to a t:pid/0 returns a t:Task.t/0 which will send messages to the specified process.


# License

This package is open source and released under the [Apache-2 License](https://github.com/lebrunel/ollama/blob/master/LICENSE).

© Copyright 2024 [Push Code Ltd](https://www.pushcode.com/).

About

Unofficial Anthropic API client for Elixir. Integrate Claude, Anthropic's powerful language model, into your applications.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages