Skip to content

Commit e2817f0

Browse files
authored
Merge pull request #361 from relayplatform/assistant-support
Support for Assistants, Threads, Messages and Runs
2 parents b618e84 + 830f438 commit e2817f0

35 files changed

+2541
-25
lines changed

lib/openai.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
require_relative "openai/finetunes"
88
require_relative "openai/images"
99
require_relative "openai/models"
10+
require_relative "openai/assistants"
11+
require_relative "openai/threads"
12+
require_relative "openai/messages"
13+
require_relative "openai/runs"
14+
require_relative "openai/run_steps"
1015
require_relative "openai/audio"
1116
require_relative "openai/version"
1217

@@ -30,7 +35,7 @@ def initialize
3035
@organization_id = nil
3136
@uri_base = DEFAULT_URI_BASE
3237
@request_timeout = DEFAULT_REQUEST_TIMEOUT
33-
@extra_headers = nil
38+
@extra_headers = {}
3439
end
3540

3641
def access_token

lib/openai/assistants.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module OpenAI
2+
class Assistants
3+
def initialize(client:)
4+
@client = client.beta(assistants: "v1")
5+
end
6+
7+
def list
8+
@client.get(path: "/assistants")
9+
end
10+
11+
def retrieve(id:)
12+
@client.get(path: "/assistants/#{id}")
13+
end
14+
15+
def create(parameters: {})
16+
@client.json_post(path: "/assistants", parameters: parameters)
17+
end
18+
19+
def modify(id:, parameters: {})
20+
@client.json_post(path: "/assistants/#{id}", parameters: parameters)
21+
end
22+
23+
def delete(id:)
24+
@client.delete(path: "/assistants/#{id}")
25+
end
26+
end
27+
end

lib/openai/client.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,38 @@ def models
5353
@models ||= OpenAI::Models.new(client: self)
5454
end
5555

56+
def assistants
57+
@assistants ||= OpenAI::Assistants.new(client: self)
58+
end
59+
60+
def threads
61+
@threads ||= OpenAI::Threads.new(client: self)
62+
end
63+
64+
def messages
65+
@messages ||= OpenAI::Messages.new(client: self)
66+
end
67+
68+
def runs
69+
@runs ||= OpenAI::Runs.new(client: self)
70+
end
71+
72+
def run_steps
73+
@run_steps ||= OpenAI::RunSteps.new(client: self)
74+
end
75+
5676
def moderations(parameters: {})
5777
json_post(path: "/moderations", parameters: parameters)
5878
end
5979

6080
def azure?
6181
@api_type&.to_sym == :azure
6282
end
83+
84+
def beta(apis)
85+
dup.tap do |client|
86+
client.add_headers("OpenAI-Beta": apis.map { |k, v| "#{k}=#{v}" }.join(";"))
87+
end
88+
end
6389
end
6490
end

lib/openai/http.rb

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
require "event_stream_parser"
22

3+
require_relative "http_headers"
4+
35
module OpenAI
46
module HTTP
7+
include HTTPHeaders
8+
59
def get(path:)
610
parse_jsonl(conn.get(uri(path: path)) do |req|
711
req.headers = headers
812
end&.body)
913
end
1014

15+
def post(path:)
16+
parse_jsonl(conn.post(uri(path: path)) do |req|
17+
req.headers = headers
18+
end&.body)
19+
end
20+
1121
def json_post(path:, parameters:)
1222
conn.post(uri(path: path)) do |req|
1323
configure_json_post_request(req, parameters)
@@ -78,29 +88,6 @@ def uri(path:)
7888
end
7989
end
8090

81-
def headers
82-
if azure?
83-
azure_headers
84-
else
85-
openai_headers
86-
end.merge(@extra_headers || {})
87-
end
88-
89-
def openai_headers
90-
{
91-
"Content-Type" => "application/json",
92-
"Authorization" => "Bearer #{@access_token}",
93-
"OpenAI-Organization" => @organization_id
94-
}
95-
end
96-
97-
def azure_headers
98-
{
99-
"Content-Type" => "application/json",
100-
"api-key" => @access_token
101-
}
102-
end
103-
10491
def multipart_parameters(parameters)
10592
parameters&.transform_values do |value|
10693
next value unless value.respond_to?(:close) # File or IO object.

