Class: Octoryn::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/octoryn/client.rb

Overview

High-level governed client for text, tools, streaming, and JSON Schema output.

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: 'https://api.octoryn.dev/v1/', transport: nil) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
# File 'lib/octoryn/client.rb', line 9

def initialize(api_key:, base_url: 'https://api.octoryn.dev/v1/',
               transport: nil)
  raise ArgumentError, 'Octoryn API key is required' if api_key.to_s.strip.empty?

  @transport = transport || NetHTTPTransport.new(
    base_url: base_url,
    api_key: api_key
  )
end

Instance Method Details

#generate_object(schema:, schema_name: 'response', schema_description: nil, **options) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/octoryn/client.rb', line 28

def generate_object(schema:, schema_name: 'response',
                    schema_description: nil, **options)
  payload = build_request(options, stream: false)
  payload['response_format'] = {
    'type' => 'json_schema',
    'json_schema' => {
      'name' => schema_name,
      'description' => schema_description,
      'strict' => true,
      'schema' => schema
    }
  }
  response = request(payload)
  result = normalize(JSON.parse(response.body), governance(response))
  object = parse_structured(result.text)
  errors = JSONSchemer.schema(schema).validate(object).to_a
  unless errors.empty?
    raise StructuredOutputError.new(
      'Octoryn structured output does not match the JSON Schema',
      raw_output: result.text,
      validation_errors: errors
    )
  end
  ObjectResult.new(object, result)
end

#generate_text(**options) ⇒ Object



19
20
21
22
# File 'lib/octoryn/client.rb', line 19

def generate_text(**options)
  response = request(build_request(options, stream: false))
  normalize(JSON.parse(response.body), governance(response))
end

#generate_text_async(**options) ⇒ Object



24
25
26
# File 'lib/octoryn/client.rb', line 24

def generate_text_async(**options)
  Thread.new { generate_text(**options) }
end

#stream_text(**options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/octoryn/client.rb', line 54

def stream_text(**options)
  payload = build_request(options, stream: true)
  producer = proc do |&on_chunk|
    response = @transport.post('chat/completions', payload, stream: true) do |head, chunk|
      if chunk.nil?
        ensure_success!(head)
        on_chunk.call(nil, governance(head))
      else
        on_chunk.call(chunk, nil)
      end
    end
    ensure_success!(response)
  end
  TextStream.new(&producer)
end