Class: LlmGateway::Adapters::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_gateway/adapters/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Adapter

Returns a new instance of Adapter.



10
11
12
# File 'lib/llm_gateway/adapters/adapter.rb', line 10

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/llm_gateway/adapters/adapter.rb', line 8

def client
  @client
end

Instance Method Details

#download_file(file_id:) ⇒ Object



57
58
59
60
61
62
# File 'lib/llm_gateway/adapters/adapter.rb', line 57

def download_file(file_id:)
  raise LlmGateway::Errors::MissingMapperForProvider, "No file_output_mapper configured" unless file_output_mapper

  result = client.download_file(file_id)
  file_output_mapper.map(result)
end

#stream(message, tools: nil, system: nil, **options, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/llm_gateway/adapters/adapter.rb', line 14

def stream(message, tools: nil, system: nil, **options, &block)
  raise LlmGateway::Errors::MissingMapperForProvider, "No stream_mapper configured" unless stream_mapper

  normalized_input = map_input({
    messages: sanitize_messages(normalize_messages(message)),
    tools: tools,
    system: normalize_system(system)
  })

  mapper = stream_mapper.new

  perform_stream(
    normalized_input[:messages],
    tools: normalized_input[:tools],
    system: normalized_input[:system],
    **map_options(options)
  ) do |chunk|
    mapper.map(chunk, &block)
  end

  AssistantMessage.new(
    mapper.result.merge(
      provider: LlmGateway::Client.provider_id_from_client(client),
      api: api_name
    )
  )
end

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/llm_gateway/adapters/adapter.rb', line 42

def upload_file(filename:, content:, mime_type: "application/octet-stream", purpose: "assistants")
  raise LlmGateway::Errors::MissingMapperForProvider, "No file_output_mapper configured" unless file_output_mapper

  upload_params = client.method(:upload_file).parameters
  supports_purpose = upload_params.any? { |type, name| [ :key, :keyreq ].include?(type) && name == :purpose }

  result = if supports_purpose
    client.upload_file(filename, content, mime_type, purpose: purpose)
  else
    client.upload_file(filename, content, mime_type)
  end

  file_output_mapper.map(result)
end