Class: OmniAI::OpenAI::Thread::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/omniai/openai/thread/message.rb

Overview

An OpenAI message within a thread.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, assistant_id: nil, thread_id: nil, run_id: nil, role: nil, content: nil, attachments: [], metadata: {}, client: Client.new) ⇒ Message

Returns a new instance of Message.

Parameters:

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

    optional

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

    optional

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

    optional

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

    optional

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

    optional

  • content (String, Array, nil) (defaults to: nil)

    optional

  • attachments (Array, nil) (defaults to: [])

    optional

  • metadata (Hash, nil) (defaults to: {})

    optional

  • client (OmniAI::OpenAI::Client) (defaults to: Client.new)

    optional



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/omniai/openai/thread/message.rb', line 53

def initialize(
  id: nil,
  assistant_id: nil,
  thread_id: nil,
  run_id: nil,
  role: nil,
  content: nil,
  attachments: [],
  metadata: {},
  client: Client.new
)
  @id = id
  @assistant_id = assistant_id
  @thread_id = thread_id
  @run_id = run_id
  @role = role
  @content = content
  @attachments = attachments
  @metadata = 
  @client = client
end

Instance Attribute Details

#assistant_idString?

Returns:

  • (String, nil)


14
15
16
# File 'lib/omniai/openai/thread/message.rb', line 14

def assistant_id
  @assistant_id
end

#attachmentsArray?

Returns:

  • (Array, nil)


34
35
36
# File 'lib/omniai/openai/thread/message.rb', line 34

def attachments
  @attachments
end

#contentString, ...

Returns:

  • (String, Array, nil)


30
31
32
# File 'lib/omniai/openai/thread/message.rb', line 30

def content
  @content
end

#deletedBoolean?

Returns:

  • (Boolean, nil)


42
43
44
# File 'lib/omniai/openai/thread/message.rb', line 42

def deleted
  @deleted
end

#idString?

Returns:

  • (String, nil)


10
11
12
# File 'lib/omniai/openai/thread/message.rb', line 10

def id
  @id
end

#metadataArray?

Returns:

  • (Array, nil)


38
39
40
# File 'lib/omniai/openai/thread/message.rb', line 38

def 
  @metadata
end

#roleString?

Returns:

  • (String, nil)


26
27
28
# File 'lib/omniai/openai/thread/message.rb', line 26

def role
  @role
end

#run_idString?

Returns:

  • (String, nil)


22
23
24
# File 'lib/omniai/openai/thread/message.rb', line 22

def run_id
  @run_id
end

#thread_idString?

Returns:

  • (String, nil)


18
19
20
# File 'lib/omniai/openai/thread/message.rb', line 18

def thread_id
  @thread_id
end

Class Method Details

.all(thread_id:, limit: nil, client: Client.new) ⇒ Array<OmniAI::OpenAI::Thread::Message>

Parameters:

Returns:

Raises:

  • (HTTPError)


105
106
107
108
109
110
111
112
113
114
# File 'lib/omniai/openai/thread/message.rb', line 105

def self.all(thread_id:, limit: nil, client: Client.new)
  response = client.connection
    .accept(:json)
    .headers(HEADERS)
    .get("/#{OmniAI::OpenAI::Client::VERSION}/threads/#{thread_id}/messages", params: { limit: }.compact)

  raise HTTPError, response.flush unless response.status.ok?

  response.parse['data'].map { |data| parse(data:, client:) }
end

.destroy!(thread_id:, id:, client: Client.new) ⇒ Hash

Parameters:

  • thread_id (String)

    required

  • id (String)

    required

  • client (OmniAI::OpenAI::Client) (defaults to: Client.new)

    optional

Returns:

  • (Hash)

Raises:

  • (HTTPError)


120
121
122
123
124
125
126
127
128
129
# File 'lib/omniai/openai/thread/message.rb', line 120

def self.destroy!(thread_id:, id:, client: Client.new)
  response = client.connection
    .accept(:json)
    .headers(HEADERS)
    .delete("/#{OmniAI::OpenAI::Client::VERSION}/threads/#{thread_id}/messages/#{id}")

  raise HTTPError, response.flush unless response.status.ok?

  response.parse
end

.find(thread_id:, id:, client: Client.new) ⇒ OmniAI::OpenAI::Thread::Message

Parameters:

  • thread_id (String)

    required

  • id (String)

    required

  • client (OmniAI::OpenAI::Client) (defaults to: Client.new)

    optional

Returns:

Raises:

  • (HTTPError)


91
92
93
94
95
96
97
98
99
100
# File 'lib/omniai/openai/thread/message.rb', line 91

def self.find(thread_id:, id:, client: Client.new)
  response = client.connection
    .accept(:json)
    .headers(HEADERS)
    .get("/#{OmniAI::OpenAI::Client::VERSION}/threads/#{thread_id}/messages/#{id}")

  raise HTTPError, response.flush unless response.status.ok?

  parse(data: response.parse)
end

Instance Method Details

#destroy!OmniAI::OpenAI::Thread

Returns:

Raises:

  • (OmniAI::Error)


147
148
149
150
151
152
153
# File 'lib/omniai/openai/thread/message.rb', line 147

def destroy!
  raise OmniAI::Error, 'cannot destroy a non-persisted thread' unless @id

  data = self.class.destroy!(thread_id: @thread_id, id: @id, client: @client)
  @deleted = data['deleted']
  self
end

#inspectString

Returns:

  • (String)


76
77
78
79
80
81
82
83
84
85
# File 'lib/omniai/openai/thread/message.rb', line 76

def inspect
  props = [
    "id=#{@id.inspect}",
    ("assistant_id=#{@assistant_id.inspect}" if @assistant_id),
    ("thread_id=#{@thread_id.inspect}" if @thread_id),
    ("content=#{@content.inspect}" if @content),
  ].compact

  "#<#{self.class.name} #{props.join(' ')}>"
end

#save!OmniAI::OpenAI::Thread

Returns:

Raises:

  • (HTTPError)


133
134
135
136
137
138
139
140
141
142
143
# File 'lib/omniai/openai/thread/message.rb', line 133

def save!
  response = @client.connection
    .accept(:json)
    .headers(HEADERS)
    .post(path, json: payload)

  raise HTTPError, response.flush unless response.status.ok?

  parse(data: response.parse)
  self
end