openai-assistant 0.4.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/http/http.rb +31 -0
- data/lib/openai/assistant/version.rb +2 -2
- data/lib/openai/assistant.rb +37 -65
- data/lib/openai/base.rb +20 -0
- data/openai-assistant.gemspec +1 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30d997127785423573776d064b5a0096f121df245af1c56511be53563b342505
|
4
|
+
data.tar.gz: 166a91a99f0caf0f5b08c9de791531c9f16c9493d2ffa1395db11237c982f620
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/openai/assistant.rb
CHANGED
@@ -2,94 +2,65 @@
|
|
2
2
|
|
3
3
|
require_relative "assistant/version"
|
4
4
|
require_relative "assistant_obj"
|
5
|
+
require_relative "base"
|
6
|
+
require_relative "../http/http"
|
5
7
|
require "json"
|
6
8
|
require "net/http"
|
7
|
-
require "
|
9
|
+
require "uri"
|
10
|
+
|
8
11
|
module Openai
|
9
12
|
# An openai assistant
|
10
|
-
class Assistant
|
11
|
-
@
|
12
|
-
@openai_url = nil
|
13
|
-
|
14
|
-
# @param api_key [String] The api key of openai
|
13
|
+
class Assistant < Base
|
14
|
+
# @param api_key [String] The api key of openai\
|
15
15
|
def initialize(api_key = "")
|
16
|
-
|
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"
|
16
|
+
super(api_key)
|
20
17
|
end
|
21
18
|
|
22
19
|
# @param api_key [String] The api key of openai
|
23
20
|
def self.setup(api_key = "")
|
24
|
-
|
25
|
-
@openai_url = "https://api.openai.com/v1/assistants"
|
21
|
+
initialize(api_key)
|
26
22
|
end
|
27
23
|
|
28
24
|
# @param model [String] Select model of the assistant. refer on: https://platform.openai.com/docs/api-reference/models/list.
|
29
25
|
# @param instructions [String] The system instructions that the assistant uses.
|
30
26
|
# @return [Openai::AssistantObj] A new response object of assistant.
|
31
27
|
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
|
-
}
|
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
|
-
|
45
|
-
|
46
|
-
|
47
|
-
resp = e.response
|
48
|
-
end
|
49
|
-
unless resp.code == 200
|
50
|
-
parsed = JSON.parse(resp.body)
|
34
|
+
}.to_json
|
35
|
+
response = @http_client.call_post(url, req_body, default_headers)
|
36
|
+
unless response.code == "200"
|
37
|
+
parsed = JSON.parse(response.body)
|
51
38
|
return parsed["error"]["code"]
|
52
39
|
end
|
53
|
-
parse_assistant_object(JSON.parse(
|
40
|
+
parse_assistant_object(JSON.parse(response.body))
|
54
41
|
end
|
55
42
|
|
56
43
|
# @param assistant_id [String] The id of assistant after create
|
57
44
|
# @return [Openai::AssistantObj] A new response object of assistant.
|
58
45
|
def retrieve_assistant(assistant_id)
|
59
46
|
url = "#{@openai_url}/#{assistant_id}"
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
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)
|
47
|
+
uri = URI(url)
|
48
|
+
response = @http_client.call_get(uri, default_headers)
|
49
|
+
unless response.code == "200"
|
50
|
+
parsed = JSON.parse(response.body)
|
72
51
|
return parsed["error"]["code"]
|
73
52
|
end
|
74
|
-
parse_assistant_object(JSON.parse(
|
53
|
+
parse_assistant_object(JSON.parse(response.body))
|
75
54
|
end
|
76
55
|
|
77
56
|
# @param assistant_id [String] The id of assistant after create
|
78
57
|
# @return [String] Message delete the assistant ok or not
|
79
58
|
def delete_assistant(assistant_id)
|
80
59
|
url = "#{@openai_url}/#{assistant_id}"
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
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
|
60
|
+
uri = URI(url)
|
61
|
+
response = @http_client.call_delete(uri, default_headers)
|
62
|
+
parsed = JSON.parse(response.body)
|
63
|
+
return parsed["error"]["code"] unless response.code == "200"
|
93
64
|
|
94
65
|
parsed["deleted"]
|
95
66
|
end
|
@@ -97,18 +68,10 @@ module Openai
|
|
97
68
|
# @return [Array<Openai::AssistantObj>] List all assistant
|
98
69
|
def list_assistant
|
99
70
|
url = @openai_url
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
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
|
71
|
+
uri = URI(url)
|
72
|
+
response = @http_client.call_get(uri, default_headers)
|
73
|
+
parsed = JSON.parse(response.body)
|
74
|
+
return parsed["error"]["code"] unless response.code == "200"
|
112
75
|
|
113
76
|
assistants = []
|
114
77
|
parsed["data"].each do |ast|
|
@@ -116,7 +79,8 @@ module Openai
|
|
116
79
|
end
|
117
80
|
end
|
118
81
|
|
119
|
-
|
82
|
+
private
|
83
|
+
|
120
84
|
def parse_assistant_object(data)
|
121
85
|
Openai::AssistantObj.new(
|
122
86
|
id: data["id"],
|
@@ -131,5 +95,13 @@ module Openai
|
|
131
95
|
metadata: data["metadata"]
|
132
96
|
)
|
133
97
|
end
|
98
|
+
|
99
|
+
def default_headers
|
100
|
+
{
|
101
|
+
"Authorization": "Bearer #{@openai_api_key}",
|
102
|
+
"OpenAI-Beta": "assistants=v1",
|
103
|
+
"Content-Type": "application/json"
|
104
|
+
}
|
105
|
+
end
|
134
106
|
end
|
135
107
|
end
|
data/lib/openai/base.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "../http/http"
|
4
|
+
|
5
|
+
module Openai
|
6
|
+
# Base class of openai
|
7
|
+
class Base
|
8
|
+
@openai_api_key = nil
|
9
|
+
@openai_url = nil
|
10
|
+
@http_client = nil
|
11
|
+
|
12
|
+
def initialize(api_key = "")
|
13
|
+
@openai_api_key = api_key
|
14
|
+
# hard the host because if the official docs change the host, maybe it will change another
|
15
|
+
# we need to update this gem for any change
|
16
|
+
@openai_url = "https://api.openai.com/v1/assistants"
|
17
|
+
@http_client = Http::Client.new
|
18
|
+
end
|
19
|
+
end
|
20
|
+
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.6.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
|
@@ -38,9 +38,11 @@ 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
|
45
|
+
- lib/openai/base.rb
|
44
46
|
- openai-assistant.gemspec
|
45
47
|
- sig/openai/assistant.rbs
|
46
48
|
homepage: https://rubygems.org/
|