Module: Legion::LLM::Helper
- Includes:
- Legion::Logging::Helper
- Included in:
- Extensions::Helpers::LLM
- Defined in:
- lib/legion/llm/helper.rb
Instance Method Summary collapse
- #llm_ask(message:) ⇒ Object
- #llm_budget_remaining ⇒ Object
- #llm_can_embed? ⇒ Boolean
-
#llm_chat(message, model: nil, provider: nil, intent: nil, tier: nil, tools: [], instructions: nil, compress: 0, escalate: nil, max_escalations: nil, quality_check: nil, caller: nil, use_default_intent: false) ⇒ Object
— Core Operations —.
-
#llm_connected? ⇒ Boolean
— Status —.
-
#llm_cost_estimate(model: nil, input_tokens: 0, output_tokens: 0) ⇒ Object
— Cost / Budget —.
- #llm_cost_summary(since: nil) ⇒ Object
- #llm_default_intent ⇒ Object
-
#llm_default_model ⇒ Object
— Layered Defaults — Override in your LEX to set extension-specific defaults.
- #llm_default_provider ⇒ Object
- #llm_embed(text) ⇒ Object
- #llm_embed_batch(texts) ⇒ Object
- #llm_routing_enabled? ⇒ Boolean
- #llm_session(model: nil, provider: nil, intent: nil, tier: nil, caller: nil, use_default_intent: false) ⇒ Object
- #llm_structured(messages:, schema:) ⇒ Object
Instance Method Details
#llm_ask(message:) ⇒ Object
86 87 88 |
# File 'lib/legion/llm/helper.rb', line 86 def llm_ask(message:, **) Legion::LLM.ask(message: , **) end |
#llm_budget_remaining ⇒ Object
131 132 133 134 135 136 |
# File 'lib/legion/llm/helper.rb', line 131 def llm_budget_remaining Legion::LLM::Hooks::BudgetGuard.remaining rescue StandardError => e handle_exception(e, level: :debug, operation: 'llm.helper.budget_remaining') Float::INFINITY end |
#llm_can_embed? ⇒ Boolean
99 100 101 102 103 104 |
# File 'lib/legion/llm/helper.rb', line 99 def llm_connected? && Legion::LLM. rescue StandardError => e handle_exception(e, level: :debug, operation: 'llm.helper.can_embed') false end |
#llm_chat(message, model: nil, provider: nil, intent: nil, tier: nil, tools: [], instructions: nil, compress: 0, escalate: nil, max_escalations: nil, quality_check: nil, caller: nil, use_default_intent: false) ⇒ Object
— Core Operations —
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 62 |
# File 'lib/legion/llm/helper.rb', line 37 def llm_chat(, model: nil, provider: nil, intent: nil, tier: nil, tools: [], # rubocop:disable Metrics/ParameterLists instructions: nil, compress: 0, escalate: nil, max_escalations: nil, quality_check: nil, caller: nil, use_default_intent: false) effective_model = model || llm_default_model effective_provider = provider || llm_default_provider effective_intent = intent || (use_default_intent ? llm_default_intent : nil) if compress.positive? = Legion::LLM::Context::Compressor.compress(, level: compress) instructions = Legion::LLM::Context::Compressor.compress(instructions, level: compress) if instructions end if escalate return Legion::LLM.chat(model: effective_model, provider: effective_provider, intent: effective_intent, tier: tier, escalate: true, max_escalations: max_escalations, quality_check: quality_check, message: , caller: caller) end chat = Legion::LLM.chat(model: effective_model, provider: effective_provider, intent: effective_intent, tier: tier, escalate: false, caller: caller) chat.with_instructions(instructions) if instructions chat.with_tools(*tools) unless tools.empty? chat.ask() end |
#llm_connected? ⇒ Boolean
— Status —
92 93 94 95 96 97 |
# File 'lib/legion/llm/helper.rb', line 92 def llm_connected? defined?(Legion::LLM) && Legion::LLM.started? rescue StandardError => e handle_exception(e, level: :debug, operation: 'llm.helper.connected') false end |
#llm_cost_estimate(model: nil, input_tokens: 0, output_tokens: 0) ⇒ Object
— Cost / Budget —
115 116 117 118 119 120 121 122 |
# File 'lib/legion/llm/helper.rb', line 115 def llm_cost_estimate(model: nil, input_tokens: 0, output_tokens: 0) model ||= llm_default_model Legion::LLM::Metering::Pricing.estimate(model_id: model, input_tokens: input_tokens, output_tokens: output_tokens) rescue StandardError => e handle_exception(e, level: :debug, operation: 'llm.helper.cost_estimate', model: model) 0.0 end |
#llm_cost_summary(since: nil) ⇒ Object
124 125 126 127 128 129 |
# File 'lib/legion/llm/helper.rb', line 124 def llm_cost_summary(since: nil) Legion::LLM::Metering::Recorder.summary(since: since) rescue StandardError => e handle_exception(e, level: :debug, operation: 'llm.helper.cost_summary') { total_cost_usd: 0.0, total_requests: 0, total_input_tokens: 0, total_output_tokens: 0, by_model: {} } end |
#llm_default_intent ⇒ Object
28 29 30 31 32 33 |
# File 'lib/legion/llm/helper.rb', line 28 def llm_default_intent Legion::LLM::Settings.value(:routing, :default_intent) rescue StandardError => e handle_exception(e, level: :debug, operation: 'llm.helper.default_intent') nil end |
#llm_default_model ⇒ Object
— Layered Defaults — Override in your LEX to set extension-specific defaults. Resolution chain: per-call kwarg -> LEX override -> Settings -> nil (auto-detect)
14 15 16 17 18 19 |
# File 'lib/legion/llm/helper.rb', line 14 def llm_default_model Legion::LLM::Settings.value(:default_model) rescue StandardError => e handle_exception(e, level: :debug, operation: 'llm.helper.default_model') nil end |
#llm_default_provider ⇒ Object
21 22 23 24 25 26 |
# File 'lib/legion/llm/helper.rb', line 21 def llm_default_provider Legion::LLM::Settings.value(:default_provider) rescue StandardError => e handle_exception(e, level: :debug, operation: 'llm.helper.default_provider') nil end |
#llm_embed(text) ⇒ Object
64 65 66 |
# File 'lib/legion/llm/helper.rb', line 64 def (text, **) Legion::LLM.(text, **) end |
#llm_embed_batch(texts) ⇒ Object
68 69 70 |
# File 'lib/legion/llm/helper.rb', line 68 def (texts, **) Legion::LLM.(texts, **) end |
#llm_routing_enabled? ⇒ Boolean
106 107 108 109 110 111 |
# File 'lib/legion/llm/helper.rb', line 106 def llm_routing_enabled? llm_connected? && Legion::LLM::Router.routing_enabled? rescue StandardError => e handle_exception(e, level: :debug, operation: 'llm.helper.routing_enabled') false end |
#llm_session(model: nil, provider: nil, intent: nil, tier: nil, caller: nil, use_default_intent: false) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/legion/llm/helper.rb', line 72 def llm_session(model: nil, provider: nil, intent: nil, tier: nil, caller: nil, use_default_intent: false) effective_model = model || llm_default_model effective_provider = provider || llm_default_provider effective_intent = intent || (use_default_intent ? llm_default_intent : nil) Legion::LLM.chat(model: effective_model, provider: effective_provider, intent: effective_intent, tier: tier, escalate: false, caller: caller) end |
#llm_structured(messages:, schema:) ⇒ Object
82 83 84 |
# File 'lib/legion/llm/helper.rb', line 82 def llm_structured(messages:, schema:, **) Legion::LLM.structured(messages: , schema: schema, **) end |