Class: Regent::LLM

Inherits:
Object
  • Object
show all
Defined in:
lib/regent/llm.rb,
lib/regent/llm/base.rb,
lib/regent/llm/gemini.rb,
lib/regent/llm/ollama.rb,
lib/regent/llm/open_ai.rb,
lib/regent/llm/anthropic.rb,
lib/regent/llm/open_router.rb

Defined Under Namespace

Classes: APIKeyNotFoundError, Anthropic, ApiError, Base, Gemini, Ollama, OpenAI, OpenRouter, ProviderNotFoundError, Result

Constant Summary collapse

DEFAULT_RETRY_COUNT =
3
PROVIDER_PATTERNS =
{
  OpenAI: /^gpt-/,
  Gemini: /^gemini-/,
  Anthropic: /^claude-/
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, strict_mode: true, **options) ⇒ LLM



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/regent/llm.rb', line 16

def initialize(model, strict_mode: true, **options)
  @strict_mode = strict_mode
  @options = options
  if model.class.ancestors.include?(Regent::LLM::Base)
    @model = model.model
    @provider = model
  else
    @model = model
    @provider = instantiate_provider
  end
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



28
29
30
# File 'lib/regent/llm.rb', line 28

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



28
29
30
# File 'lib/regent/llm.rb', line 28

def options
  @options
end

Instance Method Details

#invoke(messages, **args) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/regent/llm.rb', line 30

def invoke(messages, **args)
  retries = 0

  messages = [{ role: "user", content: messages }] if messages.is_a?(String)

  provider.invoke(messages, **args)

rescue Faraday::Error, ApiError => error
  if error.respond_to?(:retryable?) && error.retryable? && retries < DEFAULT_RETRY_COUNT
    sleep(exponential_backoff(retries))
    retry
  end
  handle_error(error)
end