Build the Loop

Add the chat_loop function with a while True input cycle

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

Wrapping the loop in a function

The interactive loop goes into its own function — chat_loop — rather than directly inside main. This keeps main focused on startup: reading arguments, finding the cache, and indexing if needed. Once the assistant is ready, main hands off to chat_loop, which runs for the rest of the session.

The ready message mentions /help — you'll build that command in Lesson 4. Until then, the loop only handles plain questions. If you type /help now, the assistant will search the index for an answer instead.

Instructions

  1. Define a function called chat_loop that takes client, chunks, and embeddings.
  2. Print "Assistant ready. Type your question, or /help for commands.\n".
  3. Start a while True: loop.
  4. Declare a variable called question and assign it input("You: ").strip() — this displays You: as a prompt, waits for the user to type something and press Enter, then removes any leading or trailing whitespace from their input.
  5. Add if not question: continue — this skips to the next loop iteration when the user presses Enter without typing anything, preventing an empty string from reaching search.