openai-assistant 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 47c4f3c25796b691c23177c0305accd4d4eb10d13f3d5dd8d5828d6070a7f801
4
- data.tar.gz: 22cdf48b820eed695c042ecb9f5892ff8cc05c78547a6d98c95de0a7591c1435
3
+ metadata.gz: 4eeb3d8b4d984070b698e6dbaf0cc49da89e42e44f765b2fc7327f36e2cfb012
4
+ data.tar.gz: 4779ca985f33430bf684077e80dcaa4cf4faa11f3d836a996c760c42c6c6998f
5
5
  SHA512:
6
- metadata.gz: be4e756a8888af5c0b1f0247a6a17bd1e02cbdc636f43f28b84c2a7c91dd1644d981357c8ead3d81b6cb5831f35b220a6e881d8fbefb5aaa36f0349130995ff9
7
- data.tar.gz: 25566590081132fb1526f6970a472b2ca348881931e8052270aad14557fa53fb6e3020f56705866ed488dac04947d69bba7c32ea3b99f5eb3c3f355c0fc1f65a
6
+ metadata.gz: 173ba5925ce44d502ca2957488d04fcd42d69dc2a4fa5bed56525966abff7728f39ef131c76fa3030e1801f43c68ddbee994bb83a403281c0d34fa8dae1e0e5a
7
+ data.tar.gz: 65cacb1f2f1a1de42f577d0c914df1029f4873ea8d4b1300e26aa7803adf8662889f552986100eae93e0652468d13e24de066b601159a3742ca178b003e8f32a
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Openai
4
- class Assistant
5
- VERSION = "0.4.0"
4
+ class Assistant < Openai::Base
5
+ VERSION = "0.5.0"
6
6
  end
7
7
  end
@@ -2,94 +2,62 @@
2
2
 
3
3
  require_relative "assistant/version"
4
4
  require_relative "assistant_obj"
5
+ require_relative "base"
5
6
  require "json"
6
7
  require "net/http"
7
- require "rest-client"
8
+ require "uri"
9
+
8
10
  module Openai
9
11
  # An openai assistant
10
- class Assistant
11
- @openai_api_key = nil
12
- @openai_url = nil
13
-
14
- # @param api_key [String] The api key of openai
12
+ class Assistant < Base
13
+ # @param api_key [String] The api key of openai\
15
14
  def initialize(api_key = "")
16
- @openai_api_key = api_key
17
- # hard the host because if the official docs change the host, maybe it will change another
18
- # we need to update this gem for any change
19
- @openai_url = "https://api.openai.com/v1/assistants"
15
+ super(api_key)
20
16
  end
21
17
 
22
18
  # @param api_key [String] The api key of openai
23
19
  def self.setup(api_key = "")
24
- @openai_api_key = api_key
25
- @openai_url = "https://api.openai.com/v1/assistants"
20
+ initialize(api_key)
26
21
  end
27
22
 
28
23
  # @param model [String] Select model of the assistant. refer on: https://platform.openai.com/docs/api-reference/models/list.
29
24
  # @param instructions [String] The system instructions that the assistant uses.
30
25
  # @return [Openai::AssistantObj] A new response object of assistant.
31
26
  def create_assistant(model, instructions)
32
- url = @openai_url
33
- headers = {
34
- "Authorization": "Bearer #{@openai_api_key}",
35
- "OpenAI-Beta": "assistants=v1",
36
- "Content-Type": "application/json"
37
- }
27
+ puts default_headers
28
+ url = URI.parse(@openai_url)
38
29
  req_body = {
39
30
  "instructions": instructions,
40
31
  "name": "assistant",
41
32
  "tools": [{ "type": "code_interpreter" }],
42
33
  "model": model
43
- }
44
- begin
45
- resp = RestClient.post(url, req_body.to_json, headers)
46
- rescue RestClient::ExceptionWithResponse => e
47
- resp = e.response
48
- end
49
- unless resp.code == 200
50
- parsed = JSON.parse(resp.body)
51
- return parsed["error"]["code"]
52
- end
53
- 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))
54
39
  end
55
40
 
56
41
  # @param assistant_id [String] The id of assistant after create
57
42
  # @return [Openai::AssistantObj] A new response object of assistant.
58
43
  def retrieve_assistant(assistant_id)
59
44
  url = "#{@openai_url}/#{assistant_id}"
60
- headers = {
61
- "Authorization": "Bearer #{@openai_api_key}",
62
- "OpenAI-Beta": "assistants=v1",
63
- "Content-Type": "application/json"
64
- }
65
- begin
66
- resp = RestClient.get(url, headers)
67
- rescue RestClient::ExceptionWithResponse => e
68
- resp = e.response
69
- end
70
- unless resp.code == 200
71
- parsed = JSON.parse(resp.body)
72
- return parsed["error"]["code"]
73
- end
74
- 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))
75
50
  end
76
51
 
77
52
  # @param assistant_id [String] The id of assistant after create
78
53
  # @return [String] Message delete the assistant ok or not
79
54
  def delete_assistant(assistant_id)
80
55
  url = "#{@openai_url}/#{assistant_id}"
81
- headers = {
82
- "Authorization": "Bearer #{@openai_api_key}",
83
- "OpenAI-Beta": "assistants=v1",
84
- "Content-Type": "application/json"
85
- }
86
- begin
87
- resp = RestClient.delete(url, headers)
88
- rescue RestClient::ExceptionWithResponse => e
89
- resp = e.response
90
- end
91
- parsed = JSON.parse(resp.body)
92
- 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)
93
61
 
94
62
  parsed["deleted"]
95
63
  end
@@ -97,19 +65,11 @@ module Openai
97
65
  # @return [Array<Openai::AssistantObj>] List all assistant
98
66
  def list_assistant
99
67
  url = @openai_url
100
- headers = {
101
- "Authorization": "Bearer #{@openai_api_key}",
102
- "OpenAI-Beta": "assistants=v1",
103
- "Content-Type": "application/json"
104
- }
105
- begin
106
- resp = RestClient.get(url, headers)
107
- rescue RestClient::ExceptionWithResponse => e
108
- resp = e.response
109
- end
110
- parsed = JSON.parse(resp.body)
111
- 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?
112
71
 
72
+ parsed = JSON.parse(response.body)
113
73
  assistants = []
114
74
  parsed["data"].each do |ast|
115
75
  assistants << parse_assistant_object(ast)
@@ -131,5 +91,36 @@ module Openai
131
91
  metadata: data["metadata"]
132
92
  )
133
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
134
125
  end
135
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.4.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/