openai-assistant 0.6.0 → 1.0.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: 30d997127785423573776d064b5a0096f121df245af1c56511be53563b342505
4
- data.tar.gz: 166a91a99f0caf0f5b08c9de791531c9f16c9493d2ffa1395db11237c982f620
3
+ metadata.gz: 44a58132dffbb0bb717d0c0f5c5760a222f88fde9f7cc9721e8ed0d91788e5bb
4
+ data.tar.gz: 45c2cebb8c681541526685d0c2ce90631cff61c7d0d7a72eb85b93c17b7fa23d
5
5
  SHA512:
6
- metadata.gz: c5f306812242afaac48bd3da55e0280a36877238052ebdd5e75942d68bdf68cdbfc9a81d1c0ebeb2b9d8abd4b10644bdfaef37b1b22ba8580a5a00222a597693
7
- data.tar.gz: 8aa5092ad31e0372595baf420020f8d953a500dce327cab67c9344221c7aceca230d4150ef11637304f8558eebcd7d32506ceb533a7de1f7e7647dd5c53dac96
6
+ metadata.gz: 37399c1452826279d1065a4a0829b0077fe017283e51b796055ff2830740bb668449ec90ec77109155c7b84d617fc7bd8643870e40537f4b77b9e13da90b98cc
7
+ data.tar.gz: a59828b4b77ea422d0aabfb2adcef8e499f5300514c42e4e45f69e09394b00234164cce72c6e25d597aa15396ab9dc9e2189a6bc3dc37e3cc617adc3f7268d9e
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
- # Openai::Assistant
1
+ # OpenaiAsissistant::Assistant
2
2
  This project/gem is for Ruby interact with OpenAI Assistant throught rest api.
3
3
 
4
- The client to call is: `Openai::Assistant`
4
+ The client to call is: `OpenaiAsissistant::Assistant`
5
5
 
6
6
  ## Installation
7
7
  Install throught RubyGems server by below step:
@@ -14,7 +14,7 @@ Install throught RubyGems server by below step:
14
14
  You can direct interact with gem by:
15
15
 
16
16
  - use the gem `require "openai/assistant"`
17
- - setup the api key `instance =Openai::Assistant::new(${API_KEY})`
17
+ - setup the api key `instance =OpenaiAsissistant::Assistant::new(${API_KEY})`
18
18
  - interact with assistant:
19
19
  + `instance.create_assistant(model, instruction)`
20
20
 
@@ -1,20 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "../http/http"
4
-
5
- module Openai
3
+ module OpenaiAsissistant
6
4
  # Base class of openai
7
5
  class Base
8
- @openai_api_key = nil
9
- @openai_url = nil
10
- @http_client = nil
11
-
12
6
  def initialize(api_key = "")
13
7
  @openai_api_key = api_key
14
8
  # hard the host because if the official docs change the host, maybe it will change another
15
9
  # we need to update this gem for any change
16
10
  @openai_url = "https://api.openai.com/v1/assistants"
17
- @http_client = Http::Client.new
11
+ @http_client = OpenaiAsissistant::HTTPClient.new
12
+ end
13
+
14
+ def default_headers
15
+ {
16
+ "Authorization": "Bearer #{@openai_api_key}",
17
+ "OpenAI-Beta": "assistants=v1",
18
+ "Content-Type": "application/json"
19
+ }
18
20
  end
19
21
  end