lib/openai/http_headers.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module OpenAI
2+
module HTTPHeaders
3+
def add_headers(headers)
4+
@extra_headers = extra_headers.merge(headers.transform_keys(&:to_s))
5+
end
6+
7+
private
8+
9+
def headers
10+
if azure?
11+
azure_headers
12+
else
13+
openai_headers
14+
end.merge(extra_headers)
15+
end
16+
17+
def openai_headers
18+
{
19+
"Content-Type" => "application/json",
20+
"Authorization" => "Bearer #{@access_token}",
21+
"OpenAI-Organization" => @organization_id
22+
}
23+
end
24+
25+
def azure_headers
26+
{
27+
"Content-Type" => "application/json",
28+
"api-key" => @access_token
29+
}
30+
end
31+
32+
def extra_headers
33+
@extra_headers ||= {}
34+
end
35+
end
36+
end

lib/openai/messages.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module OpenAI
2+
class Messages
3+
def initialize(client:)
4+
@client = client.beta(assistants: "v1")
5+
end
6+
7+
def list(thread_id:)
8+
@client.get(path: "/threads/#{thread_id}/messages")
9+
end
10+
11+
def retrieve(thread_id:, id:)
12+
@client.get(path: "/threads/#{thread_id}/messages/#{id}")
13+
end
14+
15+
def create(thread_id:, parameters: {})
16+
@client.json_post(path: "/threads/#{thread_id}/messages", parameters: parameters)
17+
end
18+
19+
def modify(id:, thread_id:, parameters: {})
20+
@client.json_post(path: "/threads/#{thread_id}/messages/#{id}", parameters: parameters)
21+
end
22+
end
23+
end

lib/openai/run_steps.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module OpenAI
2+
class RunSteps
3+
def initialize(client:)
4+
@client = client.beta(assistants: "v1")
5+
end
6+
7+
def list(thread_id:, run_id:)
8+
@client.get(path: "/threads/#{thread_id}/runs/#{run_id}/steps")
9+
end
10+
11+
def retrieve(thread_id:, run_id:, id:)
12+
@client.get(path: "/threads/#{thread_id}/runs/#{run_id}/steps/#{id}")
13+
end
14+
end
15+
end

lib/openai/runs.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module OpenAI
2+
class Runs
3+
def initialize(client:)
4+
@client = client.beta(assistants: "v1")
5+
end
6+
7+
def list(thread_id:)
8+
@client.get(path: "/threads/#{thread_id}/runs")
9+
end
10+
11+
def retrieve(thread_id:, id:)
12+
@client.get(path: "/threads/#{thread_id}/runs/#{id}")
13+
end
14+
15+
def create(thread_id:, parameters: {})
16+
@client.json_post(path: "/threads/#{thread_id}/runs", parameters: parameters)
17+
end
18+
19+
def modify(id:, thread_id:, parameters: {})
20+
@client.json_post(path: "/threads/#{thread_id}/runs/#{id}", parameters: parameters)
21+
end
22+
23+
def cancel(id:, thread_id:)
24+
@client.post(path: "/threads/#{thread_id}/runs/#{id}/cancel")
25+
end
26+
27+
def submit_tool_outputs(thread_id:, run_id:, parameters: {})
28+
@client.json_post(path: "/threads/#{thread_id}/runs/#{run_id}/submit_tool_outputs",
29+
parameters: parameters)
30+
end
31+
end
32+
end

lib/openai/threads.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module OpenAI
2+
class Threads
3+
def initialize(client:)
4+
@client = client.beta(assistants: "v1")
5+
end
6+
7+
def list
8+
@client.get(path: "/threads")
9+
end
10+
11+
def retrieve(id:)
12+
@client.get(path: "/threads/#{id}")
13+
end
14+
15+
def create(parameters: {})
16+
@client.json_post(path: "/threads", parameters: parameters)
17+
end
18+
19+
def modify(id:, parameters: {})
20+
@client.json_post(path: "/threads/#{id}", parameters: parameters)
21+
end
22+
23+
def delete(id:)
24+
@client.delete(path: "/threads/#{id}")
25+
end
26+
end
27+
end

spec/fixtures/cassettes/assistants_create.yml

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

0 commit comments

Comments
 (0)