An Anthropic implementation of the OmniAI APIs.


gem install omniai-anthropic



A client is setup as follows if ENV['ANTHROPIC_API_KEY'] exists:

client =

A client may also be passed the following options:

  • api_key (required - default is ENV['ANTHROPIC_API_KEY'])
  • host (optional)


Global configuration is supported for the following options:

OmniAI::Anthropic.configure do |config|
  config.api_key = '...' # default: ENV['ANTHROPIC_API_KEY'] = '...' # default: ''


A chat completion is generated by passing in prompts using any a variety of formats:

completion ='Tell me a joke!')
completion.choice.message.content # 'Why did the chicken cross the road? To get to the other side.'
completion ={
  role: OmniAI::Chat::Role::USER,
  content: 'Is it wise to jump off a bridge?'
completion.choice.message.content # 'No.'
completion =[
    role: OmniAI::Chat::Role::SYSTEM,
    content: 'You are a helpful assistant.'
  'What is the capital of Canada?',
completion.choice.message.content # 'The capital of Canada is Ottawa.'


model takes an optional string (default is claude-3-haiku-20240307):

completion ='Provide code for fibonacci', model: OmniAI::Anthropic::Chat::Model::OPUS)
completion.choice.message.content # 'def fibonacci(n)...end'

Anthropic API Reference model


temperature takes an optional float between 0.0 and 1.0 (defaults is 0.7):

completion ='Pick a number between 1 and 5', temperature: 1.0)
completion.choice.message.content # '3'

Anthropic API Reference temperature


stream takes an optional a proc to stream responses in real-time chunks instead of waiting for a complete response:

stream = proc do |chunk|
  print( # 'Better', 'three', 'hours', ...
end'Be poetic.', stream:)

Anthropic API Reference stream


format takes an optional symbol (:json) and modifies requests to send additional system text requesting JSON:

completion =[
  { role: OmniAI::Chat::Role::USER, content: 'What is the name of the drummer for the Beatles?' }
], format: :json)
JSON.parse(completion.choice.message.content) # { "name": "Ringo" }

Anthropic API Reference control-output-format