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.



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

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

Instance Method Details

#chat(message, tools: nil, system: nil, **options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/llm_gateway/adapters/adapter.rb', line 15

def chat(message, tools: nil, system: nil, **options)
  normalized_input = map_input({
    messages: sanitize_messages(normalize_messages(message)),
    tools: tools,
    system: normalize_system(system)
  })

  result = perform_chat(
    normalized_input[:messages],
    tools: normalized_input[:tools],
    system: normalized_input[:system],
    **map_options(options)
  )

  map_output(result)
end

#download_file(file_id:) ⇒ Object



78
79
80
81
82
83
# File 'lib/llm_gateway/adapters/adapter.rb', line 78

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/llm_gateway/adapters/adapter.rb', line 32

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)
  })

  accumulator = ::StreamAccumulator.new
  mapper = stream_mapper.new

  perform_stream(
    normalized_input[:messages],
    tools: normalized_input[:tools],
    system: normalized_input[:system],
    **map_options(options)
  ) do |chunk|
    event = mapper.map(chunk)
    accumulator.push(event)
    block.call(event) if block && event
  end

  AssistantMessage.new(
    accumulator.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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/llm_gateway/adapters/adapter.rb', line 63

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