Class: OllamaAgent::Providers::Base

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

Overview

Abstract base class for model providers.

Every provider must implement:

#chat(messages:, model:, tools: nil, stream_hooks: nil, **opts) → Response
#available? → Boolean

A Response must respond to:

#message    → { role:, content:, tool_calls: }
#usage      → { prompt_tokens:, completion_tokens:, total_tokens: } or nil

Direct Known Subclasses

Anthropic, Ollama, OpenAI, Router

Defined Under Namespace

Classes: Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, **options) ⇒ Base

Returns a new instance of Base.



44
45
46
47
# File 'lib/ollama_agent/providers/base.rb', line 44

def initialize(name:, **options)
  @name    = name.to_s
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



42
43
44
# File 'lib/ollama_agent/providers/base.rb', line 42

def name
  @name
end

Instance Method Details

#available?Boolean

This method is abstract.

Returns true when the provider can accept requests right now.

Returns:

  • (Boolean)

    true when the provider can accept requests right now

Raises:

  • (NotImplementedError)


61
62
63
# File 'lib/ollama_agent/providers/base.rb', line 61

def available?
  raise NotImplementedError, "#{self.class}#available? is not implemented"
end

#chat(messages:, model:, tools: nil, stream_hooks: nil, **opts) ⇒ Response

This method is abstract.

Parameters:

  • messages (Array<Hash>)

    conversation history

  • model (String)

    model identifier

  • tools (Array<Hash>) (defaults to: nil)

    tool schemas (nil = no tools)

  • stream_hooks (Hash) (defaults to: nil)

    optional :on_token, :on_thinking lambdas

Returns:

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/ollama_agent/providers/base.rb', line 55

def chat(messages:, model:, tools: nil, stream_hooks: nil, **opts)
  raise NotImplementedError, "#{self.class}#chat is not implemented"
end

#estimate_cost(input_tokens:, output_tokens:) ⇒ Object

Returns approximate cost in USD for the given token counts. Subclasses may override with provider-specific pricing.



72
73
74
# File 'lib/ollama_agent/providers/base.rb', line 72

def estimate_cost(input_tokens:, output_tokens:)
  0.0
end

#streaming_supported?Boolean

Override in subclasses that support streaming natively.

Returns:

  • (Boolean)


66
67
68
# File 'lib/ollama_agent/providers/base.rb', line 66

def streaming_supported?
  false
end

#to_sObject



76
77
78
# File 'lib/ollama_agent/providers/base.rb', line 76

def to_s
  "#<#{self.class.name} name=#{@name}>"
end