Class: LlmGateway::Proxy::Adapter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, provider_key: nil) ⇒ Adapter

Returns a new instance of Adapter.



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

def initialize(client, provider_key: nil)
  @client = client
  @provider_key = provider_key
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/llm_gateway/proxy/adapter.rb', line 6

def client
  @client
end

#provider_keyObject (readonly)

Returns the value of attribute provider_key.



6
7
8
# File 'lib/llm_gateway/proxy/adapter.rb', line 6

def provider_key
  @provider_key
end

Instance Method Details

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



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

def stream(message, tools: nil, system: nil, **options, &block)
  target_adapter = LlmGateway.build_provider(client.target_config.merge(provider: client.target_provider))
  mapper_class = target_adapter.stream_mapper_class
  raise LlmGateway::Errors::MissingMapperForProvider, "No stream_mapper configured" unless mapper_class

  mapper = mapper_class.new(
    provider: LlmGateway::Client.provider_id_from_client(target_adapter.client),
    api: target_adapter.stream_api_name
  )

  client.stream(normalize_messages(message), tools: tools, system: normalize_system(system), **options) do |chunk|
    mapper.map(chunk, &block)
  end

  mapper.result
end