Detect the @filename Syntax
Exit

Detect the @filename Syntax

Write resolve_file_reference to extract @filename from a question and return the matching chunks

💻

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

Targeting a specific file

Vector search finds the most relevant chunks across all indexed files. That works well for general questions, but sometimes you already know which file has the answer. Searching across everything wastes time and may pull in chunks from the wrong file.

The @filename syntax lets you skip the search entirely. If your question starts with @readme.md, the assistant collects all chunks from readme.md and sends them directly to the model — no embeddings, no ranking. For example: @api-reference.md what authentication method does it use?

You'll write resolve_file_reference to detect the @filename pattern and return either the matched file's chunks or None if no match is found.

Python's re module provides re.search, which scans a string for a pattern and returns a match object — or None if nothing matches. The pattern @(\S+) matches @ followed by one or more non-whitespace characters.

Instructions

  1. Add import re to the imports, on a new line after import os.
  2. Define a function called resolve_file_reference that takes question and chunks as arguments.
  3. Inside resolve_file_reference, assign match = re.search(r'@(\S+)', question) — this looks for an @filename token anywhere in the question.
  4. Add if not match: then return question, None.
  5. Assign filename = match.group(1) — this extracts the filename without the @.
  6. Assign file_chunks = [c for c in chunks if c["source"] == filename] — this collects all chunks whose source matches exactly.
  7. Add if not file_chunks: then file_chunks = [c for c in chunks if filename in c["source"]] — this falls back to a partial match if no exact match exists.
  8. Add if not file_chunks: then return question, None — if still no chunks, treat it as a normal question.
  9. Assign clean_question = question.replace(match.group(0), "").strip() — this removes the @filename token from the question text before sending it to the model.
  10. Return clean_question, file_chunks.