Use File Routing in the Loop
Exit

Use File Routing in the Loop

Update chat_loop to call resolve_file_reference before deciding whether to run vector search

💻

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

Routing questions to files

resolve_file_reference returns two values: a cleaned question and the matching chunks (or None). But chat_loop still calls search for every question — it never uses the new function.

This chapter wires up the routing. Before running vector search, chat_loop calls resolve_file_reference. If the function returns chunks, those chunks are used directly. If it returns None, the normal search pipeline runs instead.

The cleaned question replaces the original in build_prompt and in the history append, so the @filename token is stripped from both the model prompt and the stored exchange.

Instructions

  1. In chat_loop, replace top_chunks = search(client, question, chunks, embeddings) with three lines that check for an @filename reference first:
    • clean_question, file_chunks = resolve_file_reference(question, chunks) — this returns the cleaned question (with @filename removed) and the matching chunks, or None if no @filename was found.
    • if file_chunks:
    • top_chunks = file_chunks — use the targeted chunks directly, skipping vector search.
  2. Add the else: fallback for questions without a file reference:
    • clean_question = question — when there is no @filename, the question is unchanged, but assign it to clean_question so the variable name is consistent in the lines below.
    • top_chunks = search(client, question, chunks, embeddings)
  3. Update the build_prompt call: change the first argument from question to clean_question.
  4. Update history.append({"role": "user", "content": question}) to use clean_question instead of question — this stores the cleaned version (without the @filename token) in history.