openai-assistant 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4eeb3d8b4d984070b698e6dbaf0cc49da89e42e44f765b2fc7327f36e2cfb012
4
- data.tar.gz: 4779ca985f33430bf684077e80dcaa4cf4faa11f3d836a996c760c42c6c6998f
3
+ metadata.gz: 30d997127785423573776d064b5a0096f121df245af1c56511be53563b342505
4
+ data.tar.gz: 166a91a99f0caf0f5b08c9de791531c9f16c9493d2ffa1395db11237c982f620
5
5
  SHA512:
6
- metadata.gz: 173ba5925ce44d502ca2957488d04fcd42d69dc2a4fa5bed56525966abff7728f39ef131c76fa3030e1801f43c68ddbee994bb83a403281c0d34fa8dae1e0e5a
7
- data.tar.gz: 65cacb1f2f1a1de42f577d0c914df1029f4873ea8d4b1300e26aa7803adf8662889f552986100eae93e0652468d13e24de066b601159a3742ca178b003e8f32a
6
+ metadata.gz: c5f306812242afaac48bd3da55e0280a36877238052ebdd5e75942d68bdf68cdbfc9a81d1c0ebeb2b9d8abd4b10644bdfaef37b1b22ba8580a5a00222a597693
7
+ data.tar.gz: 8aa5092ad31e0372595baf420020f8d953a500dce327cab67c9344221c7aceca230d4150ef11637304f8558eebcd7d32506ceb533a7de1f7e7647dd5c53dac96
data/lib/http/http.rb ADDED
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "json"
5
+
6
+ module Http
7
+ # An http client
8
+ class Client
9
+ def call_post(_url, req_body, headers)
10
+ http = Net::HTTP.new(_url.host, _url.port)
11
+ http.use_ssl = true
12
+ request = Net::HTTP::Post.new(_url.path, headers)
13
+ request.body = req_body unless req_body.nil?
14
+ http.request(request)
15
+ end
16
+
17
+ def call_delete(_url, headers)
18
+ http = Net::HTTP.new(_url.host, _url.port)
19
+ http.use_ssl = true
20
+ request = Net::HTTP::Delete.new(_url.path, headers)
21
+ http.request(request)
22
+ end
23
+
24
+ def call_get(_url, headers)
25
+ http = Net::HTTP.new(_url.host, _url.port)
26
+ http.use_ssl = true
27
+ request = Net::HTTP::Get.new(_url.path, headers)
28
+ http.request(request)
29
+ end
30
+ end
31
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Openai
4
4
  class Assistant < Openai::Base
5
- VERSION = "0.5.0"
5
+ VERSION = "0.6.0"
6
6
  end
7
7
  end
@@ -3,6 +3,7 @@
3
3
  require_relative "assistant/version"
4
4
  require_relative "assistant_obj"
5
5
  require_relative "base"
6
+ require_relative "../http/http"
6
7
  require "json"
7
8
  require "net/http"
8
9
  require "uri"
@@ -24,7 +25,6 @@ module Openai
24
25
  # @param instructions [String] The system instructions that the assistant uses.
25
26
  # @return [Openai::AssistantObj] A new response object of assistant.
26
27
  def create_assistant(model, instructions)
27
- puts default_headers
28
28
  url = URI.parse(@openai_url)
29
29
  req_body = {
30
30
  "instructions": instructions,
@@ -32,9 +32,11 @@ module Openai
32
32
  "tools": [{ "type": "code_interpreter" }],
33
33
  "model": model
34
34
  }.to_json
35
- response = call_post(url, req_body)
36
- return response["error"]["code"] unless response["error"].nil?
37
-
35
+ response = @http_client.call_post(url, req_body, default_headers)
36
+ unless response.code == "200"
37
+ parsed = JSON.parse(response.body)
38
+ return parsed["error"]["code"]
39
+ end
38
40
  parse_assistant_object(JSON.parse(response.body))
39
41
  end
40
42
 
