Class: RuboCop::Cop::Legion::HelperMigration::DirectLlm

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/legion/helper_migration/direct_llm.rb

Overview

Detects direct calls to ‘Legion::LLM` methods and suggests using the `llm_*` helpers from `Legion::Extensions::Helpers::LLM`.

Examples:

# bad
Legion::LLM.embed(text)
Legion::LLM.chat(message, intent: :moderate)
Legion::LLM.ask(message: 'hello')
Legion::LLM.structured(messages: [], schema: {})
Legion::LLM.embed_batch(texts)

# good
llm_embed(text)
llm_chat(message, intent: :moderate)
llm_ask(message: 'hello')
llm_structured(messages: [], schema: {})
llm_embed_batch(texts)

Constant Summary collapse

MSG =
'Use `%<helper>s` instead of `Legion::LLM.%<method>s`. ' \
'Include the LLM helper mixin.'
RESTRICT_ON_SEND =
%i[embed chat ask structured embed_batch].freeze
HELPER_MAP =
{
  embed: 'llm_embed',
  chat: 'llm_chat',
  ask: 'llm_ask',
  structured: 'llm_structured',
  embed_batch: 'llm_embed_batch'
}.freeze

Instance Method Summary collapse

Instance Method Details

#legion_llm_call?(node) ⇒ Object



41
42
43
# File 'lib/rubocop/cop/legion/helper_migration/direct_llm.rb', line 41

def_node_matcher :legion_llm_call?, <<~PATTERN
  (send (const (const nil? :Legion) :LLM) {:embed :chat :ask :structured :embed_batch} ...)
PATTERN

#on_send(node) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/legion/helper_migration/direct_llm.rb', line 45

def on_send(node)
  return unless legion_llm_call?(node)

  method_name = node.method_name
  helper = HELPER_MAP[method_name]
  message = format(MSG, helper: helper, method: method_name)

  add_offense(node, message: message) do |corrector|
    args_source = node.arguments.map(&:source).join(', ')
    corrector.replace(node, "#{helper}(#{args_source})")
  end
end