Class: LlmGateway::Clients::OpenAI
- Inherits:
-
BaseClient
show all
- Defined in:
- lib/llm_gateway/clients/openai.rb,
lib/llm_gateway/clients/openai_codex/oauth_flow.rb,
lib/llm_gateway/clients/openai_codex/token_manager.rb
Defined Under Namespace
Classes: OAuthFlow, TokenManager
Constant Summary
collapse
- CODEX_BASE_ENDPOINT =
"https://chatgpt.com/backend-api/codex"
- DEFAULT_MODEL =
"gpt-4o"
- DEFAULT_EMBEDDINGS_MODEL =
"text-embedding-3-small"
Instance Attribute Summary collapse
Attributes inherited from BaseClient
#api_key, #base_endpoint
Instance Method Summary
collapse
-
#chat(messages, tools: nil, system: [], model: DEFAULT_MODEL, **options) ⇒ Object
-
#chat_codex(messages, tools: nil, system: [], account_id: nil, model: DEFAULT_MODEL, **options) ⇒ Object
-
#download_file(file_id) ⇒ Object
-
#generate_embeddings(input, model: DEFAULT_EMBEDDINGS_MODEL) ⇒ Object
-
#get_oauth_access_token(access_token:, refresh_token:, expires_at:, account_id: nil, &block) ⇒ Object
-
#initialize(api_key: ENV["OPENAI_API_KEY"], account_id: nil) ⇒ OpenAI
constructor
A new instance of OpenAI.
-
#responses(messages, tools: nil, system: [], model: DEFAULT_MODEL, **options) ⇒ Object
-
#stream(messages, tools: nil, system: [], model: DEFAULT_MODEL, **options, &block) ⇒ Object
-
#stream_codex(messages, tools: nil, system: [], account_id: nil, model: DEFAULT_MODEL, **options, &block) ⇒ Object
-
#stream_responses(messages, tools: nil, system: [], model: DEFAULT_MODEL, **options, &block) ⇒ Object
-
#upload_file(filename, content, mime_type = "application/octet-stream", purpose: "user_data") ⇒ Object
Methods inherited from BaseClient
#get, #post, #post_file, #post_stream
Constructor Details
#initialize(api_key: ENV["OPENAI_API_KEY"], account_id: nil) ⇒ OpenAI
Returns a new instance of OpenAI.
14
15
16
17
18
|
# File 'lib/llm_gateway/clients/openai.rb', line 14
def initialize(api_key: ENV["OPENAI_API_KEY"], account_id: nil)
@base_endpoint = "https://api.openai.com/v1"
@account_id = account_id
super(api_key: api_key)
end
|
Instance Attribute Details
#account_id ⇒ Object
Returns the value of attribute account_id.
12
13
14
|
# File 'lib/llm_gateway/clients/openai.rb', line 12
def account_id
@account_id
end
|
Instance Method Details
#chat(messages, tools: nil, system: [], model: DEFAULT_MODEL, **options) ⇒ Object
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/llm_gateway/clients/openai.rb', line 20
def chat(messages, tools: nil, system: [], model: DEFAULT_MODEL, **options)
body = {
model: model,
messages: system + messages
}
body[:tools] = tools if tools
body.merge!(options)
post("chat/completions", body)
end
|
#chat_codex(messages, tools: nil, system: [], account_id: nil, model: DEFAULT_MODEL, **options) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/llm_gateway/clients/openai.rb', line 79
def chat_codex(messages, tools: nil, system: [], account_id: nil, model: DEFAULT_MODEL, **options)
body = build_codex_body(messages, system, tools, model: model, **options)
completed_response = nil
post_codex_stream("responses", body, account_id: account_id) do |raw_sse|
if raw_sse[:event] == "response.completed"
completed_response = raw_sse.dig(:data, :response)
end
end
completed_response
end
|
#download_file(file_id) ⇒ Object
97
98
99
|
# File 'lib/llm_gateway/clients/openai.rb', line 97
def download_file(file_id)
get("files/#{file_id}/content")
end
|
#generate_embeddings(input, model: DEFAULT_EMBEDDINGS_MODEL) ⇒ Object
101
102
103
104
105
106
107
|
# File 'lib/llm_gateway/clients/openai.rb', line 101
def generate_embeddings(input, model: DEFAULT_EMBEDDINGS_MODEL)
body = {
input:,
model: model
}
post("embeddings", body)
end
|
#get_oauth_access_token(access_token:, refresh_token:, expires_at:, account_id: nil, &block) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/llm_gateway/clients/openai.rb', line 67
def get_oauth_access_token(access_token:, refresh_token:, expires_at:, account_id: nil, &block)
token_manager = LlmGateway::Clients::OpenAI::TokenManager.new(
access_token: access_token,
refresh_token: refresh_token,
expires_at: expires_at,
account_id: account_id
)
token_manager.on_token_refresh = block if block_given?
token_manager.ensure_valid_token
token_manager.access_token
end
|
#responses(messages, tools: nil, system: [], model: DEFAULT_MODEL, **options) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
|
# File 'lib/llm_gateway/clients/openai.rb', line 43
def responses(messages, tools: nil, system: [], model: DEFAULT_MODEL, **options)
body = {
model: model,
input: messages.flatten
}
body[:instructions] = system[0][:content] if system.any?
body[:tools] = tools if tools
body.merge!(options)
post("responses", body)
end
|
#stream(messages, tools: nil, system: [], model: DEFAULT_MODEL, **options, &block) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/llm_gateway/clients/openai.rb', line 31
def stream(messages, tools: nil, system: [], model: DEFAULT_MODEL, **options, &block)
body = {
model: model,
messages: system + messages
}
body[:tools] = tools if tools
body.merge!(options)
body[:stream_options] = (body[:stream_options] || {}).merge(include_usage: true)
post_stream("chat/completions", body, &block)
end
|
#stream_codex(messages, tools: nil, system: [], account_id: nil, model: DEFAULT_MODEL, **options, &block) ⇒ Object
92
93
94
95
|
# File 'lib/llm_gateway/clients/openai.rb', line 92
def stream_codex(messages, tools: nil, system: [], account_id: nil, model: DEFAULT_MODEL, **options, &block)
body = build_codex_body(messages, system, tools, model: model, **options)
post_codex_stream("responses", body, account_id: account_id, &block)
end
|
#stream_responses(messages, tools: nil, system: [], model: DEFAULT_MODEL, **options, &block) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/llm_gateway/clients/openai.rb', line 55
def stream_responses(messages, tools: nil, system: [], model: DEFAULT_MODEL, **options, &block)
body = {
model: model,
input: messages.flatten
}
body[:instructions] = system[0][:content] if system.any?
body[:tools] = tools if tools
body.merge!(options)
post_stream("responses", body, &block)
end
|
#upload_file(filename, content, mime_type = "application/octet-stream", purpose: "user_data") ⇒ Object
109
110
111
|
# File 'lib/llm_gateway/clients/openai.rb', line 109
def upload_file(filename, content, mime_type = "application/octet-stream", purpose: "user_data")
post_file("files", content, filename, purpose: purpose, mime_type: mime_type)
end
|