openai-assistant 0.1.0 → 0.3.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 +4 -4
- data/README.md +13 -27
- data/lib/openai/assistant/version.rb +1 -1
- data/lib/openai/assistant.rb +49 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e4ddb6b9bd825412979c3e7a33935200236dff748ce439c8c5ac29fb7dfadc6c
|
4
|
+
data.tar.gz: c0c4fa73e072bfc18daa545ce8b06243d581adc3a5d948ffad68e311016de6c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67d7e18366125045cfbdb2180955b337a31b361eb8079c118a8fa8ca919b9d78878bf7879d910a2d942fdb3d5d85a23adbbf67d276974caf86d3c0c5223e0e7a
|
7
|
+
data.tar.gz: e3dab4d30f625f79d93f424641640f74089915b103dc7e2fa32d1995f29fdd5805cd402b73e639db6eef70f8b8c608dd36f669a3e9b41847997bf686480b3ee8
|
data/README.md
CHANGED
@@ -1,39 +1,25 @@
|
|
1
1
|
# Openai::Assistant
|
2
|
+
This project/gem is for Ruby interact with OpenAI Assistant throught rest api.
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/openai/assistant`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
The client to call is: `Openai::Assistant`
|
6
5
|
|
7
6
|
## Installation
|
7
|
+
Install throught RubyGems server by below step:
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
Install the gem and add to the application's Gemfile by executing:
|
12
|
-
|
13
|
-
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
9
|
+
`$ bundle add openai/assistant`.
|
14
10
|
|
15
|
-
|
16
|
-
|
17
|
-
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
11
|
+
`$ bundle install openai/assistant`
|
18
12
|
|
19
13
|
## Usage
|
14
|
+
You can direct interact with gem by:
|
20
15
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
26
|
-
|
27
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
28
|
-
|
29
|
-
## Contributing
|
30
|
-
|
31
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/openai-assistant. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/openai-assistant/blob/main/CODE_OF_CONDUCT.md).
|
32
|
-
|
33
|
-
## License
|
16
|
+
- use the gem `require "openai/assistant"`
|
17
|
+
- setup the api key `instance =Openai::Assistant::new(${API_KEY})`
|
18
|
+
- interact with assistant:
|
19
|
+
+ `instance.create_assistant(model, instruction)`
|
34
20
|
|
35
|
-
|
21
|
+
+ `instance.retrieve_assistant(assistant_id)`
|
36
22
|
|
37
|
-
|
23
|
+
+ `instance.delete_assistant(assistant_id)`
|
38
24
|
|
39
|
-
|
25
|
+
+ `instance.list_assistant()`
|
data/lib/openai/assistant.rb
CHANGED
@@ -10,12 +10,28 @@ module Openai
|
|
10
10
|
# An openai assistant
|
11
11
|
class Assistant
|
12
12
|
@openai_api_key = nil
|
13
|
-
@openai_url =
|
14
|
-
|
13
|
+
@openai_url = nil
|
14
|
+
|
15
|
+
# @param api_key [String] The api key of openai
|
16
|
+
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"
|
21
|
+
end
|
22
|
+
|
23
|
+
# @param api_key [String] The api key of openai
|
24
|
+
def self.setup(api_key = "")
|
15
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"
|
16
29
|
end
|
17
30
|
|
18
|
-
|
31
|
+
# @param model [String] Select model of the assistant. refer on: https://platform.openai.com/docs/api-reference/models/list.
|
32
|
+
# @param instructions [String] The system instructions that the assistant uses.
|
33
|
+
# @return [Openai::AssistantObj] A new response object of assistant.
|
34
|
+
def create_assistant(model, instructions)
|
19
35
|
url = @openai_url
|
20
36
|
headers = {
|
21
37
|
"Authorization": "Bearer #{@openai_api_key}",
|
@@ -40,7 +56,9 @@ module Openai
|
|
40
56
|
parse_assistant_object(JSON.parse(resp.body))
|
41
57
|
end
|
42
58
|
|
43
|
-
|
59
|
+
# @param assistant_id [String] The id of assistant after create
|
60
|
+
# @return [Openai::AssistantObj] A new response object of assistant.
|
61
|
+
def retrieve_assistant(assistant_id)
|
44
62
|
url = "#{@openai_url}/#{assistant_id}"
|
45
63
|
headers = {
|
46
64
|
"Authorization": "Bearer #{@openai_api_key}",
|
@@ -59,7 +77,9 @@ module Openai
|
|
59
77
|
parse_assistant_object(JSON.parse(resp.body))
|
60
78
|
end
|
61
79
|
|
62
|
-
|
80
|
+
# @param assistant_id [String] The id of assistant after create
|
81
|
+
# @return [String] Message delete the assistant ok or not
|
82
|
+
def delete_assistant(assistant_id)
|
63
83
|
url = "#{@openai_url}/#{assistant_id}"
|
64
84
|
headers = {
|
65
85
|
"Authorization": "Bearer #{@openai_api_key}",
|
@@ -77,7 +97,30 @@ module Openai
|
|
77
97
|
parsed["deleted"]
|
78
98
|
end
|
79
99
|
|
80
|
-
|
100
|
+
# @return [Array<Openai::AssistantObj>] List all assistant
|
101
|
+
def list_assistant
|
102
|
+
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
|
115
|
+
|
116
|
+
assistants = []
|
117
|
+
parsed["data"].each do |ast|
|
118
|
+
assistants << parse_assistant_object(ast)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# @return private
|
123
|
+
def parse_assistant_object(data)
|
81
124
|
Openai::AssistantObj.new(
|
82
125
|
id: data["id"],
|
83
126
|
object: data["object"],
|
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.3.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-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|