Skip to content

Commit fdc66a8

Browse files
committed
Add support for cancelling runs
1 parent 024900e commit fdc66a8

File tree

5 files changed

+202
-4
lines changed

5 files changed

+202
-4
lines changed

lib/openai/http.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ def get(path:)
1212
end&.body)
1313
end
1414

15+
def post(path:)
16+
parse_jsonl(conn.post(uri(path: path)) do |req|
17+
req.headers = headers
18+
end&.body)
19+
end
20+
1521
def json_post(path:, parameters:)
1622
conn.post(uri(path: path)) do |req|
1723
configure_json_post_request(req, parameters)

lib/openai/http_headers.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
module OpenAI
22
module HTTPHeaders
3+
def add_headers(headers)
4+
@extra_headers = extra_headers.merge(headers.transform_keys(&:to_s))
5+
end
6+
7+
private
8+
39
def headers
410
if azure?
511
azure_headers
@@ -26,9 +32,5 @@ def azure_headers
2632
def extra_headers
2733
@extra_headers ||= {}
2834
end
29-
30-
def add_headers(headers)
31-
@extra_headers = extra_headers.merge(headers.transform_keys(&:to_s))
32-
end
3335
end
3436
end

lib/openai/runs.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ def modify(id:, thread_id:, parameters: {})
2020
@client.json_post(path: "/threads/#{thread_id}/runs/#{id}", parameters: parameters)
2121
end
2222

23+
def cancel(id:, thread_id:)
24+
@client.post(path: "/threads/#{thread_id}/runs/#{id}/cancel")
25+
end
26+
2327
def submit_tool_outputs(thread_id:, run_id:, parameters: {})
2428
@client.json_post(path: "/threads/#{thread_id}/runs/#{run_id}/submit_tool_outputs",
2529
parameters: parameters)

spec/fixtures/cassettes/runs_cancel.yml

Lines changed: 150 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/openai/client/runs_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require "byebug"
2+
13
RSpec.describe OpenAI::Client do
24
describe "#runs" do
35
describe "#list" do
@@ -66,6 +68,40 @@
6668
end
6769
end
6870

71+
describe "#cancel" do
72+
let(:cassette) { "runs cancel" }
73+
74+
context "for a run in progress" do
75+
let(:response) do
76+
OpenAI::Client.new.runs.cancel(
77+
id: "run_tWHF8ZDAEHDxetXOFIEiUvJr",
78+
thread_id: "thread_gOFC5endN7iUfAhpekz50O5G"
79+
)
80+
end
81+
82+
it "succeeds" do
83+
VCR.use_cassette(cassette) do
84+
expect(response["object"]).to eq "thread.run"
85+
end
86+
end
87+
end
88+
89+
context "for a completed run" do
90+
let(:response) do
91+
OpenAI::Client.new.runs.cancel(
92+
id: "run_1bs2QXbSoM9kfQ4UxRbOjfpP",
93+
thread_id: "thread_UCMWkz0d98AqtTSrFZjfh89x"
94+
)
95+
end
96+
97+
it "responds with a 400" do
98+
VCR.use_cassette(cassette) do
99+
expect { response }.to raise_error(Faraday::BadRequestError)
100+
end
101+
end
102+
end
103+
end
104+
69105
describe "#submit_tool_outputs" do
70106
let(:cassette) { "runs submit_tool_outputs" }
71107
let(:response) do

0 commit comments

Comments
 (0)