Class: OmniAI::OpenAI::Thread::Run

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

Overview

An OpenAI run within a thread.

Defined Under Namespace

Modules: Status

Constant Summary collapse

TERMINATED_STATUSES =
[
  Status::CANCELLED,
  Status::FAILED,
  Status::COMPLETED,
  Status::EXPIRED,
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, assistant_id: nil, thread_id: nil, status: nil, model: nil, temperature: nil, instructions: nil, metadata: {}, tools: [], client: Client.new) ⇒ Run

Returns a new instance of Run.

Parameters:

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

    optional

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

    optional

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

    optional

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

    optional

  • temperature (Decimal, nil) (defaults to: nil)

    optional

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

    optional

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

    optional

  • tools (Array<Hash>, nil) (defaults to: [])

    optional

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

    optional



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/omniai/openai/thread/run.rb', line 67

def initialize(
  id: nil,
  assistant_id: nil,
  thread_id: nil,
  status: nil,
  model: nil,
  temperature: nil,
  instructions: nil,
  metadata: {},
  tools: [],
  client: Client.new
)
  @id = id
  @assistant_id = assistant_id
  @thread_id = thread_id
  @status = status
  @model = model
  @temperature = temperature
  @instructions = instructions
  @metadata = 
  @tools = tools
  @client = client
end

Instance Attribute Details

#assistant_idString?

Returns:

  • (String, nil)


28
29
30
# File 'lib/omniai/openai/thread/run.rb', line 28

def assistant_id
  @assistant_id
end

#idString?

Returns:

  • (String, nil)


24
25
26
# File 'lib/omniai/openai/thread/run.rb', line 24

def id
  @id
end

#instructionsString?

Returns:

  • (String, nil)


48
49
50
# File 'lib/omniai/openai/thread/run.rb', line 48

def instructions
  @instructions
end

#metadataHash?

Returns:

  • (Hash, nil)


56
57
58
# File 'lib/omniai/openai/thread/run.rb', line 56

def 
  @metadata
end

#modelString?

Returns:

  • (String, nil)


40
41
42
# File 'lib/omniai/openai/thread/run.rb', line 40

def model
  @model
end

#statusString?

Returns:

  • (String, nil)


36
37
38
# File 'lib/omniai/openai/thread/run.rb', line 36

def status
  @status
end

#temperatureFloat?

Returns:

  • (Float, nil)


44
45
46
# File 'lib/omniai/openai/thread/run.rb', line 44

def temperature
  @temperature
end

#thread_idString?

Returns:

  • (String, nil)


32
33
34
# File 'lib/omniai/openai/thread/run.rb', line 32

def thread_id
  @thread_id
end

#toolsArray<Hash>?

Returns:

  • (Array<Hash>, nil)


52
53
54
# File 'lib/omniai/openai/thread/run.rb', line 52

def tools
  @tools
end

Class Method Details

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

Parameters:

Returns:

Raises:

  • (HTTPError)


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

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}/runs", params: { limit: }.compact)

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

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

.cancel!(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)


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

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

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

  response.parse
end

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

Parameters:

  • thread_id (String)

    required

  • id (String)

    required

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

    optional

Returns:

Raises:

  • (HTTPError)


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

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

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

  parse(data: response.parse)
end

Instance Method Details

#cancel!OmniAI::OpenAI::Thread

Returns:

Raises:

  • (OmniAI::Error)


178
179
180
181
182
183
184
# File 'lib/omniai/openai/thread/run.rb', line 178

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

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

#inspectString

Returns:

  • (String)


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

def inspect
  props = [
    "id=#{@id.inspect}",
    ("assistant_id=#{@assistant_id.inspect}" if @assistant_id),
    ("thread_id=#{@thread_id.inspect}" if @thread_id),
    ("status=#{@status.inspect}" if @status),
  ].compact
  "#<#{self.class.name} #{props.join(' ')}>"
end

#poll!(delay: 2) ⇒ OmniAI::OpenAI::Thread::Run

Parameters:

  • delay (Numeric, nil) (defaults to: 2)

    optional (seconds)

Returns:



189
190
191
192
193
194
195
196
# File 'lib/omniai/openai/thread/run.rb', line 189

def poll!(delay: 2)
  loop do
    reload!
    break if terminated?

    sleep(delay) if delay
  end
end

#reload!OmniAI::OpenAI::Thread

Returns:

Raises:

  • (HTTPError)


162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/omniai/openai/thread/run.rb', line 162

def reload!
  raise Error, 'unable to fetch! without an ID' unless @id

  response = @client.connection
    .accept(:json)
    .headers(HEADERS)
    .get(path)

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

  parse(data: response.parse)
  self
end

#save!OmniAI::OpenAI::Thread

Returns:

Raises:

  • (HTTPError)


148
149
150
151
152
153
154
155
156
157
158
# File 'lib/omniai/openai/thread/run.rb', line 148

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

#terminated?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/omniai/openai/thread/run.rb', line 199

def terminated?
  TERMINATED_STATUSES.include?(@status)
end