Class: LlmGateway::Client
- Inherits:
-
Object
- Object
- LlmGateway::Client
- Defined in:
- lib/llm_gateway/client.rb
Class Method Summary collapse
- .chat(model, message, response_format: "text", tools: nil, system: nil, api_key: nil) ⇒ Object
- .client_class(model) ⇒ Object
- .input_mapper_for_client(client) ⇒ Object
- .normalize_messages(message) ⇒ Object
- .normalize_response_format(response_format) ⇒ Object
- .normalize_system(system) ⇒ Object
- .result_mapper(client) ⇒ Object
Class Method Details
.chat(model, message, response_format: "text", tools: nil, system: nil, api_key: nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/llm_gateway/client.rb', line 5 def self.chat(model, , response_format: "text", tools: nil, system: nil, api_key: nil) client_klass = client_class(model) = { model_key: model } [:api_key] = api_key if api_key client = client_klass.new(**) input_mapper = input_mapper_for_client(client) normalized_input = input_mapper.map({ messages: (), response_format: normalize_response_format(response_format), tools: tools, system: normalize_system(system) }) result = client.chat( normalized_input[:messages], response_format: normalized_input[:response_format], tools: normalized_input[:tools], system: normalized_input[:system] ) result_mapper(client).map(result) end |
.client_class(model) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/llm_gateway/client.rb', line 27 def self.client_class(model) return LlmGateway::Adapters::Claude::Client if model.start_with?("claude") return LlmGateway::Adapters::Groq::Client if model.start_with?("llama") return LlmGateway::Adapters::OpenAi::Client if model.start_with?("gpt") || model.start_with?("o4-") || model.start_with?("openai") raise LlmGateway::Errors::UnsupportedModel, model end |
.input_mapper_for_client(client) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/llm_gateway/client.rb', line 37 def self.input_mapper_for_client(client) return LlmGateway::Adapters::Claude::InputMapper if client.is_a?(LlmGateway::Adapters::Claude::Client) return LlmGateway::Adapters::OpenAi::InputMapper if client.is_a?(LlmGateway::Adapters::OpenAi::Client) LlmGateway::Adapters::Groq::InputMapper if client.is_a?(LlmGateway::Adapters::Groq::Client) end |
.normalize_messages(message) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/llm_gateway/client.rb', line 63 def self.() if .is_a?(String) [ { 'role': "user", 'content': } ] else end end |
.normalize_response_format(response_format) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/llm_gateway/client.rb', line 71 def self.normalize_response_format(response_format) if response_format.is_a?(String) { type: response_format } else response_format end end |
.normalize_system(system) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/llm_gateway/client.rb', line 51 def self.normalize_system(system) if system.nil? [] elsif system.is_a?(String) [ { role: "system", content: system } ] elsif system.is_a?(Array) system else raise ArgumentError, "System parameter must be a string or array, got #{system.class}" end end |
.result_mapper(client) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/llm_gateway/client.rb', line 44 def self.result_mapper(client) return LlmGateway::Adapters::Claude::OutputMapper if client.is_a?(LlmGateway::Adapters::Claude::Client) return LlmGateway::Adapters::OpenAi::OutputMapper if client.is_a?(LlmGateway::Adapters::OpenAi::Client) LlmGateway::Adapters::Groq::OutputMapper if client.is_a?(LlmGateway::Adapters::Groq::Client) end |