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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64fdebd2ebeba89ddc5a70fd0a3c56eb677935138de23e70dbdeafbd91ce5b84
4
- data.tar.gz: f3b25cfae2e3e0dd3149a0da2d16f3719cf1ed54ddc8d1f22e0d6dcb4ab4a7d4
3
+ metadata.gz: 47c4f3c25796b691c23177c0305accd4d4eb10d13f3d5dd8d5828d6070a7f801
4
+ data.tar.gz: 22cdf48b820eed695c042ecb9f5892ff8cc05c78547a6d98c95de0a7591c1435
5
5
  SHA512:
6
- metadata.gz: 0665f422378e79c08cb8e249b10db948216c390159156bbec06f2b253799c1e525fb2a3a54d6c625157509657434b03901e902f7364714d3f253d67050a2fb35
7
- data.tar.gz: 24e349e872037bec3e4ba5f95555cf0835dbee1aa1f496337d9f0cb93a4d21926eae4ca893e5d11fa9a01b5ea16bfcf7d8231f4327a53ad70eed4e28c349002c
6
+ metadata.gz: be4e756a8888af5c0b1f0247a6a17bd1e02cbdc636f43f28b84c2a7c91dd1644d981357c8ead3d81b6cb5831f35b220a6e881d8fbefb5aaa36f0349130995ff9
7
+ data.tar.gz: 25566590081132fb1526f6970a472b2ca348881931e8052270aad14557fa53fb6e3020f56705866ed488dac04947d69bba7c32ea3b99f5eb3c3f355c0fc1f65a
data/.rubocop.yml CHANGED
@@ -16,4 +16,7 @@ Metrics/MethodLength:
16
16
  Max: 100
17
17
 
18
18
  Metrics/BlockLength:
19
- Max: 100
19
+ Max: 100
20
+
21
+ Metrics/ClassLength:
22
+ Max: 500
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
- TODO: Delete this and the text below, and describe your gem
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
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
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
- If bundler is not being used to manage dependencies, install the gem by executing:
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
- TODO: Write usage instructions here
22
-
23
- ## Development
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
- ## License
21
+ + `instance.retrieve_assistant(assistant_id)`
34
22
 
35
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
23
+ + `instance.delete_assistant(assistant_id)`
36
24
 
37
- ## Code of Conduct
25
+ + `instance.list_assistant()`
38
26
 
39
- Everyone interacting in the Openai::Assistant project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/openai-assistant/blob/main/CODE_OF_CONDUCT.md).
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/
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Openai
4
4
  class Assistant
5
- VERSION = "0.1.0"
5
+ VERSION = "0.4.0"
6
6
  end
7
7
  end
@@ -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 = "https://api.openai.com/v1/assistants"
14
- def self.initialize(api_key)
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
- def self.create_assistant(model, instructions)
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
- def self.retrieve_assistant(assistant_id)
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
- def self.delete_assistant(assistant_id)
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
- def self.parse_assistant_object(data)
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.1.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-28 00:00:00.000000000 Z
11
+ date: 2023-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec