Bird
0
0

What is wrong with this custom metric class that causes an error?

medium📝 Debug Q14 of 15
LangChain - Evaluation and Testing
What is wrong with this custom metric class that causes an error?
class LengthDiff(Evaluation):
    def evaluate(self, predictions, references):
        return abs(len(predictions) - len(references)) / len(references)
AIt returns a number instead of a score between 0 and 1
BIt does not implement the evaluate method
CIt uses abs() incorrectly causing a syntax error
DIt does not handle empty lists causing runtime error
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the evaluate method with empty references

    If references=[], len(references)=0 causes ZeroDivisionError in the division.
  2. Step 2: Identify the runtime error cause

    The code divides by len(references) without checking if references is empty, causing runtime error.
  3. Final Answer:

    It does not handle empty lists causing runtime error -> Option D
  4. Quick Check:

    len(references)==0 -> ZeroDivisionError [OK]
Quick Trick: Check how method handles empty input lists [OK]
Common Mistakes:
MISTAKES
  • Assuming abs() causes syntax error
  • Thinking evaluate method is missing
  • Ignoring empty list edge cases

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes