Class: Lyman::Conversation
- Inherits:
-
Object
- Object
- Lyman::Conversation
- Defined in:
- lib/lyman/conversation.rb
Overview
The item that flows through the spine and the circuit: the whole conversation so far, plus the control data workers consult to decide whether to act (never which of several things to do).
Messages use string keys throughout, for clean round-tripping with OpenAI-compatible wire formats.
Instance Attribute Summary collapse
-
#max_rounds ⇒ Object
Returns the value of attribute max_rounds.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#rounds ⇒ Object
Returns the value of attribute rounds.
Instance Method Summary collapse
- #add_assistant_message(message) ⇒ Object
- #add_tool_result(tool_call_id, content) ⇒ Object
- #add_user_message(text) ⇒ Object
- #finish! ⇒ Object
- #finished? ⇒ Boolean
-
#initialize(system_prompt: nil, max_rounds: 10) ⇒ Conversation
constructor
A new instance of Conversation.
- #last_assistant_content ⇒ Object
- #pending_tool_calls ⇒ Object
- #runaway? ⇒ Boolean
Constructor Details
#initialize(system_prompt: nil, max_rounds: 10) ⇒ Conversation
Returns a new instance of Conversation.
12 13 14 15 16 17 18 |
# File 'lib/lyman/conversation.rb', line 12 def initialize(system_prompt: nil, max_rounds: 10) @messages = [] @messages << {"role" => "system", "content" => system_prompt} if system_prompt @rounds = 0 @max_rounds = max_rounds @finished = false end |
Instance Attribute Details
#max_rounds ⇒ Object
Returns the value of attribute max_rounds.
10 11 12 |
# File 'lib/lyman/conversation.rb', line 10 def max_rounds @max_rounds end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
9 10 11 |
# File 'lib/lyman/conversation.rb', line 9 def @messages end |
#rounds ⇒ Object
Returns the value of attribute rounds.
10 11 12 |
# File 'lib/lyman/conversation.rb', line 10 def rounds @rounds end |
Instance Method Details
#add_assistant_message(message) ⇒ Object
27 28 29 30 |
# File 'lib/lyman/conversation.rb', line 27 def () @messages << self end |
#add_tool_result(tool_call_id, content) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/lyman/conversation.rb', line 32 def add_tool_result(tool_call_id, content) @messages << { "role" => "tool", "tool_call_id" => tool_call_id, "content" => content } self end |
#add_user_message(text) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/lyman/conversation.rb', line 20 def (text) @messages << {"role" => "user", "content" => text} @finished = false @rounds = 0 self end |
#finish! ⇒ Object
52 53 54 55 |
# File 'lib/lyman/conversation.rb', line 52 def finish! @finished = true self end |
#finished? ⇒ Boolean
57 58 59 |
# File 'lib/lyman/conversation.rb', line 57 def finished? @finished end |
#last_assistant_content ⇒ Object
47 48 49 50 |
# File 'lib/lyman/conversation.rb', line 47 def last_assistant_content = @messages.rfind { |m| m["role"] == "assistant" } && ["content"] end |
#pending_tool_calls ⇒ Object
41 42 43 44 45 |
# File 'lib/lyman/conversation.rb', line 41 def pending_tool_calls last = @messages.last return [] unless last && last["role"] == "assistant" last["tool_calls"] || [] end |
#runaway? ⇒ Boolean
61 62 63 |
# File 'lib/lyman/conversation.rb', line 61 def runaway? @rounds >= @max_rounds end |