Sha256: d4cafe770a89358f8b75538d3ead2b7cdccabb4a0a426f83781e7aba56893c42

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require 'httpx'

module Vercon
  class Claude
    BASE_URL = 'https://api.anthropic.com'

    def initialize
      config = Vercon::Config.new

      @api_token = config.token
      @claude_model = config.claude_model
    end

    def submit(model: nil, system: nil, max_tokens: 4096, temperature: 0.2, stop_sequences: nil, user: nil, # rubocop:disable Metrics/ParameterLists
               messages: nil)
      body = {
        model: model || @claude_model,
        system: system,
        max_tokens: max_tokens,
        temperature: temperature,
        stop_sequences: stop_sequences,
        messages: messages || [{ role: 'user', content: user }]
      }.reject { |_, v| v.nil? || v == '' }

      client.post('/v1/messages', body: body.to_json).then { |res| prepare_response(res.json) }
    end

    private

    def extra_headers
      { 'x-api-key' => @api_token, 'anthropic-version' => '2023-06-01' }
    end

    def client
      @client ||=
        HTTPX
        .plugin(:retries)
        .with(headers: { 'Content-Type' => 'application/json', 'Cache-Control' => 'no-cache' })
        .with(headers: extra_headers)
        .with(origin: BASE_URL)
        .with(ssl: { alpn_protocols: %w[http/1.1] })
        .with(timeout: { keep_alive_timeout: 180 })
    end

    def prepare_response(response)
      return { error: response.dig('error', 'message') } if response['type'] == 'error'

      { text: response.dig('content', 0, 'text') }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vercon-0.0.1 lib/vercon/claude.rb