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
- #system(content, extra: nil)
- #talk(content, role: @llm.user_role, extra: nil) (also: #chat)
- #user(content, extra: nil)
-
#initialize(llm, &b) ⇒ Prompt
constructor
A new instance of Prompt.
- #developer(content, extra: nil)
-
#to_a ⇒ Array<LLM::Message>
Returns the prompt messages in order.
-
#==(other) ⇒ Boolean
(also: #eql?)
Returns true when two prompts have the same buffer.
Constructor Details
#initialize(llm, &b) ⇒ Prompt
Returns a new instance of Prompt.
30 31 32 33 34 35 36 |
# File 'lib/llm/prompt.rb', line 30 def initialize(llm, &b) @llm = llm @buffer = [] unless b.nil? (b.arity == 1) ? b.call(self) : instance_eval(&b) end end |
Instance Method Details
#system(content, extra: nil)
This method returns an undefined value.
76 77 78 |
# File 'lib/llm/prompt.rb', line 76 def system(content, extra: nil) talk(content, role: @llm.system_role, extra:) end |
#talk(content, role: @llm.user_role, extra: nil) Also known as: chat
This method returns an undefined value.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/llm/prompt.rb', line 48 def talk(content, role: @llm.user_role, extra: nil) role = case role.to_sym when :system then @llm.system_role when :user then @llm.user_role when :developer then @llm.developer_role else role end = extra ? LLM::Message.new(role, content, extra) : LLM::Message.new(role, content) @buffer << end |
#user(content, extra: nil)
This method returns an undefined value.
66 67 68 |
# File 'lib/llm/prompt.rb', line 66 def user(content, extra: nil) talk(content, role: @llm.user_role, extra:) end |
#developer(content, extra: nil)
This method returns an undefined value.
86 87 88 |
# File 'lib/llm/prompt.rb', line 86 def developer(content, extra: nil) talk(content, role: @llm.developer_role, extra:) end |
#to_a ⇒ Array<LLM::Message>
Returns the prompt messages in order.
93 94 95 |
# File 'lib/llm/prompt.rb', line 93 def to_a @buffer.dup end |