Class: Openai::Assistant
- Inherits:
-
Object
- Object
- Openai::Assistant
- Defined in:
- lib/openai/assistant.rb,
lib/openai/assistant/version.rb
Overview
An openai assistant
Constant Summary collapse
- VERSION =
"0.4.0"
Class Method Summary collapse
Instance Method Summary collapse
-
#create_assistant(model, instructions) ⇒ Openai::AssistantObj
A new response object of assistant.
-
#delete_assistant(assistant_id) ⇒ String
Message delete the assistant ok or not.
-
#initialize(api_key = "") ⇒ Assistant
constructor
A new instance of Assistant.
-
#list_assistant ⇒ Array<Openai::AssistantObj>
List all assistant.
-
#parse_assistant_object(data) ⇒ Object
Private.
-
#retrieve_assistant(assistant_id) ⇒ Openai::AssistantObj
A new response object of assistant.
Constructor Details
#initialize(api_key = "") ⇒ Assistant
Returns a new instance of Assistant.
15 16 17 18 19 20 |
# File 'lib/openai/assistant.rb', line 15 def initialize(api_key = "") @openai_api_key = api_key # hard the host because if the official docs change the host, maybe it will change another # we need to update this gem for any change @openai_url = "https://api.openai.com/v1/assistants" end |
Class Method Details
.setup(api_key = "") ⇒ Object
23 24 25 26 |
# File 'lib/openai/assistant.rb', line 23 def self.setup(api_key = "") @openai_api_key = api_key @openai_url = "https://api.openai.com/v1/assistants" end |
Instance Method Details
#create_assistant(model, instructions) ⇒ Openai::AssistantObj
Returns A new response object of assistant.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/openai/assistant.rb', line 31 def create_assistant(model, instructions) url = @openai_url headers = { "Authorization": "Bearer #{@openai_api_key}", "OpenAI-Beta": "assistants=v1", "Content-Type": "application/json" } req_body = { "instructions": instructions, "name": "assistant", "tools": [{ "type": "code_interpreter" }], "model": model } begin resp = RestClient.post(url, req_body.to_json, headers) rescue RestClient::ExceptionWithResponse => e resp = e.response end unless resp.code == 200 parsed = JSON.parse(resp.body) return parsed["error"]["code"] end parse_assistant_object(JSON.parse(resp.body)) end |
#delete_assistant(assistant_id) ⇒ String
Returns Message delete the assistant ok or not.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/openai/assistant.rb', line 79 def delete_assistant(assistant_id) url = "#{@openai_url}/#{assistant_id}" headers = { "Authorization": "Bearer #{@openai_api_key}", "OpenAI-Beta": "assistants=v1", "Content-Type": "application/json" } begin resp = RestClient.delete(url, headers) rescue RestClient::ExceptionWithResponse => e resp = e.response end parsed = JSON.parse(resp.body) return parsed["error"]["code"] unless resp.code == 200 parsed["deleted"] end |
#list_assistant ⇒ Array<Openai::AssistantObj>
Returns List all assistant.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/openai/assistant.rb', line 98 def list_assistant url = @openai_url headers = { "Authorization": "Bearer #{@openai_api_key}", "OpenAI-Beta": "assistants=v1", "Content-Type": "application/json" } begin resp = RestClient.get(url, headers) rescue RestClient::ExceptionWithResponse => e resp = e.response end parsed = JSON.parse(resp.body) return parsed["error"]["code"] unless resp.code == 200 assistants = [] parsed["data"].each do |ast| assistants << parse_assistant_object(ast) end end |
#parse_assistant_object(data) ⇒ Object
Returns private.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/openai/assistant.rb', line 120 def parse_assistant_object(data) Openai::AssistantObj.new( id: data["id"], object: data["object"], created_at: data["created_at"], name: data["name"], description: data["description"], model: data["model"], instructions: data["instructions"], tools: data["tools"], file_ids: data["file_ids"], metadata: data["metadata"] ) end |
#retrieve_assistant(assistant_id) ⇒ Openai::AssistantObj
Returns A new response object of assistant.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/openai/assistant.rb', line 58 def retrieve_assistant(assistant_id) url = "#{@openai_url}/#{assistant_id}" headers = { "Authorization": "Bearer #{@openai_api_key}", "OpenAI-Beta": "assistants=v1", "Content-Type": "application/json" } begin resp = RestClient.get(url, headers) rescue RestClient::ExceptionWithResponse => e resp = e.response end unless resp.code == 200 parsed = JSON.parse(resp.body) return parsed["error"]["code"] end parse_assistant_object(JSON.parse(resp.body)) end |