Class: Docit::Ai::AnthropicClient

Inherits:
Object
  • Object
show all
Defined in:
lib/docit/ai/anthropic_client.rb

Constant Summary collapse

API_URL =
"https://api.anthropic.com/v1/messages"
API_VERSION =
"2023-06-01"

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, model:) ⇒ AnthropicClient

Returns a new instance of AnthropicClient.



13
14
15
16
# File 'lib/docit/ai/anthropic_client.rb', line 13

def initialize(api_key:, model:)
  @api_key = api_key
  @model = model
end

Instance Method Details

#generate(prompt) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/docit/ai/anthropic_client.rb', line 18

def generate(prompt)
  uri = URI(API_URL)
  request = Net::HTTP::Post.new(uri)
  request["x-api-key"] = @api_key
  request["anthropic-version"] = API_VERSION
  request["Content-Type"] = "application/json"
  request.body = {
    model: @model,
    max_tokens: 4096,
    messages: [{ role: "user", content: prompt }]
  }.to_json

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

  handle_response(response)
end