Class: OmniAgent::Providers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/omni_agent/providers/base.rb

Direct Known Subclasses

Mock, OpenAI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, model: nil) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/omni_agent/providers/base.rb', line 7

def initialize(api_key: nil, model: nil)
  @api_key = api_key || default_api_key
  @model = model || default_model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/omni_agent/providers/base.rb', line 5

def model
  @model
end

Instance Method Details

#chat(messages:, tools: [], **_options) ⇒ Object

Raises:

  • (NotImplementedError)


12
13
14
# File 'lib/omni_agent/providers/base.rb', line 12

def chat(messages:, tools: [], **_options)
  raise NotImplementedError, "Providers must implement #chat"
end

#validate_messages!(messages, allowed_roles:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/omni_agent/providers/base.rb', line 16

def validate_messages!(messages, allowed_roles:)
  unless messages.is_a?(Array)
    raise OmniAgent::Error, "messages must be an array"
  end

  normalized_roles = Array(allowed_roles).map(&:to_s)

  messages.each_with_index do |message, index|
    unless message.is_a?(Hash)
      raise OmniAgent::Error, "message at index #{index} must be a hash"
    end

    role = message[:role] || message["role"]
    if role.nil?
      raise OmniAgent::Error, "message at index #{index} is missing role"
    end

    role_name = role.to_s
    unless normalized_roles.include?(role_name)
      raise OmniAgent::Error, "invalid message role '#{role_name}' at index #{index}"
    end

    validate_message_payload!(message, index, role_name)
  end
end