Class: LLM::Conversation

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/conversation.rb

Overview

LLM::Conversation provides a conversation object that maintains a thread of messages that act as the context of the conversation.

Examples:

llm = LLM.openai(key)
bot = llm.chat("What is the capital of France?")
bot.chat("What should we eat in Paris?")
bot.chat("What is the weather like in Paris?")
p bot.messages.map { [_1.role, _1.content] }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider) ⇒ Conversation

Returns a new instance of Conversation.

Parameters:



23
24
25
26
# File 'lib/llm/conversation.rb', line 23

def initialize(provider)
  @provider = provider
  @messages = []
end

Instance Attribute Details

#messagesArray<LLM::Message> (readonly)

Returns:



18
19
20
# File 'lib/llm/conversation.rb', line 18

def messages
  @messages
end

Instance Method Details

#chat(prompt, role = :user, **params) ⇒ LLM::Conversation

Returns:



31
32
33
34
35
36
# File 'lib/llm/conversation.rb', line 31

def chat(prompt, role = :user, **params)
  tap do
    completion = @provider.complete(prompt, role, **params.merge(messages:))
    @messages.concat [Message.new(role, prompt), completion.choices[0]]
  end
end