Module: RubyLLM::Text::Classify

Defined in:
lib/ruby_llm/text/classify.rb

Class Method Summary collapse

Class Method Details

.build_prompt(text, categories) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_llm/text/classify.rb', line 14

def self.build_prompt(text, categories)
  category_list = categories.map { |c| "- #{c}" }.join("\n")

  <<~PROMPT
    Classify the following text into one of these categories:
    #{category_list}

    Return only the category name, nothing else.

    Text:
    #{text}
  PROMPT
end

.call(text, categories:, model: nil, **options) ⇒ Object

Raises:

  • (ArgumentError)


4
5
6
7
8
9
10
# File 'lib/ruby_llm/text/classify.rb', line 4

def self.call(text, categories:, model: nil, **options)
  model ||= RubyLLM::Text.config.model_for(:classify)
  raise ArgumentError, "categories are required" if categories.empty?

  prompt = build_prompt(text, categories)
  Base.call_llm(prompt, model: model, **options)
end