20
22
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenaiAsissistant
4
+ module Assistant
5
+ # An openai assistant client
6
+ class Client
7
+ attr_accessor :api_key
8
+
9
+ def initialize(api_key)
10
+ @api_key = api_key
11
+ end
12
+
13
+ def create_assistant(model, instructions)
14
+ OpenaiAsissistant::Assistant::Create.new(@api_key).create_assistant(model, instructions)
15
+ end
16
+
17
+ def retrieve_assistant(assistant_id)
18
+ OpenaiAsissistant::Assistant::Retrieve.new(@api_key).retrieve_assistant(assistant_id)
19
+ end
20
+
21
+ def list_assistant
22
+ OpenaiAsissistant::Assistant::List.new(@api_key).list_assistant
23
+ end
24
+
25
+ def delete_assistant(assistant_id)
26
+ OpenaiAsissistant::Assistant::Delete.new(@api_key).delete_assistant(assistant_id)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenaiAsissistant
4
+ module Assistant
5
+ # An openai assistant
6
+ class Create < Base
7
+ # @param model [String] Select model of the assistant. refer on: https://platform.openai.com/docs/api-reference/models/list.
8
+ # @param instructions [String] The system instructions that the assistant uses.
9
+ # @return [OpenaiAsissistant::Mapper::Assistant] A new response object of assistant.
10
+ def create_assistant(model, instructions)
11
+ url = URI.parse(@openai_url)
12
+ req_body = {
13
+ "instructions": instructions,
14
+ "name": "assistant",
15
+ "tools": [{ "type": "code_interpreter" }],
16
+ "model": model
17
+ }.to_json
18
+ response = @http_client.call_post(url, req_body, default_headers)
19
+ return OpenaiAsissistant::ErrorResponse.from_json(response.body) unless response.code == "200"
20
+
21
+ OpenaiAsissistant::Mapper::Assistant.from_json(JSON.parse(response.body))
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenaiAsissistant
4
+ module Assistant
5
+ # An openai assistant
6
+ class Delete < Base
7
+ # @param assistant_id [String] The id of assistant after create
8
+ # @return [String] Message delete the assistant ok or not
9
+ def delete_assistant(assistant_id)
10
+ url = "#{@openai_url}/#{assistant_id}"
11
+ uri = URI(url)
12
+ response = @http_client.call_delete(uri, default_headers)
13
+ return OpenaiAsissistant::ErrorResponse.from_json(response.body) unless response.code == "200"
14
+
15
+ true
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenaiAsissistant
4
+ module Assistant
5
+ # An openai assistant
6
+ class List < Base
7
+ # @return [Array<OpenaiAsissistant::Mapper::Assistant>] List all assistant
8
+ def list_assistant
9
+ url = @openai_url
10
+ uri = URI(url)
11
+ response = @http_client.call_get(uri, default_headers)
12
+ return OpenaiAsissistant::ErrorResponse.from_json(response.body) unless response.code == "200"
13
+
14
+ parsed = JSON.parse(response.body)
15
+ assistants = []
16
+ parsed["data"].each do |ast|
17
+ assistants << OpenaiAsissistant::Mapper::Assistant.from_json(ast)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenaiAsissistant
4
+ module Assistant
5
+ # An openai assistant
6
+ class Retrieve < Base
7
+ # @param assistant_id [String] The id of assistant after create
8
+ # @return [OpenaiAsissistant::Mapper::OpenaiAsissistant] A new response object of assistant.
9
+ def retrieve_assistant(assistant_id)
10
+ url = "#{@openai_url}/#{assistant_id}"
11
+ uri = URI(url)
12
+ response = @http_client.call_get(uri, default_headers)
13
+ return OpenaiAsissistant::ErrorResponse.from_json(response.body) unless response.code == "200"
14
+
15
+ OpenaiAsissistant::Mapper::Assistant.from_json(JSON.parse(response.body))
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenaiAsissistant
4
+ # An http client
5
+ class HTTPClient
6
+ # disable_ssl instead of ssl because almost the host is https
7
+ def call_post(url, req_body, headers, disable_ssl: false)
8
+ http = Net::HTTP.new(url.host, url.port)
9
+ http.use_ssl = disable_ssl ? false : true
10
+ request = Net::HTTP::Post.new(url.path, headers)
11
+ request.body = req_body unless req_body.nil?
12
+ http.request(request)
13
+ end
14
+
15
+ def call_delete(url, headers, disable_ssl: false)
16
+ http = Net::HTTP.new(url.host, url.port)
17
+ http.use_ssl = disable_ssl ? false : true
18
+ request = Net::HTTP::Delete.new(url.path, headers)
19
+ http.request(request)
20
+ end
21
+
22
+ def call_get(url, headers, disable_ssl: false)
23
+ http = Net::HTTP.new(url.host, url.port)
24
+ http.use_ssl = disable_ssl ? false : true
25
+ request = Net::HTTP::Get.new(url.path, headers)
26
+ http.request(request)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenaiAsissistant
4
+ # A error response of openai
5
+ class ErrorResponse
6
+ # @return [String] error message
7
+ attr_accessor :message
8
+ # @return [String] type of error
9
+ attr_accessor :type
10
+ # @return [String] parameter that caused the error
11
+ attr_accessor :param
12
+ # @return [String] error code of openai
13
+ attr_accessor :code
14
+
15
+ def initialize(**args)
16
+ args.each do |k, v|
17
+ instance_variable_set("@#{k}", v) unless v.nil?
18
+ end
19
+ end
20
+
21
+ def self.from_json(response_body)
22
+ data = JSON.parse(response_body)["error"]
23
+ OpenaiAsissistant::ErrorResponse.new(
24
+ message: data["message"],
25
+ type: data["type"],
26
+ param: data["param"],
27
+ code: data["code"]
28
+ )
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenaiAsissistant
4
+ # A mapper of assistant
5
+ module Mapper
6
+ # A object model struct of assistant
7
+ class Assistant
8
+ # @return [String] The identifier, which can be referenced in API endpoints.
9
+ attr_accessor :id
10
+ # @return [String] The object type, which is always assistant.
11
+ attr_accessor :object
12
+ # @return [Integer] The Unix timestamp (in seconds) for when the assistant was created.
13
+ attr_accessor :created_at
14
+ # @return [String] The name of the assistant. The maximum length is 256 characters.
15
+ attr_accessor :name
16
+ # @return [String] The description of the assistant. The maximum length is 512 characters.
17
+ attr_accessor :description
18
+ # @return [String] ID of the model to use. Use the List models API to see all available models
19
+ attr_accessor :model
20
+ # @return [String] The system instructions that the assistant uses. The maximum length is 32768 characters.
21
+ attr_accessor :instructions
22
+ # @return [String] A list of tool enabled on the assistant.
23
+ attr_accessor :tools
24
+ # @return [String] A list of file IDs attached to this assistant.
25
+ attr_accessor :file_ids
26
+ # @return [String] Set of 16 key-value pairs that can be attached to an object.
27
+ attr_accessor :metadata
28
+
29
+ def initialize(**args)
30
+ args.each do |k, v|
31
+ instance_variable_set("@#{k}", v) unless v.nil?
32
+ end
33
+ end
34
+
35
+ def self.from_json(data)
36
+ OpenaiAsissistant::Mapper::Assistant.new(
37
+ id: data["id"],
38
+ object: data["object"],
39
+ created_at: data["created_at"],
40
+ name: data["name"],
41
+ description: data["description"],
42
+ model: data["model"],
43
+ instructions: data["instructions"],
44
+ tools: data["tools"],
45
+ file_ids: data["file_ids"],
46
+ metadata: data["metadata"]
47
+ )
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenaiAsissistant
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+ require "uri"
6
+ require_relative "openai_assistant/clients/http/http"
7
+ require_relative "openai_assistant/mappers/assistant"
8
+ require_relative "openai_assistant/error_response"
9
+ require_relative "openai_assistant/version"
10
+ require_relative "openai_assistant/base"
11
+ require_relative "openai_assistant/client"
12
+ require_relative "openai_assistant/clients/assistant/create"
13
+ require_relative "openai_assistant/clients/assistant/retrieve"
14
+ require_relative "openai_assistant/clients/assistant/delete"
15
+ require_relative "openai_assistant/clients/assistant/list"
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "lib/openai/base"
4
- require_relative "lib/openai/assistant/version"
3
+ require_relative "lib/openai_assistant/base"
4
+ require_relative "lib/openai_assistant/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "openai-assistant"
8
- spec.version = Openai::Assistant::VERSION
8
+ spec.version = OpenaiAsissistant::VERSION
9
9
  spec.authors = ["duonghds"]
