openai-assistant 0.3.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -1
- data/README.md +6 -1
- data/lib/openai/assistant/version.rb +2 -2
- data/lib/openai/assistant.rb +59 -71
- data/lib/openai/base.rb +16 -0
- data/openai-assistant.gemspec +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4eeb3d8b4d984070b698e6dbaf0cc49da89e42e44f765b2fc7327f36e2cfb012
|
4
|
+
data.tar.gz: 4779ca985f33430bf684077e80dcaa4cf4faa11f3d836a996c760c42c6c6998f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 173ba5925ce44d502ca2957488d04fcd42d69dc2a4fa5bed56525966abff7728f39ef131c76fa3030e1801f43c68ddbee994bb83a403281c0d34fa8dae1e0e5a
|
7
|
+
data.tar.gz: 65cacb1f2f1a1de42f577d0c914df1029f4873ea8d4b1300e26aa7803adf8662889f552986100eae93e0652468d13e24de066b601159a3742ca178b003e8f32a
|
data/.rubocop.yml
CHANGED
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/
|
data/lib/openai/assistant.rb
CHANGED
@@ -2,97 +2,62 @@
|
|
2
2
|
|
3
3
|
require_relative "assistant/version"
|
4
4
|
require_relative "assistant_obj"
|
5
|
-
|
5
|
+
require_relative "base"
|
6
6
|
require "json"
|
7
7
|
require "net/http"
|
8
|
-
require "
|
8
|
+
require "uri"
|
9
|
+
|
9
10
|
module Openai
|
10
11
|
# An openai assistant
|
11
|
-
class Assistant
|
12
|
-
@
|
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
|
-
|
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
|
-
|
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
|
-
|
36
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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
|
-
|
104
|
-
|
105
|
-
|
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
|
data/lib/openai/base.rb
ADDED
@@ -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
|
data/openai-assistant.gemspec
CHANGED
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
|
+
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-
|
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/
|