Class: Qualspec::Client
- Inherits:
-
Object
- Object
- Qualspec::Client
- Defined in:
- lib/qualspec/client.rb
Defined Under Namespace
Classes: RequestError, Response
Instance Method Summary collapse
- #chat(model:, messages:, json_mode: true, with_metadata: false, temperature: nil) ⇒ Object
-
#initialize(config = Qualspec.configuration) ⇒ Client
constructor
A new instance of Client.
- #validate_api_key! ⇒ Object
Constructor Details
#initialize(config = Qualspec.configuration) ⇒ Client
Returns a new instance of Client.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/qualspec/client.rb', line 32 def initialize(config = Qualspec.configuration) @config = config validate_api_key! @conn = Faraday.new(url: config.api_url) do |f| f.request :json f.response :json f.headers = config.api_headers f..timeout = config.request_timeout f..open_timeout = 10 # SSL verification enabled by default, disable with QUALSPEC_SSL_VERIFY=false f.ssl.verify = ENV['QUALSPEC_SSL_VERIFY'] != 'false' f.adapter Faraday.default_adapter end end |
Instance Method Details
#chat(model:, messages:, json_mode: true, with_metadata: false, temperature: nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/qualspec/client.rb', line 61 def chat(model:, messages:, json_mode: true, with_metadata: false, temperature: nil) payload = { model: model, messages: } # Request structured JSON output payload[:response_format] = { type: 'json_object' } if json_mode # Set temperature if provided payload[:temperature] = temperature if temperature start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) response = @conn.post('chat/completions', payload) duration_ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time) * 1000).round handle_response(response, duration_ms, ) end |
#validate_api_key! ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/qualspec/client.rb', line 50 def validate_api_key! # Skip during VCR playback (VCR may not be loaded) return if defined?(VCR) && VCR.current_cassette && !VCR.current_cassette.recording? return if @config.api_key_configured? raise Qualspec::Error, <<~MSG.strip QUALSPEC_API_KEY is required but not set. Set it via environment variable or Qualspec.configure { |c| c.api_key = '...' } MSG end |