Class: LLM::Bot
- Inherits:
-
Object
- Object
- LLM::Bot
- Defined in:
- lib/llm/bot.rb,
lib/llm/bot/builder.rb,
lib/llm/bot/conversable.rb
Overview
LLM::Bot provides a bot object that can maintain a a conversation. A conversation can use the chat completions API that all LLM providers support or the responses API that a select few LLM providers support.
Defined Under Namespace
Modules: Prompt
Constant Summary
Constants included from LLM
FormatError, PromptError, RateLimitError, UnauthorizedError, VERSION
Instance Attribute Summary collapse
-
#messages ⇒ LLM::Buffer<LLM::Message>
readonly
Returns an Enumerable for the messages in a conversation.
Instance Method Summary collapse
-
#initialize(provider, params = {}) ⇒ Bot
constructor
A new instance of Bot.
-
#chat(prompt = nil, params = {}) ⇒ Object
Maintain a conversation via the chat completions API.
-
#respond(prompt = nil, params = {}) ⇒ Object
Maintain a conversation via the responses API.
-
#inspect ⇒ String
-
#functions ⇒ Array<LLM::Function>
Returns an array of functions that can be called.
Methods included from LLM
File, anthropic, deepseek, function, functions, gemini, llamacpp, ollama, openai, voyageai
Constructor Details
Instance Attribute Details
#messages ⇒ LLM::Buffer<LLM::Message> (readonly)
Returns an Enumerable for the messages in a conversation
47 48 49 |
# File 'lib/llm/bot.rb', line 47 def @messages end |
Instance Method Details
#chat(prompt, params = {}) ⇒ LLM::Bot #chat(prompt, params) { ... } ⇒ LLM::Buffer
Maintain a conversation via the chat completions API
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/llm/bot.rb', line 78 def chat(prompt = nil, params = {}) if block_given? params = prompt yield Prompt::Completion.new(self, params) elsif prompt.nil? raise ArgumentError, "wrong number of arguments (given 0, expected 1)" else params = {role: :user}.merge!(params) tap { async_completion(prompt, params) } end end |
#respond(prompt, params = {}) ⇒ LLM::Bot #respond(prompt, params) { ... } ⇒ LLM::Buffer
Maintain a conversation via the responses API
105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/llm/bot.rb', line 105 def respond(prompt = nil, params = {}) if block_given? params = prompt yield Prompt::Respond.new(self, params) elsif prompt.nil? raise ArgumentError, "wrong number of arguments (given 0, expected 1)" else params = {role: :user}.merge!(params) tap { async_response(prompt, params) } end end |
#inspect ⇒ String
120 121 122 123 124 |
# File 'lib/llm/bot.rb', line 120 def inspect "#<#{self.class.name}:0x#{object_id.to_s(16)} " \ "@provider=#{@provider.class}, @params=#{@params.inspect}, " \ "@messages=#{@messages.inspect}>" end |
#functions ⇒ Array<LLM::Function>
Returns an array of functions that can be called
129 130 131 132 133 134 |
# File 'lib/llm/bot.rb', line 129 def functions .select(&:assistant?) .flat_map(&:functions) .select(&:pending?) end |