openai-assistant 0.1.0 → 0.4.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 +17 -26
- data/lib/openai/assistant/version.rb +1 -1
- data/lib/openai/assistant.rb +47 -7
- 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: 47c4f3c25796b691c23177c0305accd4d4eb10d13f3d5dd8d5828d6070a7f801
|
4
|
+
data.tar.gz: 22cdf48b820eed695c042ecb9f5892ff8cc05c78547a6d98c95de0a7591c1435
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be4e756a8888af5c0b1f0247a6a17bd1e02cbdc636f43f28b84c2a7c91dd1644d981357c8ead3d81b6cb5831f35b220a6e881d8fbefb5aaa36f0349130995ff9
|
7
|
+
data.tar.gz: 25566590081132fb1526f6970a472b2ca348881931e8052270aad14557fa53fb6e3020f56705866ed488dac04947d69bba7c32ea3b99f5eb3c3f355c0fc1f65a
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -1,39 +1,30 @@
|
|
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).
|
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)`
|
32
20
|
|
33
|
-
|
21
|
+
+ `instance.retrieve_assistant(assistant_id)`
|
34
22
|
|
35
|
-
|
23
|
+
+ `instance.delete_assistant(assistant_id)`
|
36
24
|
|
37
|
-
|
25
|
+
+ `instance.list_assistant()`
|
38
26
|
|
39
|
-
|
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,7 +2,6 @@
|
|
2
2
|
|
3
3
|
require_relative "assistant/version"
|
4
4
|
require_relative "assistant_obj"
|
5
|
-
require "uri"
|
6
5
|
require "json"
|
7
6
|
require "net/http"
|
8
7
|
require "rest-client"
|
@@ -10,12 +9,26 @@ module Openai
|
|
10
9
|
# An openai assistant
|
11
10
|
class Assistant
|
12
11
|
@openai_api_key = nil
|
13
|
-
@openai_url =
|
14
|
-
|
12
|
+
@openai_url = nil
|
13
|
+
|
14
|
+
# @param api_key [String] The api key of openai
|
15
|
+
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"
|
20
|
+
end
|
21
|
+
|
22
|
+
# @param api_key [String] The api key of openai
|
23
|
+
def self.setup(api_key = "")
|
15
24
|
@openai_api_key = api_key
|
25
|
+
@openai_url = "https://api.openai.com/v1/assistants"
|
16
26
|
end
|
17
27
|
|
18
|
-
|
28
|
+
# @param model [String] Select model of the assistant. refer on: https://platform.openai.com/docs/api-reference/models/list.
|
29
|
+
# @param instructions [String] The system instructions that the assistant uses.
|
30
|
+
# @return [Openai::AssistantObj] A new response object of assistant.
|
31
|
+
def create_assistant(model, instructions)
|
19
32
|
url = @openai_url
|
20
33
|
headers = {
|
21
34
|
"Authorization": "Bearer #{@openai_api_key}",
|
@@ -40,7 +53,9 @@ module Openai
|
|
40
53
|
parse_assistant_object(JSON.parse(resp.body))
|
41
54
|
end
|
42
55
|
|
43
|
-
|
56
|
+
# @param assistant_id [String] The id of assistant after create
|
57
|
+
# @return [Openai::AssistantObj] A new response object of assistant.
|
58
|
+
def retrieve_assistant(assistant_id)
|
44
59
|
url = "#{@openai_url}/#{assistant_id}"
|
45
60
|
headers = {
|
46
61
|
"Authorization": "Bearer #{@openai_api_key}",
|
@@ -59,7 +74,9 @@ module Openai
|
|
59
74
|
parse_assistant_object(JSON.parse(resp.body))
|
60
75
|
end
|
61
76
|
|
62
|
-
|
77
|
+
# @param assistant_id [String] The id of assistant after create
|
78
|
+
# @return [String] Message delete the assistant ok or not
|
79
|
+
def delete_assistant(assistant_id)
|
63
80
|
url = "#{@openai_url}/#{assistant_id}"
|
64
81
|
headers = {
|
65
82
|
"Authorization": "Bearer #{@openai_api_key}",
|
@@ -77,7 +94,30 @@ module Openai
|
|
77
94
|
parsed["deleted"]
|
78
95
|
end
|
79
96
|
|
80
|
-
|
97
|
+
# @return [Array<Openai::AssistantObj>] List all assistant
|
98
|
+
def list_assistant
|
99
|
+
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
|
112
|
+
|
113
|
+
assistants = []
|
114
|
+
parsed["data"].each do |ast|
|
115
|
+
assistants << parse_assistant_object(ast)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# @return private
|
120
|
+
def parse_assistant_object(data)
|
81
121
|
Openai::AssistantObj.new(
|
82
122
|
id: data["id"],
|
83
123
|
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.4.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
|