Class: GenAI::Language::Anthropic

Inherits:
Base
  • Object
show all
Includes:
Api::Format::Anthropic
Defined in:
lib/gen_ai/language/anthropic.rb

Constant Summary collapse

BASE_API_URL =
'https://api.anthropic.com'
ANTHROPIC_VERSION =
'2023-06-01'
ANTHROPIC_BETA =
'messages-2023-12-15'
COMPLETION_MODEL =
'claude-2.1'
DEFAULT_MAX_TOKENS =
1024

Constants inherited from Base

Base::DEFAULT_ROLE

Instance Method Summary collapse

Methods included from Api::Format::Anthropic

#extract_completions, #format_messages

Methods inherited from Base

#embed

Methods included from Dependency

#depends_on

Constructor Details

#initialize(token:, options: {}) ⇒ Anthropic

Returns a new instance of Anthropic.



16
17
18
19
# File 'lib/gen_ai/language/anthropic.rb', line 16

def initialize(token:, options: {})
  @token = token
  build_client(token)
end

Instance Method Details

#chat(messages, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/gen_ai/language/anthropic.rb', line 31

def chat(messages, options = {})
  response = client.post '/v1/messages', {
    messages: format_messages(messages),
    model: options.delete(:model) || COMPLETION_MODEL,
    max_tokens: options.delete(:max_tokens) || DEFAULT_MAX_TOKENS
  }.merge(options)

  build_result(model: COMPLETION_MODEL, raw: response, parsed: extract_completions(response))
end

#complete(prompt, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/gen_ai/language/anthropic.rb', line 21

def complete(prompt, options = {})
  response = client.post '/v1/complete', {
    prompt: "\n\nHuman: #{prompt}\n\nAssistant:",
    model: options.delete(:model) || COMPLETION_MODEL,
    max_tokens_to_sample: options.delete(:max_tokens_to_sample) || DEFAULT_MAX_TOKENS
  }.merge(options)

  build_result(model: COMPLETION_MODEL, raw: response, parsed: extract_completions(response))
end