Bird
Raised Fist0
Prompt Engineering / GenAIml~10 mins

Combining retrieved context with LLM in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add the retrieved context to the prompt before sending it to the LLM.

Prompt Engineering / GenAI
prompt = "Answer the question based on the context: " + [1] + "\nQuestion: " + question
Drag options to blanks, or click blank then click option'
Auser_input
Bquestion
Cllm_response
Dretrieved_context
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the question twice instead of the context.
Using the LLM response before generating it.
2fill in blank
medium

Complete the code to combine the retrieved context and the user question into a single input for the LLM.

Prompt Engineering / GenAI
llm_input = f"Context: [1]\nQuestion: {question}"
Drag options to blanks, or click blank then click option'
Auser_input
Bllm_output
Cretrieved_context
Dsystem_message
Attempts:
3 left
💡 Hint
Common Mistakes
Using the user input instead of the retrieved context.
Forgetting to include the context in the input string.
3fill in blank
hard

Fix the error in the code that sends the combined context and question to the LLM for completion.

Prompt Engineering / GenAI
response = llm.complete(prompt=[1])
Drag options to blanks, or click blank then click option'
Allm_input
Bquestion
Cretrieved_context
Duser_input
Attempts:
3 left
💡 Hint
Common Mistakes
Passing only the question or only the context to the LLM.
Using a variable that does not contain the full prompt.
4fill in blank
hard

Fill both blanks to create a dictionary that maps each document to its text and filters documents with length greater than 100.

Prompt Engineering / GenAI
filtered_docs = {doc.id: doc.[1] for doc in documents if len(doc.[2]) > 100}
Drag options to blanks, or click blank then click option'
Atext
Bcontent
Cmetadata
Dsummary
Attempts:
3 left
💡 Hint
Common Mistakes
Using different attributes for the dictionary value and length check.
Using metadata or summary which may not contain full text.
5fill in blank
hard

Fill all three blanks to create a prompt dictionary with keys 'context', 'question', and 'max_tokens' for the LLM call.

Prompt Engineering / GenAI
prompt_data = {"context": [1], "question": [2], "max_tokens": [3]
Drag options to blanks, or click blank then click option'
Aretrieved_context
Buser_question
C150
Dllm_response
Attempts:
3 left
💡 Hint
Common Mistakes
Using the LLM response instead of the question or context.
Setting max_tokens to a string instead of a number.

Practice

(1/5)
1. Why do we combine retrieved context with a large language model (LLM)?
easy
A. To give the model extra information it did not learn before
B. To make the model run faster
C. To reduce the size of the model
D. To replace the model's training data

Solution

  1. Step 1: Understand the purpose of retrieved context

    Retrieved context provides additional information that the model might not have seen during training.
  2. Step 2: Connect context to model output quality

    Providing this extra information helps the model give better and more accurate answers.
  3. Final Answer:

    To give the model extra information it did not learn before -> Option A
  4. Quick Check:

    Extra info improves answers = D [OK]
Hint: Extra info helps model answer better [OK]
Common Mistakes:
  • Thinking context speeds up the model
  • Believing context shrinks the model size
  • Assuming context replaces training data
2. Which of the following is the correct way to combine retrieved context with an LLM prompt?
easy
A. prompt = question * context
B. prompt = question + context
C. prompt = context + ' ' + question
D. prompt = context - question

Solution

  1. Step 1: Understand prompt construction

    The prompt should start with the context followed by the question to give the model relevant info first.
  2. Step 2: Check syntax correctness

    Using string concatenation with '+' is correct; multiplication or subtraction of strings is invalid.
  3. Final Answer:

    prompt = context + ' ' + question -> Option C
  4. Quick Check:

    Context before question with '+' = A [OK]
Hint: Concatenate context and question with + [OK]
Common Mistakes:
  • Putting question before context
  • Using * or - operators on strings
  • Not adding space between context and question
3. Given the code below, what will be the output?
context = 'The capital of France is Paris.'
question = 'What is the capital of France?'
prompt = context + ' ' + question
response = llm.generate(prompt)
print(response)
Assuming llm.generate() returns the model's answer, what is the likely output?
medium
A. Paris
B. London
C. I don't know
D. Error: undefined variable

Solution

  1. Step 1: Analyze the prompt content

    The prompt includes the context 'The capital of France is Paris.' followed by the question.
  2. Step 2: Predict model output based on context

    The model uses the context to answer correctly with 'Paris'.
  3. Final Answer:

    Paris -> Option A
  4. Quick Check:

    Context guides answer = Paris [OK]
Hint: Context gives correct answer to question [OK]
Common Mistakes:
  • Ignoring context and guessing wrong
  • Assuming code error without cause
  • Thinking model says 'I don't know'
4. You wrote this code to combine context with a question:
context = 'Water boils at 100 degrees Celsius.'
question = 'At what temperature does water boil?'
prompt = question + ' ' + context
response = llm.generate(prompt)
print(response)
Why might the model give a less accurate answer?
medium
A. Because the context is missing important info
B. Because the question comes before the context, confusing the model
C. Because the model cannot handle string concatenation
D. Because the prompt is too short

Solution

  1. Step 1: Check prompt order

    The prompt puts the question before the context, which may confuse the model about what info to use.
  2. Step 2: Understand best practice

    Context should come first to provide relevant info before the question.
  3. Final Answer:

    Because the question comes before the context, confusing the model -> Option B
  4. Quick Check:

    Context before question improves accuracy = B [OK]
Hint: Put context before question in prompt [OK]
Common Mistakes:
  • Thinking model can't concatenate strings
  • Assuming context lacks info
  • Believing prompt length is the issue
5. You want to build a system that answers questions about a company's products using an LLM. You have a large product manual. What is the best way to combine the manual with the LLM to get accurate answers?
hard
A. Train a new LLM from scratch on the manual
B. Feed the entire manual as a prompt to the LLM every time
C. Only ask the question without any manual context
D. Retrieve relevant sections from the manual and add them as context before the question in the prompt

Solution

  1. Step 1: Consider prompt size limits

    Feeding the entire manual is too large and inefficient for the LLM prompt.
  2. Step 2: Use retrieval to select relevant info

    Retrieving relevant sections and adding them as context helps the model answer accurately without overload.
  3. Step 3: Evaluate other options

    Asking without context misses info; training new LLM is costly and unnecessary.
  4. Final Answer:

    Retrieve relevant sections from the manual and add them as context before the question in the prompt -> Option D
  5. Quick Check:

    Relevant context retrieval + LLM = A [OK]
Hint: Retrieve relevant info, then prompt LLM [OK]
Common Mistakes:
  • Trying to input entire manual at once
  • Ignoring context and asking only question
  • Thinking retraining is always needed