0
0
LangChainframework~20 mins

Automated evaluation pipelines in LangChain - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LangChain Evaluation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this LangChain evaluation pipeline snippet?

Consider this LangChain evaluation pipeline code that runs a simple evaluation on a list of inputs.

from langchain.evaluation import EvaluationChain
from langchain.schema import Document

def simple_eval_fn(doc: Document) -> bool:
    return "good" in doc.page_content

eval_chain = EvaluationChain.from_llm(llm=None, evaluation_fn=simple_eval_fn)
inputs = [Document(page_content="This is a good example."), Document(page_content="This is bad.")]
results = [eval_chain.evaluate(input_doc) for input_doc in inputs]
print(results)

What will be printed?

LangChain
from langchain.evaluation import EvaluationChain
from langchain.schema import Document

def simple_eval_fn(doc: Document) -> bool:
    return "good" in doc.page_content

eval_chain = EvaluationChain.from_llm(llm=None, evaluation_fn=simple_eval_fn)
inputs = [Document(page_content="This is a good example."), Document(page_content="This is bad.")]
results = [eval_chain.evaluate(input_doc) for input_doc in inputs]
print(results)
ATypeError: 'NoneType' object is not callable
B[False, True]
C["good", "bad"]
D[True, False]
Attempts:
2 left
💡 Hint

Check what the evaluation function returns for each document.

📝 Syntax
intermediate
2:00remaining
Which option correctly creates a LangChain evaluation pipeline with a custom metric?

You want to create an evaluation pipeline that uses a custom metric function returning a float score. Which code snippet is syntactically correct?

Aeval_chain = EvaluationChain.from_llm(llm=my_llm, evaluation_fn=lambda doc: 0.9)
Beval_chain = EvaluationChain.from_llm(llm=my_llm, evaluation_fn=lambda doc: return 0.9)
Ceval_chain = EvaluationChain.from_llm(llm=my_llm, evaluation_fn=lambda doc: {0.9})
Deval_chain = EvaluationChain.from_llm(llm=my_llm, evaluation_fn=lambda doc: [0.9])
Attempts:
2 left
💡 Hint

Remember how lambda functions return values in Python.

🔧 Debug
advanced
2:00remaining
Why does this LangChain evaluation pipeline raise an AttributeError?

Given this code snippet:

from langchain.evaluation import EvaluationChain

class MyEval:
    def __call__(self, doc):
        return len(doc.page_content)

eval_chain = EvaluationChain.from_llm(llm=None, evaluation_fn=MyEval())
result = eval_chain.evaluate("Test input")
print(result)

Why does it raise an AttributeError?

LangChain
from langchain.evaluation import EvaluationChain

class MyEval:
    def __call__(self, doc):
        return len(doc.page_content)

eval_chain = EvaluationChain.from_llm(llm=None, evaluation_fn=MyEval())
result = eval_chain.evaluate("Test input")
print(result)
A'MyEval' object is not callable because __call__ is missing
B'str' object has no attribute 'page_content' because evaluate was called with a string, not a Document
CTypeError because llm=None is invalid
DNameError because EvaluationChain is not imported
Attempts:
2 left
💡 Hint

Check the type of the argument passed to the evaluation function.

state_output
advanced
2:00remaining
What is the value of 'scores' after running this LangChain evaluation pipeline?

Consider this code:

from langchain.evaluation import EvaluationChain
from langchain.schema import Document

scores = []
def eval_fn(doc: Document) -> float:
    score = len(doc.page_content) / 10
    scores.append(score)
    return score

eval_chain = EvaluationChain.from_llm(llm=None, evaluation_fn=eval_fn)
inputs = [Document(page_content="Hello world!"), Document(page_content="LangChain")]
results = [eval_chain.evaluate(doc) for doc in inputs]

What is the final value of the list scores?

LangChain
from langchain.evaluation import EvaluationChain
from langchain.schema import Document

scores = []
def eval_fn(doc: Document) -> float:
    score = len(doc.page_content) / 10
    scores.append(score)
    return score

eval_chain = EvaluationChain.from_llm(llm=None, evaluation_fn=eval_fn)
inputs = [Document(page_content="Hello world!"), Document(page_content="LangChain")]
results = [eval_chain.evaluate(doc) for doc in inputs]
A[11, 9]
B[12, 9]
C[1.2, 0.9]
D[1.1, 0.9]
Attempts:
2 left
💡 Hint

Count the characters in each string and divide by 10.

🧠 Conceptual
expert
2:00remaining
Which option best describes the role of 'evaluation_fn' in LangChain's EvaluationChain?

In LangChain's EvaluationChain, what is the primary purpose of the evaluation_fn parameter?

AIt defines a function that takes a Document and returns a metric or boolean indicating evaluation results
BIt specifies the language model used to generate text outputs
CIt configures the logging level for the evaluation process
DIt initializes the database connection for storing evaluation data
Attempts:
2 left
💡 Hint

Think about what evaluation means in this context.