openai-assistant 0.3.0 → 0.5.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: e4ddb6b9bd825412979c3e7a33935200236dff748ce439c8c5ac29fb7dfadc6c
4
- data.tar.gz: c0c4fa73e072bfc18daa545ce8b06243d581adc3a5d948ffad68e311016de6c6
3
+ metadata.gz: 4eeb3d8b4d984070b698e6dbaf0cc49da89e42e44f765b2fc7327f36e2cfb012
4
+ data.tar.gz: 4779ca985f33430bf684077e80dcaa4cf4faa11f3d836a996c760c42c6c6998f
5
5
  SHA512:
6
- metadata.gz: 67d7e18366125045cfbdb2180955b337a31b361eb8079c118a8fa8ca919b9d78878bf7879d910a2d942fdb3d5d85a23adbbf67d276974caf86d3c0c5223e0e7a
7
- data.tar.gz: e3dab4d30f625f79d93f424641640f74089915b103dc7e2fa32d1995f29fdd5805cd402b73e639db6eef70f8b8c608dd36f669a3e9b41847997bf686480b3ee8
6
+ metadata.gz: 173ba5925ce44d502ca2957488d04fcd42d69dc2a4fa5bed56525966abff7728f39ef131c76fa3030e1801f43c68ddbee994bb83a403281c0d34fa8dae1e0e5a
7
+ data.tar.gz: 65cacb1f2f1a1de42f577d0c914df1029f4873ea8d4b1300e26aa7803adf8662889f552986100eae93e0652468d13e24de066b601159a3742ca178b003e8f32a
data/.rubocop.yml CHANGED
@@ -16,4 +16,7 @@ Metrics/MethodLength:
16
16
  Max: 100
17
17
 
18
18
  Metrics/BlockLength:
19
- Max: 100
19
+ Max: 100
20
+
21
+ Metrics/ClassLength:
22
+ Max: 500
data/README.md CHANGED
@@ -22,4 +22,9 @@ You can direct interact with gem by:
22
22
 
23
23
  + `instance.delete_assistant(assistant_id)`
24
24
 
25
- + `instance.list_assistant()`
25
+ + `instance.list_assistant()`
26
+
27
+ ## Reference
28
+ - source: https://github.com/duonghds24/openai-assistant
29
+ - gem: https://rubygems.org/gems/openai-assistant
30
+ - document: https://www.rubydoc.info/gems/openai-assistant/
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Openai
4
- class Assistant
5
- VERSION = "0.3.0"
4
+ class Assistant < Openai::Base
5
+ VERSION = "0.5.0"
6
6
  end
7
7
  end
@@ -2,97 +2,62 @@
2
2
 
3
3
  require_relative "assistant/version"
4
4
  require_relative "assistant_obj"
5
- require "uri"
5
+ require_relative "base"
6
6
  require "json"
7
7
  require "net/http"
8
- require "rest-client"
8
+ require "uri"
9
+
9
10
  module Openai
10
11
  # An openai assistant
11
- class Assistant
12
- @openai_api_key = nil
13
- @openai_url = nil
14
-
15
- # @param api_key [String] The api key of openai
12
+ class Assistant < Base
13
+ # @param api_key [String] The api key of openai\
16
14
  def initialize(api_key = "")
17
- @openai_api_key = api_key
18
- # hard the host because if the official docs change the host, maybe it will change another
19
- # we need to update this gem for any change
20
- @openai_url = "https://api.openai.com/v1/assistants"
15
+ super(api_key)
21
16
  end
22
17
 
23
18
  # @param api_key [String] The api key of openai
24
19
  def self.setup(api_key = "")
25
- @openai_api_key = api_key
26
- # hard the host because if the official docs change the host, maybe it will change another
27
- # we need to update this gem for any change
28
- @openai_url = "https://api.openai.com/v1/assistants"
20
+ initialize(api_key)
29
21
  end
30
22
 
31
23
  # @param model [String] Select model of the assistant. refer on: https://platform.openai.com/docs/api-reference/models/list.
32
24
  # @param instructions [String] The system instructions that the assistant uses.
33
25
  # @return [Openai::AssistantObj] A new response object of assistant.
34
26
  def create_assistant(model, instructions)
35
- url = @openai_url
36
- headers = {
37
- "Authorization": "Bearer #{@openai_api_key}",
38
- "OpenAI-Beta": "assistants=v1",
39
- "Content-Type": "application/json"
40
- }
27
+ puts default_headers
28
+ url = URI.parse(@openai_url)
41
29
  req_body = {
42
30
  "instructions": instructions,
43
31
  "name": "assistant",
44
32
  "tools": [{ "type": "code_interpreter" }],
45
33
  "model": model
46
- }
47
- begin
48
- resp = RestClient.post(url, req_body.to_json, headers)
49
- rescue RestClient::ExceptionWithResponse => e
50
- resp = e.response
51
- end
52
- unless resp.code == 200
53
- parsed = JSON.parse(resp.body)
54
- return parsed["error"]["code"]
55
- end
56
- parse_assistant_object(JSON.parse(resp.body))
34
+ }.to_json
35
+ response = call_post(url, req_body)
36
+ return response["error"]["code"] unless response["error"].nil?
37
+
38
+ parse_assistant_object(JSON.parse(response.body))
57
39
  end
