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 Session#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"])
ses = LLM::Session.new(llm)

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

res = ses.talk(prompt)

Instance Method Summary collapse

Constructor Details

#initialize(provider, &b) ⇒ Prompt

Returns a new instance of Prompt.

Parameters:

  • provider (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(provider, &b)
  @provider = provider
  @buffer = []
  unless b.nil?
    (b.arity == 1) ? b.call(self) : instance_eval(&b)
  end
end

Instance Method Details

#talk(content, role: @provider.user_role) ⇒ void Also known as: chat

This method returns an undefined value.

Parameters:

  • content (String)

    The message

  • role (Symbol) (defaults to: @provider.user_role)

    The role (eg user, system)



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

#user(content) ⇒ void

This method returns an undefined value.

Parameters:

  • content (String)

    The message content



59
60
61
# File 'lib/llm/prompt.rb', line 59

def user(content)
  chat(content, role: @provider.user_role)
end

#system(content) ⇒ void

This method returns an undefined value.

Parameters:

  • content (String)

    The message content



67
68
69
# File 'lib/llm/prompt.rb', line 67

def system(content)
  chat(content, role: @provider.system_role)
end

#developer(content) ⇒ void

This method returns an undefined value.

Parameters:

  • content (String)

    The message content



75
76
77
# File 'lib/llm/prompt.rb', line 75

def developer(content)
  chat(content, role: @provider.developer_role)
end

#to_aArray<LLM::Message>

Returns the prompt messages in order.

Returns:

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