Class: PromptBuilder::Items::Message

Inherits:
Base
  • Object
show all
Defined in:
lib/prompt_builder/items/message.rb

Overview

Represents a message item in a conversation. Messages have a role and an array of content objects.

Constant Summary

Constants inherited from Base

Base::TYPES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(role:, content:, id: nil, status: nil, phase: nil, **extra) ⇒ Message

Create a new Message item.

Parameters:

  • id (String, nil) (defaults to: nil)

    the message identifier

  • role (String)

    the message role

  • status (String, nil) (defaults to: nil)

    the message status

  • phase (String, nil) (defaults to: nil)

    the message phase

  • content (Array<Content::Base>)

    the content objects

  • extra (Hash)

    provider-specific extra keyword arguments



34
35
36
37
38
39
40
41
# File 'lib/prompt_builder/items/message.rb', line 34

def initialize(role:, content:, id: nil, status: nil, phase: nil, **extra)
  @id = id&.to_s
  @role = role&.to_s
  @status = status&.to_s
  @phase = phase&.to_s
  @content = normalize_content(content)
  @extra = extra.transform_keys(&:to_s)
end

Instance Attribute Details

#contentArray<Content::Base> (readonly)

Returns the content objects.

Returns:



21
22
23
# File 'lib/prompt_builder/items/message.rb', line 21

def content
  @content
end

#extraHash? (readonly)

Returns provider-specific extra data.

Returns:

  • (Hash, nil)

    provider-specific extra data



24
25
26
# File 'lib/prompt_builder/items/message.rb', line 24

def extra
  @extra
end

#idString? (readonly)

Returns the message identifier.

Returns:

  • (String, nil)

    the message identifier



9
10
11
# File 'lib/prompt_builder/items/message.rb', line 9

def id
  @id
end

#phaseString? (readonly)

Returns the message phase.

Returns:

  • (String, nil)

    the message phase



18
19
20
# File 'lib/prompt_builder/items/message.rb', line 18

def phase
  @phase
end

#roleString (readonly)

Returns the message role (e.g. “user”, “assistant”, “system”, “developer”).

Returns:

  • (String)

    the message role (e.g. “user”, “assistant”, “system”, “developer”)



12
13
14
# File 'lib/prompt_builder/items/message.rb', line 12

def role
  @role
end

#statusString? (readonly)

Returns the message status.

Returns:

  • (String, nil)

    the message status



15
16
17
# File 'lib/prompt_builder/items/message.rb', line 15

def status
  @status
end

Class Method Details

.from_h(hash) ⇒ Message

Deserialize a Message from a Hash.

Parameters:

  • hash (Hash)

    a Hash with string keys

Returns:



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/prompt_builder/items/message.rb', line 48

def from_h(hash)
  content = (hash["content"] || []).map { |c| Content::Base.from_h(c) }
  new(
    id: hash["id"],
    role: hash["role"],
    status: hash["status"],
    phase: hash["phase"],
    content: content,
    **hash.except("type", "id", "role", "status", "phase", "content").transform_keys(&:to_sym)
  )
end

Instance Method Details

#to_hHash

Serialize to a Hash with string keys.

Returns:

  • (Hash)


64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/prompt_builder/items/message.rb', line 64

def to_h
  hash = {
    "type" => "message",
    "role" => @role,
    "content" => @content.map(&:to_h)
  }
  hash["id"] = @id if @id
  hash["status"] = @status if @status
  hash["phase"] = @phase if @phase
  hash = PromptBuilder.jsonify(@extra).merge(hash) unless @extra.empty?
  hash
end