Class: OpenAI::Client

Inherits:
Object
  • Object
show all
Includes:
HTTP
Defined in:
lib/openai/client.rb

Constant Summary collapse

SENSITIVE_ATTRIBUTES =
%i[@access_token @admin_token @organization_id @extra_headers].freeze
CONFIG_KEYS =
%i[access_token admin_token api_type api_version extra_headers
log_errors organization_id request_timeout uri_base].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HTTP

#delete, #get, #json_post, #multipart_post, #post

Methods included from HTTPHeaders

#add_headers

Constructor Details

#initialize(config = {}, &faraday_middleware) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/openai/client.rb', line 12

def initialize(config = {}, &faraday_middleware)
  CONFIG_KEYS.each do |key|
    # Set instance variables like api_type & access_token. Fall back to global config
    # if not present.
    instance_variable_set(
      "@#{key}",
      config[key].nil? ? OpenAI.configuration.send(key) : config[key]
    )
  end
  @faraday_middleware = faraday_middleware
end

Instance Attribute Details

#access_token=(value) ⇒ Object (writeonly)

Sets the attribute access_token

Parameters:

  • value

    the value to set the attribute access_token to.



10
11
12
# File 'lib/openai/client.rb', line 10

def access_token=(value)
  @access_token = value
end

Instance Method Details

#adminObject



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

def admin
  unless admin_token
    e = "You must set an OPENAI_ADMIN_TOKEN= to use administrative endpoints:\n\n  https://platform.openai.com/settings/organization/admin-keys"
    raise AuthenticationError, e
  end

  dup.tap do |client|
    client.access_token = client.admin_token
  end
end

#assistantsObject



60
61
62
# File 'lib/openai/client.rb', line 60

def assistants
  @assistants ||= OpenAI::Assistants.new(client: self)
end

#audioObject



36
37
38
# File 'lib/openai/client.rb', line 36

def audio
  @audio ||= OpenAI::Audio.new(client: self)
end

#azure?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/openai/client.rb', line 112

def azure?
  @api_type&.to_sym == :azure
end

#batchesObject



92
93
94
# File 'lib/openai/client.rb', line 92

def batches
  @batches ||= OpenAI::Batches.new(client: self)
end

#beta(apis) ⇒ Object



127
128
129
130
131
# File 'lib/openai/client.rb', line 127

def beta(apis)
  dup.tap do |client|
    client.add_headers("OpenAI-Beta": apis.map { |k, v| "#{k}=#{v}" }.join(";"))
  end
end

#chat(parameters: {}) ⇒ Object



24
25
26
# File 'lib/openai/client.rb', line 24

def chat(parameters: {})
  json_post(path: "/chat/completions", parameters: parameters)
end

#completions(parameters: {}) ⇒ Object



32
33
34
# File 'lib/openai/client.rb', line 32

def completions(parameters: {})
  json_post(path: "/completions", parameters: parameters)
end

#conversationsObject



108
109
110
# File 'lib/openai/client.rb', line 108

def conversations
  @conversations ||= OpenAI::Conversations.new(client: self)
end

#embeddings(parameters: {}) ⇒ Object



28
29
30
# File 'lib/openai/client.rb', line 28

def embeddings(parameters: {})
  json_post(path: "/embeddings", parameters: parameters)
end

#filesObject



40
41
42
# File 'lib/openai/client.rb', line 40

def files
  @files ||= OpenAI::Files.new(client: self)
end

#finetunesObject



44
45
46
# File 'lib/openai/client.rb', line 44

def finetunes
  @finetunes ||= OpenAI::Finetunes.new(client: self)
end

#imagesObject



48
49
50
# File 'lib/openai/client.rb', line 48

def images
  @images ||= OpenAI::Images.new(client: self)
end

#inspectObject



133
134
135
136
137
138
139
140
141
# File 'lib/openai/client.rb', line 133

def inspect
  vars = instance_variables.map do |var|
    value = instance_variable_get(var)

    SENSITIVE_ATTRIBUTES.include?(var) ? "#{var}=[REDACTED]" : "#{var}=#{value.inspect}"
  end

  "#<#{self.class}:#{object_id} #{vars.join(', ')}>"
end

#messagesObject



68
69
70
# File 'lib/openai/client.rb', line 68

def messages
  @messages ||= OpenAI::Messages.new(client: self)
end

#modelsObject



52
53
54
# File 'lib/openai/client.rb', line 52

def models
  @models ||= OpenAI::Models.new(client: self)
end

#moderations(parameters: {}) ⇒ Object



100
101
102
# File 'lib/openai/client.rb', line 100

def moderations(parameters: {})
  json_post(path: "/moderations", parameters: parameters)
end

#realtimeObject



96
97
98
# File 'lib/openai/client.rb', line 96

def realtime
  @realtime ||= OpenAI::Realtime.new(client: self)
end

#responsesObject



56
57
58
# File 'lib/openai/client.rb', line 56

def responses
  @responses ||= OpenAI::Responses.new(client: self)
end

#run_stepsObject



76
77
78
# File 'lib/openai/client.rb', line 76

def run_steps
  @run_steps ||= OpenAI::RunSteps.new(client: self)
end

#runsObject



72
73
74
# File 'lib/openai/client.rb', line 72

def runs
  @runs ||= OpenAI::Runs.new(client: self)
end

#threadsObject



64
65
66
# File 'lib/openai/client.rb', line 64

def threads
  @threads ||= OpenAI::Threads.new(client: self)
end

#usageObject



104
105
106
# File 'lib/openai/client.rb', line 104

def usage
  @usage ||= OpenAI::Usage.new(client: self)
end

#vector_store_file_batchesObject



88
89
90
# File 'lib/openai/client.rb', line 88

def vector_store_file_batches
  @vector_store_file_batches ||= OpenAI::VectorStoreFileBatches.new(client: self)
end

#vector_store_filesObject



84
85
86
# File 'lib/openai/client.rb', line 84

def vector_store_files
  @vector_store_files ||= OpenAI::VectorStoreFiles.new(client: self)
end

#vector_storesObject



80
81
82
# File 'lib/openai/client.rb', line 80

def vector_stores
  @vector_stores ||= OpenAI::VectorStores.new(client: self)
end