58
40
 
59
41
  # @param assistant_id [String] The id of assistant after create
60
42
  # @return [Openai::AssistantObj] A new response object of assistant.
61
43
  def retrieve_assistant(assistant_id)
62
44
  url = "#{@openai_url}/#{assistant_id}"
63
- headers = {
64
- "Authorization": "Bearer #{@openai_api_key}",
65
- "OpenAI-Beta": "assistants=v1",
66
- "Content-Type": "application/json"
67
- }
68
- begin
69
- resp = RestClient.get(url, headers)
70
- rescue RestClient::ExceptionWithResponse => e
71
- resp = e.response
72
- end
73
- unless resp.code == 200
74
- parsed = JSON.parse(resp.body)
75
- return parsed["error"]["code"]
76
- end
77
- parse_assistant_object(JSON.parse(resp.body))
45
+ uri = URI(url)
46
+ response = call_post(uri, nil)
47
+ return response["error"]["code"] unless response["error"].nil?
48
+
49
+ parse_assistant_object(JSON.parse(response.body))
78
50
  end
79
51
 
80
52
  # @param assistant_id [String] The id of assistant after create
81
53
  # @return [String] Message delete the assistant ok or not
82
54
  def delete_assistant(assistant_id)
83
55
  url = "#{@openai_url}/#{assistant_id}"
84
- headers = {
85
- "Authorization": "Bearer #{@openai_api_key}",
86
- "OpenAI-Beta": "assistants=v1",
87
- "Content-Type": "application/json"
88
- }
89
- begin
90
- resp = RestClient.delete(url, headers)
91
- rescue RestClient::ExceptionWithResponse => e
92
- resp = e.response
93
- end
94
- parsed = JSON.parse(resp.body)
95
- return parsed["error"]["code"] unless resp.code == 200
56
+ uri = URI(url)
57
+ response = call_post(uri, nil)
58
+ return response["error"]["code"] unless response["error"].nil?
59
+
60
+ parsed = JSON.parse(response.body)
96
61
 
97
62
  parsed["deleted"]
98
63
  end
@@ -100,19 +65,11 @@ module Openai
100
65
  # @return [Array<Openai::AssistantObj>] List all assistant
101
66
  def list_assistant
102
67
  url = @openai_url
103
- headers = {
104
- "Authorization": "Bearer #{@openai_api_key}",
105
- "OpenAI-Beta": "assistants=v1",
106
- "Content-Type": "application/json"
107
- }
108
- begin
109
- resp = RestClient.get(url, headers)
110
- rescue RestClient::ExceptionWithResponse => e
111
- resp = e.response
112
- end
113
- parsed = JSON.parse(resp.body)
114
- return parsed["error"]["code"] unless resp.code == 200
68
+ uri = URI(url)
69
+ response = call_get(uri)
70
+ return response["error"]["code"] unless response["error"].nil?
115
71
 
72
+ parsed = JSON.parse(response.body)
116
73
  assistants = []
117
74
  parsed["data"].each do |ast|
118
75
  assistants << parse_assistant_object(ast)
@@ -134,5 +91,36 @@ module Openai
134
91
  metadata: data["metadata"]
135
92
  )
136
93
  end
94
+
95
+ def default_headers
96
+ {
97
+ "Authorization": "Bearer #{@openai_api_key}",
98
+ "OpenAI-Beta": "assistants=v1",
99
+ "Content-Type": "application/json"
100
+ }
101
+ 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
137
125
  end
138
126
  end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Openai
4
+ # Base class of openai
5
+ class Base
6
+ @openai_api_key = nil
7
+ @openai_url = nil
8
+
9
+ def initialize(api_key = "")
10
+ @openai_api_key = api_key
11
+ # hard the host because if the official docs change the host, maybe it will change another
12
+ # we need to update this gem for any change
13
+ @openai_url = "https://api.openai.com/v1/assistants"
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "lib/openai/base"
3
4
  require_relative "lib/openai/assistant/version"
4
5
 
5
6
  Gem::Specification.new do |spec|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openai-assistant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - duonghds
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-29 00:00:00.000000000 Z
11
+ date: 2023-11-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -41,6 +41,7 @@ files:
41
41
  - lib/openai/assistant.rb
42
42
  - lib/openai/assistant/version.rb
43
43
  - lib/openai/assistant_obj.rb
44
+ - lib/openai/base.rb
44
45
  - openai-assistant.gemspec
45
46
  - sig/openai/assistant.rbs
46
47
  homepage: https://rubygems.org/