Class: Intelligent::Llm::Anthropic
- Defined in:
- lib/intelligent/llm/anthropic.rb
Instance Attribute Summary
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
- #generate(prompt_content, files = []) ⇒ Object
-
#initialize(model: self.class.default_model) ⇒ Anthropic
constructor
A new instance of Anthropic.
Constructor Details
#initialize(model: self.class.default_model) ⇒ Anthropic
Returns a new instance of Anthropic.
20 21 22 23 |
# File 'lib/intelligent/llm/anthropic.rb', line 20 def initialize(model: self.class.default_model) super(provider: "anthropic", model: model) @client = ::Anthropic::Client.new(api_key: api_key) end |
Class Method Details
.available_models ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/intelligent/llm/anthropic.rb', line 7 def self.available_models { "claude-sonnet-4-20250514" => "Claude Sonnet 4 (Recommended)", "claude-opus-4-20250514" => "Claude Opus 4 (Most Capable)", "claude-3-7-sonnet-20250219" => "Claude Sonnet 3.7 (Fast)", "claude-3-5-haiku-20241022" => "Claude Haiku 3.5 (Fastest)" } end |
.default_model ⇒ Object
16 17 18 |
# File 'lib/intelligent/llm/anthropic.rb', line 16 def self.default_model "claude-sonnet-4-20250514" end |
Instance Method Details
#generate(prompt_content, files = []) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/intelligent/llm/anthropic.rb', line 25 def generate(prompt_content, files = []) raise ArgumentError, "API key not configured" unless api_key_present? # Prepare message content content = [ { type: "text", text: prompt_content } ] # Add files to content if provided if files && !files.empty? files.each do |file| file_content = prepare_file_content(file) content << file_content if file_content end end begin response = @client..create( model: model, max_tokens: 4000, messages: [ { role: "user", content: content } ] ) { success: true, text: response.content.first.text, usage: response.usage&.to_h } rescue ::Anthropic::Errors::APIError => e { error: "API request failed: #{e.}", status: e.status } rescue => e { error: "Request failed: #{e.}" } end end |