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) ⇒ 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) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/llm_gateway/client.rb', line 5 def self.chat(model, , response_format: "text", tools: nil, system: nil) client_klass = client_class(model) client = client_klass.new(model_key: model) 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
25 26 27 28 29 30 31 32 33 |
# File 'lib/llm_gateway/client.rb', line 25 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
35 36 37 38 39 40 |
# File 'lib/llm_gateway/client.rb', line 35 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
61 62 63 64 65 66 67 |
# File 'lib/llm_gateway/client.rb', line 61 def self.() if .is_a?(String) [ { 'role': "user", 'content': } ] else end end |
.normalize_response_format(response_format) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/llm_gateway/client.rb', line 69 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
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/llm_gateway/client.rb', line 49 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
42 43 44 45 46 47 |
# File 'lib/llm_gateway/client.rb', line 42 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 |