Use the Anthropic API with Ruby! 🤖🌌
You can apply for access to the API here.
🚢 Need someone to ship critical Rails features for you, fast? I'm taking on a few new clients at an experimental crazy low price, check it out: railsai.com
🎮 Ruby AI Builders Discord | 🐦 Twitter | 🤖 OpenAI Gem | 🚂 Midjourney Gem
Add this line to your application's Gemfile:
gem "anthropic"
And then execute:
$ bundle install
Or install with:
$ gem install anthropic
and require with:
require "anthropic"
- Get your API key from https://console.anthropic.com/account/keys
For a quick test you can pass your token directly to a new client:
client = Anthropic::Client.new(access_token: "access_token_goes_here")
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.
Anthropic.configure do |config|
config.access_token = ENV.fetch("ANTHROPIC_API_KEY")
end
Then you can create a client like this:
client = Anthropic::Client.new
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
Hit the Anthropic API for a completion:
response = client.complete(
parameters: {
model: "claude-2",
prompt: "How high is the sky?",
max_tokens_to_sample: 5
})
puts response["completion"]
# => " The sky has no definitive"
Note that all requests are prepended by this library with
\n\nHuman:
and appended with
\n\nAssistant:
so whatever prompt you pass will be sent to the API as
\n\nHuman: How high is the sky?\n\nAssistant:
This is a requirement of the API.
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
.
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.
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.
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.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Ruby Anthropic project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.