Sha256: f9042ac36dff3ed27be0809c116c779558cfef0855fbb8741a8acf7ee02c287f

Contents?: true

Size: 1.56 KB

Versions: 4

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require 'faraday'

module GenAI
  class Language
    class Anthropic < Base
      include GenAI::Api::Format::Anthropic

      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

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

      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

      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

      private

      def build_client(token)
        @client = GenAI::Api::Client.new(url: BASE_API_URL, token: nil, headers: {
          'anthropic-beta' => ANTHROPIC_BETA,
          'anthropic-version' => ANTHROPIC_VERSION,
          'x-api-key' => token
        })
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
gen-ai-0.4.3 lib/gen_ai/language/anthropic.rb
gen-ai-0.4.2 lib/gen_ai/language/anthropic.rb
gen-ai-0.4.1 lib/gen_ai/language/anthropic.rb
gen-ai-0.4.0 lib/gen_ai/language/anthropic.rb