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.5.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\



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

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



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

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

Instance Method Details

#call_get(url) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/openai/assistant.rb', line 115

def call_get(url)
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Get.new(uri.path, default_headers)
  response = http.request(request)
  parsed = JSON.parse(response.body)
  return parsed["error"]["code"] unless response.code == "200"

  parsed
end

#call_post(url, req_body) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/openai/assistant.rb', line 103

def call_post(url, req_body)
  uri = URI(url)
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.path, default_headers)
  request.body = req_body
  response = http.request(request)
  parsed = JSON.parse(response.body)
  return parsed["error"]["code"] unless response.code == "200"

  parsed
end

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

Returns A new response object of assistant.

Parameters:

Returns:



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

def create_assistant(model, instructions)
  puts default_headers
  url = URI.parse(@openai_url)
  req_body = {
    "instructions": instructions,
    "name": "assistant",
    "tools": [{ "type": "code_interpreter" }],
    "model": model
  }.to_json
  response = call_post(url, req_body)
  return response["error"]["code"] unless response["error"].nil?

  parse_assistant_object(JSON.parse(response.body))
end

#default_headersObject



95
96
97
98
99
100
101
# File 'lib/openai/assistant.rb', line 95

def default_headers
  {
    "Authorization": "Bearer #{@openai_api_key}",
    "OpenAI-Beta": "assistants=v1",
    "Content-Type": "application/json"
  }
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



54
55
56
57
58
59
60
61
62
63
# File 'lib/openai/assistant.rb', line 54

def delete_assistant(assistant_id)
  url = "#{@openai_url}/#{assistant_id}"
  uri = URI(url)
  response = call_post(uri, nil)
  return response["error"]["code"] unless response["error"].nil?

  parsed = JSON.parse(response.body)

  parsed["deleted"]
end

#list_assistantArray<Openai::AssistantObj>

Returns List all assistant.

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/openai/assistant.rb', line 66

def list_assistant
  url = @openai_url
  uri = URI(url)
  response = call_get(uri)
  return response["error"]["code"] unless response["error"].nil?

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

#parse_assistant_object(data) ⇒ Object

Returns private.

Returns:

  • private



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/openai/assistant.rb', line 80

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.

Parameters:

  • assistant_id (String)

    The id of assistant after create

Returns:



43
44
45
46
47
48
49
50
# File 'lib/openai/assistant.rb', line 43

def retrieve_assistant(assistant_id)
  url = "#{@openai_url}/#{assistant_id}"
  uri = URI(url)
  response = call_post(uri, nil)
  return response["error"]["code"] unless response["error"].nil?

  parse_assistant_object(JSON.parse(response.body))
end