Class: LlmGateway::Adapters::Adapter
- Defined in:
- lib/llm_gateway/adapters/adapter.rb
Direct Known Subclasses
LlmGateway::Adapters::Anthropic::MessagesAdapter, Groq::ChatCompletionsAdapter, OpenAI::ChatCompletionsAdapter, OpenAI::ResponsesAdapter, OpenAICodex::ResponsesAdapter
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#provider_key ⇒ Object
readonly
Returns the value of attribute provider_key.
Instance Method Summary collapse
- #download_file(file_id:) ⇒ Object
-
#initialize(client, provider_key: nil) ⇒ Adapter
constructor
A new instance of Adapter.
- #raw_stream(message, tools: nil, system: nil, **options, &block) ⇒ Object
- #stream(message, tools: nil, system: nil, **options, &block) ⇒ Object
- #stream_api_name ⇒ Object
- #stream_mapper_class ⇒ Object
- #upload_file(filename:, content:, mime_type: "application/octet-stream", purpose: "assistants") ⇒ Object
Constructor Details
#initialize(client, provider_key: nil) ⇒ Adapter
Returns a new instance of Adapter.
10 11 12 13 |
# File 'lib/llm_gateway/adapters/adapter.rb', line 10 def initialize(client, provider_key: nil) @client = client @provider_key = provider_key end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
8 9 10 |
# File 'lib/llm_gateway/adapters/adapter.rb', line 8 def client @client end |
#provider_key ⇒ Object (readonly)
Returns the value of attribute provider_key.
8 9 10 |
# File 'lib/llm_gateway/adapters/adapter.rb', line 8 def provider_key @provider_key end |
Instance Method Details
#download_file(file_id:) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/llm_gateway/adapters/adapter.rb', line 61 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 |
#raw_stream(message, tools: nil, system: nil, **options, &block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/llm_gateway/adapters/adapter.rb', line 15 def raw_stream(, tools: nil, system: nil, **, &block) normalized_input = map_input({ messages: ((), target_model: [:model]), tools: tools, system: normalize_system(system) }) perform_stream( normalized_input[:messages], tools: normalized_input[:tools], system: normalized_input[:system], **(), &block ) end |
#stream(message, tools: nil, system: nil, **options, &block) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/llm_gateway/adapters/adapter.rb', line 31 def stream(, tools: nil, system: nil, **, &block) raise LlmGateway::Errors::MissingMapperForProvider, "No stream_mapper configured" unless stream_mapper mapper = stream_mapper.new( provider: LlmGateway::Client.provider_id_from_client(client), api: api_name ) raw_stream(, tools: tools, system: system, **) do |chunk| mapper.map(chunk, &block) end mapper.result end |
#stream_api_name ⇒ Object
100 101 102 |
# File 'lib/llm_gateway/adapters/adapter.rb', line 100 def stream_api_name api_name end |
#stream_mapper_class ⇒ Object
104 105 106 |
# File 'lib/llm_gateway/adapters/adapter.rb', line 104 def stream_mapper_class stream_mapper end |
#upload_file(filename:, content:, mime_type: "application/octet-stream", purpose: "assistants") ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/llm_gateway/adapters/adapter.rb', line 46 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 |