Bird
0
0

Consider this Langchain code snippet:

medium📝 Predict Output Q4 of 15
LangChain - RAG Chain Construction
Consider this Langchain code snippet:
result = qa({'query': 'Define machine learning'})
print(result['source_documents'][0].metadata['source'])

If the first retrieved document's source is 'ml_intro.txt', what will be printed?
A'source_documents'
B'machine_learning.pdf'
C'ml_intro.txt'
DAn error because 'metadata' does not exist
Step-by-Step Solution
Solution:
  1. Step 1: Identify the source document metadata

    The 'source_documents' list contains documents with metadata including 'source'.
  2. Step 2: Access the first document's source

    Accessing metadata['source'] returns the source filename.
  3. Final Answer:

    'ml_intro.txt' -> Option C
  4. Quick Check:

    Metadata source matches the document's filename [OK]
Quick Trick: Metadata['source'] holds the document filename [OK]
Common Mistakes:
  • Confusing the source with document content
  • Expecting a KeyError when metadata exists
  • Assuming source_documents is a string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LangChain Quizzes