Class: Openai::Assistant

Inherits:
Base
  • Object
show all
Defined in:
lib/openai/assistant.rb,
lib/openai/assistant/version.rb

Overview

An openai assistant

Constant Summary collapse

VERSION =
"0.7.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = "") ⇒ Assistant

Returns a new instance of Assistant.

Parameters:

  • api_key (String) (defaults to: "")

    The api key of openai\



15
16
17
# File 'lib/openai/assistant.rb', line 15

def initialize(api_key = "")
  super(api_key)
end

Class Method Details

.setup(api_key = "") ⇒ Object

Parameters:

  • api_key (String) (defaults to: "")

    The api key of openai



20
21
22
# File 'lib/openai/assistant.rb', line 20

def self.setup(api_key = "")
  initialize(api_key)
end

Instance Method Details

#create_assistant(model, instructions) ⇒ Openai::AssistantObj

Returns A new response object of assistant.

Parameters:

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/openai/assistant.rb', line 27

def create_assistant(model, instructions)
  url = URI.parse(@openai_url)
  req_body = {
    "instructions": instructions,
    "name": "assistant",
    "tools": [{ "type": "code_interpreter" }],
    "model": model
  }.to_json
  response = @http_client.call_post(url, req_body, default_headers)
  unless response.code == "200"
    parsed = JSON.parse(response.body)
    return parsed["error"]["code"]
  end
  parse_assistant_object(JSON.parse(response.body))
end

#delete_assistant(assistant_id) ⇒ String

Returns Message delete the assistant ok or not.

Parameters:

  • assistant_id (String)

    The id of assistant after create

Returns:

  • (String)

    Message delete the assistant ok or not



58
59
60
61
62
63
64
65
66
# File 'lib/openai/assistant.rb', line 58

def delete_assistant(assistant_id)
  url = "#{@openai_url}/#{assistant_id}"
  uri = URI(url)
  response = @http_client.call_delete(uri, default_headers)
  parsed = JSON.parse(response.body)
  return parsed["error"]["code"] unless response.code == "200"

  parsed["deleted"]
end

#list_assistantArray<Openai::AssistantObj>

Returns List all assistant.

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/openai/assistant.rb', line 69

def list_assistant
  url = @openai_url
  uri = URI(url)
  response = @http_client.call_get(uri, default_headers)
  parsed = JSON.parse(response.body)
  return parsed["error"]["code"] unless response.code == "200"

  assistants = []
  parsed["data"].each do |ast|
    assistants << parse_assistant_object(ast)
  end
end

#retrieve_assistant(assistant_id) ⇒ Openai::AssistantObj

Returns A new response object of assistant.

Parameters:

  • assistant_id (String)

    The id of assistant after create

Returns:



45
46
47
48
49
50
51
52
53
54
# File 'lib/openai/assistant.rb', line 45

def retrieve_assistant(assistant_id)
  url = "#{@openai_url}/#{assistant_id}"
  uri = URI(url)
  response = @http_client.call_get(uri, default_headers)
  unless response.code == "200"
    parsed = JSON.parse(response.body)
    return parsed["error"]["code"]
  end
  parse_assistant_object(JSON.parse(response.body))
end