Class: TurnKit::Adapters::RubyLLM

Inherits:
Client
  • Object
show all
Defined in:
lib/turnkit/adapters/ruby_llm.rb

Constant Summary collapse

KEY_BY_PROVIDER =
{
  openai: "OPENAI_API_KEY",
  gemini: "GEMINI_API_KEY",
  anthropic: "ANTHROPIC_API_KEY",
  openrouter: "OPENROUTER_API_KEY"
}.freeze

Instance Method Summary collapse

Instance Method Details

#chat(model:, messages:, tools:, instructions:, temperature: nil, thinking: nil, output_schema: nil, metadata: nil, on_event: nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/turnkit/adapters/ruby_llm.rb', line 27

def chat(model:, messages:, tools:, instructions:, temperature: nil, thinking: nil, output_schema: nil, metadata: nil, on_event: nil)
  require "ruby_llm"

  configure_from_environment

  chat = ::RubyLLM.chat(model: model)
  add_instructions(chat, instructions, model: model)
  chat.with_temperature(temperature) if temperature
  apply_thinking(chat, thinking)
  chat.with_schema(normalize_schema(output_schema)) if output_schema
  Array(tools).each { |tool| chat.with_tool(ruby_llm_tool(tool)) }
  Array(messages).each { |message| add_message(chat, message) }

  response = complete_without_tool_execution(chat)
  normalize_response(response, model: model)
end

#validate!(model:) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/turnkit/adapters/ruby_llm.rb', line 13

def validate!(model:)
  require "ruby_llm"

  raise ModelAccessError, "model is required" if model.to_s.empty?

  configure_from_environment
  provider = provider_for(model)
  key_name = KEY_BY_PROVIDER[provider]
  return true unless key_name
  return true if ENV[key_name].to_s != "" || config_key_present?(provider)

  raise ModelAccessError, "#{key_name} is required for #{model}. Set ENV[#{key_name.inspect}] or configure RubyLLM before running TurnKit."
end