Class: LLM::Bot
- Inherits:
-
Object
- Object
- LLM::Bot
- Includes:
- Builder, Conversable
- Defined in:
- lib/llm/bot.rb,
lib/llm/bot/builder.rb,
lib/llm/bot/conversable.rb
Overview
LLM::Bot provides an 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 currently only OpenAI supports.
Defined Under Namespace
Modules: Builder, Conversable, Prompt
Instance Attribute Summary collapse
-
#messages ⇒ LLM::Buffer<LLM::Message>
readonly
Returns an Enumerable for the messages in a conversation.
Instance Method Summary collapse
-
#chat(prompt = nil, params = {}) ⇒ Object
Maintain a conversation via the chat completions API.
-
#functions ⇒ Array<LLM::Function>
Returns an array of functions that can be called.
-
#initialize(provider, params = {}) ⇒ Bot
constructor
A new instance of Bot.
- #inspect ⇒ String
-
#respond(prompt = nil, params = {}) ⇒ Object
Maintain a conversation via the responses API.
Constructor Details
Instance Attribute Details
#messages ⇒ LLM::Buffer<LLM::Message> (readonly)
Returns an Enumerable for the messages in a conversation
45 46 47 |
# File 'lib/llm/bot.rb', line 45 def @messages end |
Instance Method Details
#chat(prompt, params = {}) ⇒ LLM::Bot #chat(prompt, params) { ... } ⇒ LLM::Buffer
Maintain a conversation via the chat completions API
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/llm/bot.rb', line 76 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 |
#functions ⇒ Array<LLM::Function>
Returns an array of functions that can be called
127 128 129 130 131 132 |
# File 'lib/llm/bot.rb', line 127 def functions .select(&:assistant?) .flat_map(&:functions) .select(&:pending?) end |
#inspect ⇒ String
118 119 120 121 122 |
# File 'lib/llm/bot.rb', line 118 def inspect "#<#{self.class.name}:0x#{object_id.to_s(16)} " \ "@provider=#{@provider.class}, @params=#{@params.inspect}, " \ "@messages=#{@messages.inspect}>" end |
#respond(prompt, params = {}) ⇒ LLM::Bot #respond(prompt, params) { ... } ⇒ LLM::Buffer
Maintain a conversation via the responses API
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/llm/bot.rb', line 103 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 |