REPL loop with citations

A REPL is the minimum viable interface for a tutor. The loop reads a question, calls the chain, prints the answer and sources, and updates the chat history. A short Python script gets you a working product.

REPL loop control flow

Read, invoke, print, append, repeat.

booktutor.py
python
import os

qa_system = create_book_qa_system(pdf_path)
chat_history = []

print('Ready to answer questions about your PDF')
print("Type 'quit' to exit")

while True:
    question = input('Ask a question: ')
    if question.lower() == 'quit':
        break

    result = qa_system.invoke({
        'question': question,
        'chat_history': chat_history,
        'book_name': os.path.basename(pdf_path),
    })

    print_result(result)
    chat_history.append((question, result['answer']))

The chat_history list is the memory buffer. Each turn appends a (question, answer) tuple, which the chain uses on the next turn to rewrite follow-ups.

Quiz: Quiz

Loading practice…