The Anthropic component provides seamless integration with Anthropic's powerful AI models including Claude, and more. This component simplifies API interactions and provides a clean Rails interface for AI-powered features.
Whether you're building chatbots, content generation tools, or AI-assisted features, the Anthropic component handles authentication, rate limiting, and error handling for you.
In order to use the Anthropic API integration, you'll need to have a developer account, which you can get on Anthropic's developer portal.
Then, you'll need to get the following:
Start by running the installation command for the Anthropic component:
rails g instrumental:anthropic
Then add the following to your Rails credentials, and add your OpenAI access token and organization ID.
anthropic:
  api_key:
Finally, uncomment the code that's in the config/initializers/anthropic.rb file.
Restart your Rails server. You're now ready to use the Anthropic API integration!
It installs these essential files for using the Anthropic API integration:
config/initializers/anthropic.rbapp/services/anthropic_service.rbIn addition, it adds the following files, which serve as examples for how to use the Anthropic API integration:
anthropic_prompt_controller.rbapp/views/anthropic_promptThat example gives you a page at /anthropic_prompt where a user can input instructions, along with several options, click submit, and get an AI-generated response.
The example includes options for a user to provide system instructions, along with various parameters to control the AI's response, such as temperature, max tokens, selected model, and more.
This is intended to be a generic example to show you how you can make use of the OpenAI integration, and the service class that Instrumental Components provides.
The Anthropic component provides a service class to interact with Anthropic's API: app/services/anthropic_service.rb
Use this to give it a prompt (along with optional parameters), then generate and use a response in your application.
Here's a simple example: Let's say you have a Posts model, and immediately after creating a new post, you want to use AI to rewrite the post's title and make it more click-baity (just what the internet needs, right? 😂)
In your Post.rb model, you can have something like this:
class Post < ApplicationRecord
  # ... other model code ...
  after_create :rename
  private
  def rename
    anthropic_service = AnthropicService.new
    prompt = "Generate a single, post title that is more click-baity than the current title, while still being accurate and relevant to the post's content.  Just return the title, nothing else."
    options = {
      system_instructions: "You are a creative content marketing expert specializing in SEO and click-baity titles. Generate only the title, no explanations or quotes.",
      max_tokens: 50,
      temperature: 0.8
    }
    response = anthropic_service.generate_response(prompt, options)
    if response[:success] && response[:content].present?
      new_name = response[:content].strip.gsub(/['""]/, '')
      update(name: new_name)
      Rails.logger.info "Post title updated to: #{new_name}"
    else
      Rails.logger.error "Failed to generate post title: #{response[:error]}"
    end
  rescue => e
    Rails.logger.error "Error renaming post: #{e.message}"
  end
end
I created Instrumental Components to make designing and building professional apps with Ruby on Rails easy, fast, and fun. I use it on all of my projects and I hope it helps you build something great.