Project

anthropic

0.03
The project is in a healthy, maintained state
Anthropic API + Ruby! 🤖🌌
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies
 Project Readme

Anthropic

Gem Version GitHub license CircleCI Build Status

Use the Anthropic API with Ruby! 🤖🌌

You can get access to the API here.

🎮 Ruby AI Builders Discord | 🐦 Twitter | 🤖 OpenAI Gem | 🚂 Midjourney Gem

Bundler

Add this line to your application's Gemfile:

gem "anthropic"

And then execute:

$ bundle install

Gem install

Or install with:

$ gem install anthropic

and require with:

require "anthropic"

Usage

Quickstart

For a quick test you can pass your token directly to a new client:

client = Anthropic::Client.new(access_token: "access_token_goes_here")

With Config

For a more robust setup, you can configure the gem with your API keys, for example in an anthropic.rb initializer file. Never hardcode secrets into your codebase - instead use something like dotenv to pass the keys safely into your environments or rails credentials if you are using this in a rails project.

Anthropic.configure do |config|
  # With dotenv
  config.access_token = ENV.fetch("ANTHROPIC_API_KEY")
  # OR
  # With Rails credentials
  config.access_token = Rails.application.credentials.dig(:anthropic, :api_key)
end

Then you can create a client like this:

client = Anthropic::Client.new

Change version or timeout

You can change to a different dated version (different from the URL version which is just v1) of Anthropic's API by passing anthropic_version when initializing the client. If you don't the default latest will be used, which is "2023-06-01". More info

The default timeout for any request using this library is 120 seconds. You can change that by passing a number of seconds to the request_timeout when initializing the client.

client = Anthropic::Client.new(
  access_token: "access_token_goes_here",
  anthropic_version: "2023-01-01", # Optional
  request_timeout: 240 # Optional
)

You can also set these keys when configuring the gem:

Anthropic.configure do |config|
  config.access_token = ENV.fetch("ANTHROPIC_API_KEY")
  config.anthropic_version = "2023-01-01" # Optional
  config.request_timeout = 240 # Optional
end

Models

Available Models:

Name API Name
Claude 3 Opus claude-3-opus-20240229
Claude 3 Sonnet claude-3-sonnet-20240229
Claude 3 Haiku claude-3-haiku-20240307

You can find the latest model names in the Anthropic API documentation.

Messages

POST https://api.anthropic.com/v1/messages

Send a sequence of messages (user or assistant) to the API and receive a message in response.

response = client.messages(
  parameters: {
    model: "claude-3-haiku-20240307", # claude-3-opus-20240229, claude-3-sonnet-20240229
    system: "Respond only in Spanish.",
    messages: [
      {"role": "user", "content": "Hello, Claude!"}
    ],
    max_tokens: 1000
  }
)
# => {
# =>   "id" => "msg_0123MiRVCgSG2PaQZwCGbgmV",
# =>   "type" => "message",
# =>   "role" => "assistant",
# =>   "content" => [{"type"=>"text", "text"=>"¡Hola! Es un gusto saludarte. ¿En qué puedo ayudarte hoy?"}],
# =>   "model" => "claude-3-haiku-20240307",
# =>   "stop_reason" => "end_turn",
# =>   "stop_sequence" => nil,
# =>   "usage" => {"input_tokens"=>17, "output_tokens"=>32}
# => }

Development

After checking out the repo, run bin/setup to install dependencies. You can run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

To run all tests, execute the command bundle exec rake, which will also run the linter (Rubocop). This repository uses VCR to log API requests.

Warning

If you have an ANTHROPIC_API_KEY in your ENV, running the specs will use this to run the specs against the actual API, which will be slow and cost you money - 2 cents or more! Remove it from your environment with unset or similar if you just want to run the specs against the stored VCR responses.

Warning

If you have an ANTHROPIC_API_KEY in your ENV, running the specs will use this to run the specs against the actual API, which will be slow and cost you money - 2 cents or more! Remove it from your environment with unset or similar if you just want to run the specs against the stored VCR responses.

Release

First run the specs without VCR so they actually hit the API. This will cost 2 cents or more. Set ANTHROPIC_API_KEY in your environment or pass it in like this:

ANTHROPIC_API_KEY=123abc bundle exec rspec

Then update the version number in version.rb, update CHANGELOG.md, run bundle install to update Gemfile.lock, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/alexrudall/anthropic. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Ruby Anthropic project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.