Class: AiClient::XAI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ai_client/xai.rb

Constant Summary collapse

BASE_URI =

Replace with actual xAI API endpoint

'https://api.x.ai/v1'

Instance Method Summary collapse

Constructor Details

#initialize(api_key: ENV['XAI_API_KEY']) ⇒ Client

Returns a new instance of Client.



8
9
10
11
# File 'lib/ai_client/xai.rb', line 8

def initialize(api_key: ENV['XAI_API_KEY'])
  @api_key = api_key
  @connection = OmniAI::HTTP::Connection.new(BASE_URI)
end

Instance Method Details

#chat(prompt:, model: 'grok3', **options) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ai_client/xai.rb', line 13

def chat(prompt:, model: 'grok3', **options)
  response = @connection.post(
    '/chat/completions', # Adjust endpoint based on xAI API docs
    headers: { 'Authorization' => "Bearer #{@api_key}", 'Content-Type' => 'application/json' },
    body: {
      model: model,
      messages: [{ role: 'user', content: prompt }],
      temperature: options[:temperature] || 0.7,
      max_tokens: options[:max_tokens] || 1024
    }.to_json
  )
  parse_response(response)
end