We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Question Answering

The most common use case for RAG is question answering, which is only slightly different from summarization.

Users ask questions, and we generate direct answers from the retrieved documents. Remember, traditional search returns documents. Users are then expected to poke through those documents to find answers on their own.

Question answering simplifies the whole process by just returning the answer.

User query: "What year was The Revenant released?"

  • Search: List of movie documents...
  • RAG (question answering): "2015"

Different questions need different approaches:

  • Factual: "When was The Revenant released?" → Direct answer
  • Analytical: "Which bear movies are most intense?" → Comparison required
  • Opinion: "What's the best bear movie?" → Subjective reasoning

As such, you should write your system prompt so that the LLM has a sense of how to answer each type of question.

prompt = f"""Answer the following question based on the provided documents.

Question: {query}

Documents:
{context}

General instructions:
- Answer directly and concisely
- Use only information from the documents
- If the answer isn't in the documents, say "I don't have enough information"
- Cite sources when possible

Guidance on types of questions:
- Factual questions: Provide a direct answer
- Analytical questions: Compare and contrast information from the documents
- Opinion-based questions: Acknowledge subjectivity and provide a balanced view

Answer:"""

You could even include examples of good responses to the different types of questions.

Assignment

Add a conversational question-answering command for direct responses.

    1. prompt = f"""Answer the user's question based on the provided movies that are available on Webflyx, a streaming service.
      
      Question: {question}
      
      Documents:
      {context}
      
      Instructions:
      - Answer questions directly and concisely
      - Be casual and conversational
      - Don't be cringe or hype-y
      - Talk like a normal person would in a chat conversation
      
      Answer:"""
      
  1. Search Results:
      - Jurassic Park
      - Mike and Dave Need Wedding Dates
      - House II: The Second Story
      - The Last of the Finest
      - Tokyo Babiron
    
    Answer:
    <ANSWER HERE>
    

If question-answering seems to be working properly, submit the CLI tests.