Class: LLM::Prompt

Inherits:
Object
  • Object
show all
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.

Examples:

llm = LLM.openai(key: ENV["KEY"])
ctx = LLM::Context.new(llm)

prompt = ctx.prompt do
  system "Your task is to assist the user"
  user "Hello. Can you assist me?"
end

res = ctx.talk(prompt)

Instance Method Summary collapse

Constructor Details

#initialize(llm, &b) ⇒ Prompt

Returns a new instance of Prompt.

Parameters:

  • llm (LLM::Provider) —

    A provider used to resolve provider-specific role names.

  • b (Proc) —

    A block that composes messages. If the block takes one argument, it receives the prompt object. Otherwise the block runs in the prompt context via instance_eval.



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.

Parameters:

  • content (String) —

    The message content

  • extra (Hash, nil) (defaults to: nil) —

    Fields carried alongside the message



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.

Parameters:

  • content (String) —

    The message

  • role (Symbol) (defaults to: @llm.user_role) —

    The role (eg user, system)

  • extra (Hash, nil) (defaults to: nil) —

    Fields carried alongside the message. A caller that composes a message of its own can mark it here, and the runtime reads the mark back rather than inferring it from the role.



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
  message = extra ? LLM::Message.new(role, content, extra) : LLM::Message.new(role, content)
  @buffer << message
end

#user(content, extra: nil)

This method returns an undefined value.

Parameters:

  • content (String) —

    The message content

  • extra (Hash, nil) (defaults to: nil) —

    Fields carried alongside the message



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.

Parameters:

  • content (String) —

    The message content

  • extra (Hash, nil) (defaults to: nil) —

    Fields carried alongside the message



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.

Returns:

  • (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

#==(other) ⇒ Boolean Also known as: eql?

Returns true when two prompts have the same buffer

Parameters:

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/llm/prompt.rb', line 101

def ==(other)
  return false unless LLM::Prompt === other
  @buffer == other.to_a
end