Class: RailsAi::Providers::AnthropicAdapter
- Defined in:
- lib/rails_ai/providers/anthropic_adapter.rb
Constant Summary collapse
- ANTHROPIC_API_BASE =
"https://api.anthropic.com/v1"
Instance Method Summary collapse
-
#analyze_image!(image:, prompt:, model: "claude-3-5-sonnet-20241022", **opts) ⇒ Object
Multimodal analysis - Anthropic supports image analysis with Claude 3 Vision.
- #analyze_video!(video:, prompt:, model:, **opts) ⇒ Object
-
#chat!(messages:, model:, **opts) ⇒ Object
Text-based operations.
- #create_variation!(image:, **opts) ⇒ Object
- #edit_image!(image:, prompt:, **opts) ⇒ Object
- #edit_video!(video:, prompt:, **opts) ⇒ Object
- #embed!(texts:, model:, **opts) ⇒ Object
-
#generate_image!(prompt:, model:, **opts) ⇒ Object
Image generation - Anthropic doesn't have image generation.
-
#generate_speech!(text:, model:, **opts) ⇒ Object
Audio generation - Anthropic doesn't have audio generation.
-
#generate_video!(prompt:, model:, **opts) ⇒ Object
Video generation - Anthropic doesn't have video generation.
-
#initialize ⇒ AnthropicAdapter
constructor
A new instance of AnthropicAdapter.
- #stream_chat!(messages:, model:, **opts, &on_token) ⇒ Object
- #transcribe_audio!(audio:, model:, **opts) ⇒ Object
Constructor Details
#initialize ⇒ AnthropicAdapter
Returns a new instance of AnthropicAdapter.
12 13 14 15 |
# File 'lib/rails_ai/providers/anthropic_adapter.rb', line 12 def initialize @api_key = ENV.fetch("ANTHROPIC_API_KEY") super end |
Instance Method Details
#analyze_image!(image:, prompt:, model: "claude-3-5-sonnet-20241022", **opts) ⇒ Object
Multimodal analysis - Anthropic supports image analysis with Claude 3 Vision
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/rails_ai/providers/anthropic_adapter.rb', line 117 def analyze_image!(image:, prompt:, model: "claude-3-5-sonnet-20241022", **opts) return "[stubbed] Image analysis: #{prompt}" if RailsAi.config.stub_responses # Anthropic supports image analysis with Claude 3 Vision models = [ { role: "user", content: [ { type: "image", source: { type: "base64", media_type: detect_image_type(image), data: extract_base64_data(image) } }, { type: "text", text: prompt } ] } ] response = make_request( "messages", { model: model, max_tokens: opts[:max_tokens] || RailsAi.config.token_limit, messages: , temperature: opts[:temperature] || 1.0, top_p: opts[:top_p] || 1.0, top_k: opts[:top_k] || 0, **opts.except(:max_tokens, :temperature, :top_p, :top_k) } ) response.dig("content", 0, "text") end |
#analyze_video!(video:, prompt:, model:, **opts) ⇒ Object
157 158 159 160 |
# File 'lib/rails_ai/providers/anthropic_adapter.rb', line 157 def analyze_video!(video:, prompt:, model:, **opts) return "[stubbed] Video analysis: #{prompt}" if RailsAi.config.stub_responses raise NotImplementedError, "Anthropic doesn't support video analysis. Use OpenAI or Gemini for video analysis." end |
#chat!(messages:, model:, **opts) ⇒ Object
Text-based operations
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rails_ai/providers/anthropic_adapter.rb', line 18 def chat!(messages:, model:, **opts) return "(stubbed) #{.last[:content]}" if RailsAi.config.stub_responses # Convert OpenAI format to Anthropic format = () response = make_request( "messages", { model: model, max_tokens: opts[:max_tokens] || RailsAi.config.token_limit, messages: , temperature: opts[:temperature] || 1.0, top_p: opts[:top_p] || 1.0, top_k: opts[:top_k] || 0, stop_sequences: opts[:stop_sequences] || [], **opts.except(:max_tokens, :temperature, :top_p, :top_k, :stop_sequences) } ) response.dig("content", 0, "text") end |
#create_variation!(image:, **opts) ⇒ Object
89 90 91 92 |
# File 'lib/rails_ai/providers/anthropic_adapter.rb', line 89 def create_variation!(image:, **opts) return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==" if RailsAi.config.stub_responses raise NotImplementedError, "Anthropic doesn't support image variations. Use OpenAI or Gemini for image variations." end |
#edit_image!(image:, prompt:, **opts) ⇒ Object
84 85 86 87 |
# File 'lib/rails_ai/providers/anthropic_adapter.rb', line 84 def edit_image!(image:, prompt:, **opts) return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==" if RailsAi.config.stub_responses raise NotImplementedError, "Anthropic doesn't support image editing. Use OpenAI or Gemini for image editing." end |
#edit_video!(video:, prompt:, **opts) ⇒ Object
100 101 102 103 |
# File 'lib/rails_ai/providers/anthropic_adapter.rb', line 100 def edit_video!(video:, prompt:, **opts) return "data:video/mp4;base64,AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAAAB1tZGF0AQAAARxtYXNrAAAAAG1wNDEAAAAAIG1kYXQ=" if RailsAi.config.stub_responses raise NotImplementedError, "Anthropic doesn't support video editing. Use OpenAI or Gemini for video editing." end |
#embed!(texts:, model:, **opts) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rails_ai/providers/anthropic_adapter.rb', line 66 def (texts:, model:, **opts) return Array.new(texts.length) { [0.0] * 1024 } if RailsAi.config.stub_responses # Anthropic doesn't have a direct embedding API, but we can use their models # This is a placeholder implementation that could be enhanced texts.map do |text| # In a real implementation, you might use a different service for embeddings # or implement a workaround using Claude's text understanding Array.new(1024) { rand(-1.0..1.0) } end end |
#generate_image!(prompt:, model:, **opts) ⇒ Object
Image generation - Anthropic doesn't have image generation
79 80 81 82 |
# File 'lib/rails_ai/providers/anthropic_adapter.rb', line 79 def generate_image!(prompt:, model:, **opts) return "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==" if RailsAi.config.stub_responses raise NotImplementedError, "Anthropic doesn't support image generation. Use OpenAI or Gemini for image generation." end |
#generate_speech!(text:, model:, **opts) ⇒ Object
Audio generation - Anthropic doesn't have audio generation
106 107 108 109 |
# File 'lib/rails_ai/providers/anthropic_adapter.rb', line 106 def generate_speech!(text:, model:, **opts) return "data:audio/mp3;base64,SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2ZjU4Ljc2LjEwMAAAAAAAAAAAAAAA//tQxAADB8AhSmAhIIEVWWWU" if RailsAi.config.stub_responses raise NotImplementedError, "Anthropic doesn't support speech generation. Use OpenAI or Gemini for speech generation." end |
#generate_video!(prompt:, model:, **opts) ⇒ Object
Video generation - Anthropic doesn't have video generation
95 96 97 98 |
# File 'lib/rails_ai/providers/anthropic_adapter.rb', line 95 def generate_video!(prompt:, model:, **opts) return "data:video/mp4;base64,AAAAIGZ0eXBpc29tAAACAGlzb21pc28yYXZjMW1wNDEAAAAIZnJlZQAAAB1tZGF0AQAAARxtYXNrAAAAAG1wNDEAAAAAIG1kYXQ=" if RailsAi.config.stub_responses raise NotImplementedError, "Anthropic doesn't support video generation. Use OpenAI or Gemini for video generation." end |
#stream_chat!(messages:, model:, **opts, &on_token) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rails_ai/providers/anthropic_adapter.rb', line 41 def stream_chat!(messages:, model:, **opts, &on_token) return on_token.call("(stubbed stream)") if RailsAi.config.stub_responses # Convert OpenAI format to Anthropic format = () make_streaming_request( "messages", { model: model, max_tokens: opts[:max_tokens] || RailsAi.config.token_limit, messages: , temperature: opts[:temperature] || 1.0, top_p: opts[:top_p] || 1.0, top_k: opts[:top_k] || 0, stop_sequences: opts[:stop_sequences] || [], stream: true, **opts.except(:max_tokens, :temperature, :top_p, :top_k, :stop_sequences, :stream) } ) do |chunk| text = chunk.dig("delta", "text") on_token.call(text) if text end end |
#transcribe_audio!(audio:, model:, **opts) ⇒ Object
111 112 113 114 |
# File 'lib/rails_ai/providers/anthropic_adapter.rb', line 111 def transcribe_audio!(audio:, model:, **opts) return "[stubbed transcription]" if RailsAi.config.stub_responses raise NotImplementedError, "Anthropic doesn't support audio transcription. Use OpenAI or Gemini for audio transcription." end |