@@ -43,9 +45,11 @@ module Openai
43
45
  def retrieve_assistant(assistant_id)
44
46
  url = "#{@openai_url}/#{assistant_id}"
45
47
  uri = URI(url)
46
- response = call_post(uri, nil)
47
- return response["error"]["code"] unless response["error"].nil?
48
-
48
+ response = @http_client.call_get(uri, default_headers)
49
+ unless response.code == "200"
50
+ parsed = JSON.parse(response.body)
51
+ return parsed["error"]["code"]
52
+ end
49
53
  parse_assistant_object(JSON.parse(response.body))
50
54
  end
51
55
 
@@ -54,10 +58,9 @@ module Openai
54
58
  def delete_assistant(assistant_id)
55
59
  url = "#{@openai_url}/#{assistant_id}"
56
60
  uri = URI(url)
57
- response = call_post(uri, nil)
58
- return response["error"]["code"] unless response["error"].nil?
59
-
61
+ response = @http_client.call_delete(uri, default_headers)
60
62
  parsed = JSON.parse(response.body)
63
+ return parsed["error"]["code"] unless response.code == "200"
61
64
 
62
65
  parsed["deleted"]
63
66
  end
@@ -66,17 +69,18 @@ module Openai
66
69
  def list_assistant
67
70
  url = @openai_url
68
71
  uri = URI(url)
69
- response = call_get(uri)
70
- return response["error"]["code"] unless response["error"].nil?
71
-
72
+ response = @http_client.call_get(uri, default_headers)
72
73
  parsed = JSON.parse(response.body)
74
+ return parsed["error"]["code"] unless response.code == "200"
75
+
73
76
  assistants = []
74
77
  parsed["data"].each do |ast|
75
78
  assistants << parse_assistant_object(ast)
76
79
  end
77
80
  end
78
81
 
79
- # @return private
82
+ private
83
+
80
84
  def parse_assistant_object(data)
81
85
  Openai::AssistantObj.new(
82
86
  id: data["id"],
@@ -99,28 +103,5 @@ module Openai
99
103
  "Content-Type": "application/json"
100
104
  }
101
105
  end
102
-
103
- def call_post(url, req_body)
104
- uri = URI(url)
105
- http = Net::HTTP.new(uri.host, uri.port)
106
- request = Net::HTTP::Post.new(uri.path, default_headers)
107
- request.body = req_body
108
- response = http.request(request)
109
- parsed = JSON.parse(response.body)
110
- return parsed["error"]["code"] unless response.code == "200"
111
-
112
- parsed
113
- end
114
-
115
- def call_get(url)
116
- uri = URI(url)
117
- http = Net::HTTP.new(uri.host, uri.port)
118
- request = Net::HTTP::Get.new(uri.path, default_headers)
119
- response = http.request(request)
120
- parsed = JSON.parse(response.body)
121
- return parsed["error"]["code"] unless response.code == "200"
122
-
123
- parsed
124
- end
125
106
  end
126
107
  end
data/lib/openai/base.rb CHANGED
@@ -1,16 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../http/http"
4
+
3
5
  module Openai
4
6
  # Base class of openai
5
7
  class Base
6
8
  @openai_api_key = nil
7
9
  @openai_url = nil
10
+ @http_client = nil
8
11
 
9
12
  def initialize(api_key = "")
10
13
  @openai_api_key = api_key
11
14
  # hard the host because if the official docs change the host, maybe it will change another
12
15
  # we need to update this gem for any change
13
16
  @openai_url = "https://api.openai.com/v1/assistants"
17
+ @http_client = Http::Client.new
14
18
  end
15
19
  end
16
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openai-assistant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - duonghds
@@ -38,6 +38,7 @@ files:
38
38
  - LICENSE.txt
39
39
  - README.md
40
40
  - Rakefile
41
+ - lib/http/http.rb
41
42
  - lib/openai/assistant.rb
42
43
  - lib/openai/assistant/version.rb
43
44
  - lib/openai/assistant_obj.rb