Class: Nanobot::Providers::RubyLLMProvider
- Inherits:
-
LLMProvider
- Object
- LLMProvider
- Nanobot::Providers::RubyLLMProvider
- Defined in:
- lib/nanobot/providers/rubyllm_provider.rb
Overview
LLM provider implementation backed by the RubyLLM gem. Supports all RubyLLM backends (Anthropic, OpenAI, Gemini, DeepSeek, OpenRouter, Mistral, Perplexity, xAI, Ollama, GPUStack, Bedrock, etc.).
Defined Under Namespace
Classes: ToolCallProxy
Constant Summary collapse
- PROVIDER_KEY_MAP =
Maps nanobot provider names to their RubyLLM api_key config attribute. Providers not listed here (or mapped to nil) have no api_key setting.
{ 'anthropic' => :anthropic_api_key, 'openai' => :openai_api_key, 'gemini' => :gemini_api_key, 'deepseek' => :deepseek_api_key, 'openrouter' => :openrouter_api_key, 'mistral' => :mistral_api_key, 'perplexity' => :perplexity_api_key, 'xai' => :xai_api_key, 'gpustack' => :gpustack_api_key, 'bedrock' => :bedrock_api_key, 'groq' => :openai_api_key }.freeze
- PROVIDER_BASE_MAP =
Maps nanobot provider names to their RubyLLM api_base config attribute. Only providers that support a custom base URL are listed.
{ 'openai' => :openai_api_base, 'gemini' => :gemini_api_base, 'ollama' => :ollama_api_base, 'gpustack' => :gpustack_api_base }.freeze
Instance Attribute Summary collapse
-
#default_model ⇒ Object
readonly
Returns the value of attribute default_model.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
-
#chat(messages:, tools: nil, model: nil, max_tokens: 4096, temperature: 0.7) ⇒ LLMResponse
Chat with tools using RubyLLM's provider API directly This bypasses RubyLLM's automatic tool execution to give us control.
-
#initialize(api_key: nil, api_base: nil, default_model: nil, provider: 'anthropic', logger: nil) ⇒ RubyLLMProvider
constructor
A new instance of RubyLLMProvider.
Constructor Details
#initialize(api_key: nil, api_base: nil, default_model: nil, provider: 'anthropic', logger: nil) ⇒ RubyLLMProvider
Returns a new instance of RubyLLMProvider.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/nanobot/providers/rubyllm_provider.rb', line 49 def initialize(api_key: nil, api_base: nil, default_model: nil, provider: 'anthropic', logger: nil) super() @api_key = api_key @api_base = api_base @provider = provider.to_s @default_model = default_model || 'claude-haiku-4-5' @logger = logger || Logger.new(IO::NULL) @chat = nil configure_client end |
Instance Attribute Details
#default_model ⇒ Object (readonly)
Returns the value of attribute default_model.
42 43 44 |
# File 'lib/nanobot/providers/rubyllm_provider.rb', line 42 def default_model @default_model end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
42 43 44 |
# File 'lib/nanobot/providers/rubyllm_provider.rb', line 42 def logger @logger end |
Instance Method Details
#chat(messages:, tools: nil, model: nil, max_tokens: 4096, temperature: 0.7) ⇒ LLMResponse
Chat with tools using RubyLLM's provider API directly This bypasses RubyLLM's automatic tool execution to give us control
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/nanobot/providers/rubyllm_provider.rb', line 69 def chat(messages:, tools: nil, model: nil, max_tokens: 4096, temperature: 0.7) model_to_use = model || @default_model log_request(model_to_use, temperature, max_tokens, , tools) response = call_provider(model_to_use, , tools, temperature) log_response(response) LLMResponse.new(content: response.content, tool_calls: convert_tool_calls(response), finish_reason: 'stop') rescue StandardError => e @logger.error "Error calling LLM: #{e.}" @logger.error e.backtrace.join("\n") LLMResponse.new(content: "Error calling LLM: #{e.}", tool_calls: [], finish_reason: 'error') end |