Sha256: eae03276fb780a32dc0c84b7e82af30ccb85ba7f3533032bed95dc2b777dc6b8

Contents?: true

Size: 1.58 KB

Versions: 16

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Luo
  class Xinghuo
    include Configurable

    setting :access_token, default: ENV.fetch('XINGHUO_ACCESS_TOKEN')
    setting :retries, default: ENV.fetch('XINGHUO_REQUEST_RETRIES', 3).to_i
    setting :host, default: ENV.fetch('XINGHUO_HOST', 'https://integration-api.iflyos.cn/')
    setting :history_limit, default: ENV.fetch('XINGHUO_LIMIT_HISTORY', '6').to_i * 2
    setting :temperature, default: ENV.fetch('XINGHUO_TEMPERATURE', 0).to_i
    setting :auditing, default: 'default'
    setting :domain, default: 'general'
    setting :max_tokens, default: 1024
    setting :uid, default: -> { SecureRandom.hex(16) }

    include HttpClient.init_client

    PARAMS = Dry::Schema.Params do
      required(:auditing).filled(:string)
      required(:messages).filled(:array)
      optional(:domain).maybe(:string)
      optional(:max_tokens).maybe(:integer)
      optional(:random_threshold).maybe(:float)
    end

    # header uid max length is 32 todo

    def request_chat(params)
      client.post('/external/ls_log/xf_completions', params.to_h)
    end

    def chat(messages, temperature: nil)
      if messages.is_a?(Messages)
        messages = messages.to_a
      end
      params = PARAMS.call(
        auditing: config.auditing,
        domain: config.domain,
        messages: messages,
        max_tokens: config.max_tokens,
        random_threshold: temperature || config.temperature,
        uid: config.uid.call
      )
      return params.errors unless params.success?
      request_chat(params).body.dig('choices', 0, 'message', 'content')
    end

  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
luo-0.1.25 lib/luo/xinghuo.rb
luo-0.1.24 lib/luo/xinghuo.rb
luo-0.1.23 lib/luo/xinghuo.rb
luo-0.1.22 lib/luo/xinghuo.rb
luo-0.1.21 lib/luo/xinghuo.rb
luo-0.1.20 lib/luo/xinghuo.rb
luo-0.1.18 lib/luo/xinghuo.rb
luo-0.1.17 lib/luo/xinghuo.rb
luo-0.1.16 lib/luo/xinghuo.rb
luo-0.1.15 lib/luo/xinghuo.rb
luo-0.1.14 lib/luo/xinghuo.rb
luo-0.1.13 lib/luo/xinghuo.rb
luo-0.1.12 lib/luo/xinghuo.rb
luo-0.1.11 lib/luo/xinghuo.rb
luo-0.1.10 lib/luo/xinghuo.rb
luo-0.1.9 lib/luo/xinghuo.rb