Class: RubyConversations::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_conversations/prompt.rb

Overview

Represents a prompt template used to generate AI messages.

Constant Summary collapse

ROLES =

Constants

%w[system user assistant].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Prompt

Returns a new instance of Prompt.



15
16
17
18
19
# File 'lib/ruby_conversations/prompt.rb', line 15

def initialize(attributes = {})
  attributes.each do |key, value|
    public_send("#{key}=", value) if respond_to?("#{key}=")
  end
end

Instance Attribute Details

#created_atObject

Define attributes



9
10
11
# File 'lib/ruby_conversations/prompt.rb', line 9

def created_at
  @created_at
end

#idObject

Define attributes



9
10
11
# File 'lib/ruby_conversations/prompt.rb', line 9

def id
  @id
end

#latest_version_idObject

Define attributes



9
10
11
# File 'lib/ruby_conversations/prompt.rb', line 9

def latest_version_id
  @latest_version_id
end

#llmObject

Define attributes



9
10
11
# File 'lib/ruby_conversations/prompt.rb', line 9

def llm
  @llm
end

#messageObject

Define attributes



9
10
11
# File 'lib/ruby_conversations/prompt.rb', line 9

def message
  @message
end

#metadataObject

Define attributes



9
10
11
# File 'lib/ruby_conversations/prompt.rb', line 9

def 
  @metadata
end

#nameObject

Define attributes



9
10
11
# File 'lib/ruby_conversations/prompt.rb', line 9

def name
  @name
end

#roleObject

Define attributes



9
10
11
# File 'lib/ruby_conversations/prompt.rb', line 9

def role
  @role
end

#temperatureObject

Define attributes



9
10
11
# File 'lib/ruby_conversations/prompt.rb', line 9

def temperature
  @temperature
end

#updated_atObject

Define attributes



9
10
11
# File 'lib/ruby_conversations/prompt.rb', line 9

def updated_at
  @updated_at
end

#valid_placeholdersObject

Define attributes



9
10
11
# File 'lib/ruby_conversations/prompt.rb', line 9

def valid_placeholders
  @valid_placeholders
end

Class Method Details

.find_by!(conditions) ⇒ Object



33
34
35
36
# File 'lib/ruby_conversations/prompt.rb', line 33

def self.find_by!(conditions)
  name = conditions[:name]
  find_by_name!(name)
end

.find_by_name!(name) ⇒ Object



26
27
28
29
30
31
# File 'lib/ruby_conversations/prompt.rb', line 26

def self.find_by_name!(name)
  prompt_data = RubyConversations.client.fetch_prompt(name)
  raise "Prompt not found: #{name}" unless prompt_data

  new(prompt_data)
end

.rolesObject

Class methods



22
23
24
# File 'lib/ruby_conversations/prompt.rb', line 22

def self.roles
  ROLES
end

Instance Method Details

#attributesObject

Basic attributes method



39
40
41
42
43
44
45
46
# File 'lib/ruby_conversations/prompt.rb', line 39

def attributes
  {
    'id' => id, 'name' => name, 'role' => role, 'message' => message,
    'valid_placeholders' => valid_placeholders, 'temperature' => temperature,
    'metadata' => , 'created_at' => created_at, 'updated_at' => updated_at,
    'latest_version_id' => latest_version_id, 'llm' => llm
  }
end

#attributes_for_apiObject

Method for API serialization



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ruby_conversations/prompt.rb', line 49

def attributes_for_api
  {
    id: id,
    name: name,
    role: role,
    message: message,
    valid_placeholders: valid_placeholders,
    temperature: temperature,
    metadata: ,
    latest_version_id: latest_version_id
  }.compact
end

#interpolate(variables = {}) ⇒ Object

Interpolate placeholders in the message



63
64
65
66
67
# File 'lib/ruby_conversations/prompt.rb', line 63

def interpolate(variables = {})
  return message if message.nil? || variables.empty?

  format(message, **variables)
end