Class: RuboCop::Cop::Prompt::TemperatureRange

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/prompt/temperature_range.rb

Overview

Checks that temperature values are appropriate for the task type.

This cop identifies code in classes, modules, or methods with “prompt” in their names and ensures that when temperature > 0.7, it’s not being used for precision tasks. High temperature values (> 0.7) should be avoided for tasks requiring accuracy, consistency, or factual correctness.

Examples:

# bad (high temperature for precision task)
OpenAI::Client.new.chat(
  parameters: {
    temperature: 0.9,
    messages: [{ role: "system", content: "Analyze this data accurately" }]
  }
)

# bad (high temperature with precision keywords)
client.chat(
  temperature: 0.8,
  messages: [{ role: "user", content: "Calculate the exact result" }]
)

# good (low temperature for precision)
OpenAI::Client.new.chat(
  parameters: {
    temperature: 0.3,
    messages: [{ role: "system", content: "Analyze this data accurately" }]
  }
)

# good (high temperature for creative task)
OpenAI::Client.new.chat(
  parameters: {
    temperature: 0.9,
    messages: [{ role: "user", content: "Write a creative story" }]
  }
)

Constant Summary collapse

MSG =
"High temperature (%.1f > 0.7) should not be used for precision tasks. " \
"Consider using temperature <= 0.7 for tasks requiring accuracy."
TEMPERATURE_THRESHOLD =

Temperature threshold above which we check for precision tasks

0.7
PRECISION_KEYWORDS =

Keywords that indicate precision/accuracy tasks

[
  # Analysis and accuracy
  "accurate", "accuracy", "precise", "precision", "exact", "exactly",
  "analyze", "analysis", "calculate", "computation", "compute",
  "measure", "measurement", "count", "sum", "total",
  # Factual and data tasks
  "fact", "factual", "data", "information", "correct", "verify",
  "validate", "check", "review", "audit", "inspect",
  # Classification and categorization
  "classify", "classification", "categorize", "category",
  "sort", "organize", "structure", "parse", "extract",
  # Technical and code tasks
  "code", "programming", "syntax", "debug", "error", "fix",
  "technical", "documentation", "specification", "format"
].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rubocop/cop/prompt/temperature_range.rb', line 69

def on_send(node)
  return unless in_prompt_context?(node)
  return unless chat_method?(node)

  temperature_value = extract_temperature(node)
  return unless temperature_value && temperature_value > TEMPERATURE_THRESHOLD

  messages = extract_messages(node)
  return unless messages && precision_task?(messages)

  add_offense(
    node,
    message: format(MSG, temperature_value)
  )
end