10
10
  spec.email = ["duong.hoang@employmenthero.com"]
11
11
 
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.6.0
4
+ version: 1.0.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-30 00:00:00.000000000 Z
11
+ date: 2023-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -38,11 +38,17 @@ files:
38
38
  - LICENSE.txt
39
39
  - README.md
40
40
  - Rakefile
41
- - lib/http/http.rb
42
- - lib/openai/assistant.rb
43
- - lib/openai/assistant/version.rb
44
- - lib/openai/assistant_obj.rb
45
- - lib/openai/base.rb
41
+ - lib/openai_assistant.rb
42
+ - lib/openai_assistant/base.rb
43
+ - lib/openai_assistant/client.rb
44
+ - lib/openai_assistant/clients/assistant/create.rb
45
+ - lib/openai_assistant/clients/assistant/delete.rb
46
+ - lib/openai_assistant/clients/assistant/list.rb
47
+ - lib/openai_assistant/clients/assistant/retrieve.rb
48
+ - lib/openai_assistant/clients/http/http.rb
49
+ - lib/openai_assistant/error_response.rb
50
+ - lib/openai_assistant/mappers/assistant.rb
51
+ - lib/openai_assistant/version.rb
46
52
  - openai-assistant.gemspec
47
53
  - sig/openai/assistant.rbs
