Class: LlmGateway::Adapters::OpenAi::Client

Inherits:
BaseClient
  • Object
show all
Defined in:
lib/llm_gateway/adapters/open_ai/client.rb

Instance Attribute Summary

Attributes inherited from BaseClient

#api_key, #base_endpoint, #model_key

Instance Method Summary collapse

Methods inherited from BaseClient

#get, #post, #post_file

Constructor Details

#initialize(model_key: "gpt-4o", api_key: ENV["OPENAI_API_KEY"]) ⇒ Client

Returns a new instance of Client.



9
10
11
12
# File 'lib/llm_gateway/adapters/open_ai/client.rb', line 9

def initialize(model_key: "gpt-4o", api_key: ENV["OPENAI_API_KEY"])
  @base_endpoint = "https://api.openai.com/v1"
  super(model_key: model_key, api_key: api_key)
end

Instance Method Details

#chat(messages, response_format: { type: "text" }, tools: nil, system: [], max_completion_tokens: 4096) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/llm_gateway/adapters/open_ai/client.rb', line 14

def chat(messages, response_format: { type: "text" }, tools: nil, system: [], max_completion_tokens: 4096)
  body = {
    model: model_key,
    messages: system + messages,
    max_completion_tokens: max_completion_tokens
  }
  body[:tools] = tools if tools

  post("chat/completions", body)
end

#download_file(file_id) ⇒ Object



38
39
40
# File 'lib/llm_gateway/adapters/open_ai/client.rb', line 38

def download_file(file_id)
  get("files/#{file_id}/content")
end

#generate_embeddings(input) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/llm_gateway/adapters/open_ai/client.rb', line 42

def generate_embeddings(input)
  body = {
    input:,
    model: model_key
  }
  post("embeddings", body)
end

#responses(messages, response_format: { type: "text" }, tools: nil, system: [], max_completion_tokens: 4096) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/llm_gateway/adapters/open_ai/client.rb', line 25

def responses(messages, response_format: { type: "text" }, tools: nil, system: [], max_completion_tokens: 4096)
    body = {
      model: model_key,
      max_output_tokens: max_completion_tokens,
      input: messages.flatten
    }
    body[:instructions] = system[0][:content] if system.any?
    body[:tools] = tools if tools
    result = post("responses", body)
    result
end

#upload_file(filename, content, mime_type = "application/octet-stream", purpose: "user_data") ⇒ Object



50
51
52
# File 'lib/llm_gateway/adapters/open_ai/client.rb', line 50

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