Class: RailsAi::Providers::SecureAnthropicAdapter
- Defined in:
- lib/rails_ai/providers/secure_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
- #chat!(messages:, model:, **opts) ⇒ Object
-
#initialize ⇒ SecureAnthropicAdapter
constructor
A new instance of SecureAnthropicAdapter.
- #stream_chat!(messages:, model:, **opts, &on_token) ⇒ Object
Methods inherited from Base
#analyze_video!, #create_variation!, #edit_image!, #edit_video!, #embed!, #generate_image!, #generate_speech!, #generate_video!, #transcribe_audio!
Constructor Details
#initialize ⇒ SecureAnthropicAdapter
Returns a new instance of SecureAnthropicAdapter.
12 13 14 15 16 |
# File 'lib/rails_ai/providers/secure_anthropic_adapter.rb', line 12 def initialize @api_key = Security::APIKeyManager.secure_fetch("ANTHROPIC_API_KEY") @rate_limiter = Security::RateLimiter.new(limit: 50, window: 3600) super end |
Instance Method Details
#analyze_image!(image:, prompt:, model: "claude-3-5-sonnet-20241022", **opts) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/rails_ai/providers/secure_anthropic_adapter.rb', line 78 def analyze_image!(image:, prompt:, model: "claude-3-5-sonnet-20241022", **opts) return "(stubbed analysis)" if RailsAi.config.stub_responses # Security validations Security::InputValidator.validate_text_input(prompt, max_length: 1000) # Rate limiting @rate_limiter.check_limit("vision_#{RailsAi::Context.user_id}") # Sanitize prompt sanitized_prompt = Security::ContentSanitizer.sanitize_content(prompt) # Prepare image securely image_data = prepare_image_securely(image) response = make_request( "messages", { model: model, max_tokens: opts[:max_tokens] || 1000, messages: [ { role: "user", content: [ { type: "text", text: sanitized_prompt }, { type: "image", source: { type: "base64", media_type: "image/png", data: image_data } } ] } ], **opts.except(:max_tokens) } ) response.dig("content", 0, "text") end |
#chat!(messages:, model:, **opts) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rails_ai/providers/secure_anthropic_adapter.rb', line 18 def chat!(messages:, model:, **opts) return "(stubbed) #{.last[:content]}" if RailsAi.config.stub_responses # Security validations Security::InputValidator.() # Rate limiting @rate_limiter.check_limit("chat_#{RailsAi::Context.user_id}") # Sanitize content = .map do || { role: [:role], content: Security::ContentSanitizer.sanitize_content([:content]) } end response = make_request( "messages", { model: model, max_tokens: opts[:max_tokens] || RailsAi.config.token_limit, messages: , **opts.except(:max_tokens) } ) response.dig("content", 0, "text") end |
#stream_chat!(messages:, model:, **opts, &on_token) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rails_ai/providers/secure_anthropic_adapter.rb', line 48 def stream_chat!(messages:, model:, **opts, &on_token) return on_token.call("(stubbed stream)") if RailsAi.config.stub_responses # Security validations Security::InputValidator.() # Rate limiting @rate_limiter.check_limit("stream_#{RailsAi::Context.user_id}") # Sanitize content = .map do || { role: [:role], content: Security::ContentSanitizer.sanitize_content([:content]) } end make_streaming_request( "messages", { model: model, max_tokens: opts[:max_tokens] || RailsAi.config.token_limit, messages: , stream: true, **opts.except(:max_tokens, :stream) }, &on_token ) end |