48
54
  homepage: https://rubygems.org/
data/lib/http/http.rb DELETED
@@ -1,31 +0,0 @@
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
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Openai
4
- class Assistant < Openai::Base
5
- VERSION = "0.6.0"
6
- end
7
- end
@@ -1,107 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "assistant/version"
4
- require_relative "assistant_obj"
5
- require_relative "base"
6
- require_relative "../http/http"
7
- require "json"
8
- require "net/http"
9
- require "uri"
10
-
11
- module Openai
12
- # An openai assistant
13
- class Assistant < Base
14
- # @param api_key [String] The api key of openai\
15
- def initialize(api_key = "")
16
- super(api_key)
17
- end
18
-
19
- # @param api_key [String] The api key of openai
20
- def self.setup(api_key = "")
21
- initialize(api_key)
22
- end
23
-
24
- # @param model [String] Select model of the assistant. refer on: https://platform.openai.com/docs/api-reference/models/list.
25
- # @param instructions [String] The system instructions that the assistant uses.
26
- # @return [Openai::AssistantObj] A new response object of assistant.
27
- def create_assistant(model, instructions)
28
- url = URI.parse(@openai_url)
29
- req_body = {
30
- "instructions": instructions,
31
- "name": "assistant",
32
- "tools": [{ "type": "code_interpreter" }],
33
- "model": model
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)
38
- return parsed["error"]["code"]
39
- end
40
- parse_assistant_object(JSON.parse(response.body))
41
- end
42
-
43
- # @param assistant_id [String] The id of assistant after create
44
- # @return [Openai::AssistantObj] A new response object of assistant.
45
- def retrieve_assistant(assistant_id)
46
- url = "#{@openai_url}/#{assistant_id}"
47
- uri = URI(url)
48
- response = @http_client.call_get(uri, default_headers)
49
- unless response.code == "200"
50
- parsed = JSON.parse(response.body)
51
- return parsed["error"]["code"]
52
- end
53
- parse_assistant_object(JSON.parse(response.body))
54
- end
55
-
56
- # @param assistant_id [String] The id of assistant after create
57
- # @return [String] Message delete the assistant ok or not
58
- def delete_assistant(assistant_id)
59
- url = "#{@openai_url}/#{assistant_id}"
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"
64
-
65
- parsed["deleted"]
66
- end
67
-
68
- # @return [Array<Openai::AssistantObj>] List all assistant
69
- def list_assistant
70
- url = @openai_url
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"
75
-
76
- assistants = []
77
- parsed["data"].each do |ast|
78
- assistants << parse_assistant_object(ast)
79
- end
80
- end
81
-
82
- private
83
-
84
- def parse_assistant_object(data)
85
- Openai::AssistantObj.new(
86
- id: data["id"],
87
- object: data["object"],
88
- created_at: data["created_at"],
89
- name: data["name"],
90
- description: data["description"],
91
- model: data["model"],
92
- instructions: data["instructions"],
93
- tools: data["tools"],
94
- file_ids: data["file_ids"],
95
- metadata: data["metadata"]
96
- )
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
106
- end
107
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Openai
4
- # A object model struct of assistant
5
- class AssistantObj
6
- # @return [String] The identifier, which can be referenced in API endpoints.
7
- attr_accessor :id
8
- # @return [String] The object type, which is always assistant.
9
- attr_accessor :object
10
- # @return [Integer] The Unix timestamp (in seconds) for when the assistant was created.
11
- attr_accessor :created_at
12
- # @return [String] The name of the assistant. The maximum length is 256 characters.
13
- attr_accessor :name
14
- # @return [String] The description of the assistant. The maximum length is 512 characters.
15
- attr_accessor :description
16
- # @return [String] ID of the model to use. Use the List models API to see all available models
17
- attr_accessor :model
18
- # @return [String] The system instructions that the assistant uses. The maximum length is 32768 characters.
19
- attr_accessor :instructions
20
- # @return [String] A list of tool enabled on the assistant.
21
- attr_accessor :tools
22
- # @return [String] A list of file IDs attached to this assistant.
23
- attr_accessor :file_ids
24
- # @return [String] Set of 16 key-value pairs that can be attached to an object.
25
- attr_accessor :metadata
26
-
27
- def initialize(**args)
28
- args.each do |k, v|
29
- instance_variable_set("@#{k}", v) unless v.nil?
30
- end
31
- end
32
- end
33
- end