Class: OmniAI::OpenAI::Thread::Run
- Inherits:
-
Object
- Object
- OmniAI::OpenAI::Thread::Run
- 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
- #assistant_id ⇒ String?
- #id ⇒ String?
- #instructions ⇒ String?
- #metadata ⇒ Hash?
- #model ⇒ String?
- #status ⇒ String?
- #temperature ⇒ Float?
- #thread_id ⇒ String?
- #tools ⇒ Array<Hash>?
Class Method Summary collapse
- .all(thread_id:, limit: nil, client: Client.new) ⇒ Array<OmniAI::OpenAI::Thread::Run>
- .cancel!(thread_id:, id:, client: Client.new) ⇒ Hash
- .find(thread_id:, id:, client: Client.new) ⇒ OmniAI::OpenAI::Thread::Run
Instance Method Summary collapse
- #cancel! ⇒ OmniAI::OpenAI::Thread
-
#initialize(id: nil, assistant_id: nil, thread_id: nil, status: nil, model: nil, temperature: nil, instructions: nil, metadata: {}, tools: [], client: Client.new) ⇒ Run
constructor
A new instance of Run.
- #inspect ⇒ String
- #poll!(delay: 2) ⇒ OmniAI::OpenAI::Thread::Run
- #reload! ⇒ OmniAI::OpenAI::Thread
- #save! ⇒ OmniAI::OpenAI::Thread
- #terminated? ⇒ Boolean
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.
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_id ⇒ String?
28 29 30 |
# File 'lib/omniai/openai/thread/run.rb', line 28 def assistant_id @assistant_id end |
#id ⇒ String?
24 25 26 |
# File 'lib/omniai/openai/thread/run.rb', line 24 def id @id end |
#instructions ⇒ String?
48 49 50 |
# File 'lib/omniai/openai/thread/run.rb', line 48 def instructions @instructions end |
#metadata ⇒ Hash?
56 57 58 |
# File 'lib/omniai/openai/thread/run.rb', line 56 def @metadata end |
#model ⇒ String?
40 41 42 |
# File 'lib/omniai/openai/thread/run.rb', line 40 def model @model end |
#status ⇒ String?
36 37 38 |
# File 'lib/omniai/openai/thread/run.rb', line 36 def status @status end |
#temperature ⇒ Float?
44 45 46 |
# File 'lib/omniai/openai/thread/run.rb', line 44 def temperature @temperature end |
#thread_id ⇒ String?
32 33 34 |
# File 'lib/omniai/openai/thread/run.rb', line 32 def thread_id @thread_id end |
#tools ⇒ Array<Hash>?
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>
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
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
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
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 |
#inspect ⇒ 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
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
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
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
199 200 201 |
# File 'lib/omniai/openai/thread/run.rb', line 199 def terminated? TERMINATED_STATUSES.include?(@status) end |