Class: N2B::Llm::Claude

Inherits:
Object
  • Object
show all
Defined in:
lib/n2b/llm/claude.rb

Constant Summary collapse

API_URI =
URI.parse('https://api.anthropic.com/v1/messages')

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Claude

Returns a new instance of Claude.



8
9
10
# File 'lib/n2b/llm/claude.rb', line 8

def initialize(config)
  @config = config
end

Instance Method Details

#analyze_code_diff(prompt_content) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/n2b/llm/claude.rb', line 62

def analyze_code_diff(prompt_content)
  # This method assumes prompt_content is the full, ready-to-send prompt
  # including all instructions for the LLM (system message, diff, user additions, JSON format).
  uri = URI.parse('https://api.anthropic.com/v1/messages')
  request = Net::HTTP::Post.new(uri)
  request.content_type = 'application/json'
  request['X-API-Key'] = @config['access_key']
  request['anthropic-version'] = '2023-06-01'

  request.body = JSON.dump({
    "model" => get_model_name,
    "max_tokens" => @config['max_tokens'] || 1024,
    "messages" => [
      {
        "role" => "user",
        "content" => prompt_content
      }
    ]
  })

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(request)
  end

  if response.code != '200'
    raise N2B::LlmApiError.new("LLM API Error: #{response.code} #{response.message} - #{response.body}")
  end

  # Return the raw JSON string. CLI's call_llm_for_diff_analysis will handle parsing.
  # The Claude API for messages returns the analysis in response.body['content'].first['text']
  # which should itself be a JSON string as per our prompt's instructions.
  JSON.parse(response.body)['content'].first['text']
end

#get_model_nameObject



12
13
14
15
16
17
18
19
20
# File 'lib/n2b/llm/claude.rb', line 12

def get_model_name
  # Resolve model name using the centralized configuration
  model_name = N2B::ModelConfig.resolve_model('claude', @config['model'])
  if model_name.nil? || model_name.empty?
    # Fallback to default if no model specified
    model_name = N2B::ModelConfig.resolve_model('claude', N2B::ModelConfig.default_model('claude'))
  end
  model_name
end

#make_request(content) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/n2b/llm/claude.rb', line 22

def make_request( content)
  uri = URI.parse('https://api.anthropic.com/v1/messages')
  request = Net::HTTP::Post.new(uri)
  request.content_type = 'application/json'
  request['X-API-Key'] = @config['access_key']
  request['anthropic-version'] = '2023-06-01'

  request.body = JSON.dump({
    "model" => get_model_name,
    "max_tokens" => 1024,
    "messages" => [
      {
        "role" => "user",
        "content" => content
      }
    ]
  })

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(request)
  end
  # check for errors
  if response.code != '200'
    raise N2B::LlmApiError.new("LLM API Error: #{response.code} #{response.message} - #{response.body}")
  end
  answer = JSON.parse(response.body)['content'].first['text']
  begin
    answer = answer.sub(/.*?\{(.*)\}.*/m, '{\1}') unless answer.start_with?('{')
    answer.gsub!(/"([^"]*)"/) { |match| match.gsub(/\n/, "\\n") }
    answer = JSON.parse(answer)
  rescue JSON::ParserError
    # This specific JSON parsing error is about the LLM's *response content*, not an API error.
    # It should probably be handled differently, but the subtask is about LlmApiError.
    # For now, keeping existing behavior for this part.
    puts "Error parsing JSON from LLM response: #{answer}"
    answer = { 'explanation' => answer} # Default fallback
  end
  answer
end