Class: LLM::Prompt
- Inherits:
-
Object
- Object
- LLM::Prompt
- Defined in:
- lib/llm/prompt.rb
Overview
LLM::Prompt is a small object for composing a single request from multiple role-aware messages. A prompt is not just a string. It is an ordered chain of messages with explicit roles (for example ‘system` and `user`). Use Context#prompt when building a prompt inside a session. Use `LLM::Prompt.new(provider)` directly when you want to construct or pass prompt objects around explicitly.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Returns true when two prompts have the same buffer.
- #developer(content) ⇒ void
-
#initialize(provider, &b) ⇒ Prompt
constructor
A new instance of Prompt.
- #system(content) ⇒ void
- #talk(content, role: @provider.user_role) ⇒ void (also: #chat)
-
#to_a ⇒ Array<LLM::Message>
Returns the prompt messages in order.
- #user(content) ⇒ void
Constructor Details
#initialize(provider, &b) ⇒ Prompt
Returns a new instance of Prompt.
30 31 32 33 34 35 36 |
# File 'lib/llm/prompt.rb', line 30 def initialize(provider, &b) @provider = provider @buffer = [] unless b.nil? (b.arity == 1) ? b.call(self) : instance_eval(&b) end end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Returns true when two prompts have the same buffer
90 91 92 93 |
# File 'lib/llm/prompt.rb', line 90 def ==(other) return false unless LLM::Prompt === other @buffer == other.to_a end |
#developer(content) ⇒ void
This method returns an undefined value.
75 76 77 |
# File 'lib/llm/prompt.rb', line 75 def developer(content) talk(content, role: @provider.developer_role) end |
#system(content) ⇒ void
This method returns an undefined value.
67 68 69 |
# File 'lib/llm/prompt.rb', line 67 def system(content) talk(content, role: @provider.system_role) end |
#talk(content, role: @provider.user_role) ⇒ void Also known as: chat
This method returns an undefined value.
44 45 46 47 48 49 50 51 52 |
# File 'lib/llm/prompt.rb', line 44 def talk(content, role: @provider.user_role) role = case role.to_sym when :system then @provider.system_role when :user then @provider.user_role when :developer then @provider.developer_role else role end @buffer << LLM::Message.new(role, content) end |
#to_a ⇒ Array<LLM::Message>
Returns the prompt messages in order.
82 83 84 |
# File 'lib/llm/prompt.rb', line 82 def to_a @buffer.dup end |
#user(content) ⇒ void
This method returns an undefined value.
59 60 61 |
# File 'lib/llm/prompt.rb', line 59 def user(content) talk(content, role: @provider.user_role) end |