Class: ActiveHarness::Providers::XAI

Inherits:
Base
  • Object
show all
Defined in:
lib/active_harness/providers/xai.rb

Overview

xAI (Grok) — OpenAI-compatible API. docs.x.ai/api

Constant Summary

Constants inherited from Base

Base::HTTP, Base::STREAMING_HTTP

Instance Method Summary collapse

Instance Method Details

#call(model:, messages:, temperature: 0.7, stream: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_harness/providers/xai.rb', line 8

def call(model:, messages:, temperature: 0.7, stream: nil)
  headers = { "Content-Type" => "application/json", "Authorization" => "Bearer #{api_key}" }
  body    = { model: model, messages: messages, temperature: temperature }

  return call_streaming(url: config.xai_api_url, headers: headers, body: body, stream: stream, provider: :xai, model: model) if stream

  raw  = post_json(URI(config.xai_api_url), headers: headers, body: body)
  data = parse!(raw)
  handle_error!(data)

  {
    content:  data.dig("choices", 0, "message", "content").to_s.strip,
    provider: :xai,
    model:    data["model"] || model,
    usage:    extract_usage_openai(data)
  }
end