Search and Answer

Wire up the final four calls — search, prompt, generate, and print

💻

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

The last four lines

At this point main() has the PDF's text loaded as chunks with their embeddings — either from the cache or freshly computed. The remaining work is four function calls, one per stage of the RAG pipeline:

StepFunctionInputOutput
Searchsearch()question + chunks + embeddingstop matching chunks
Promptbuild_prompt()question + top chunksa single prompt string
Generategenerate_answer()promptthe model's answer
Printprint_result()answer + top chunksformatted output

Each function was built and tested in an earlier chapter. main() simply chains them together: the output of one becomes the input of the next.

The show_sources choice

The last call uses show_sources=False so the output is clean for end users. During development you can change it to True to see which chunks the model used — helpful for debugging bad answers.

Instructions

Finish the main function. The starter code has argument parsing and cache logic from the previous chapters already filled in.

  1. Create a variable named top_chunks. Assign it search(client, question, chunks, embeddings).
  2. Create a variable named prompt. Assign it build_prompt(question, top_chunks).
  3. Create a variable named answer. Assign it generate_answer(client, prompt).
  4. Call print_result(answer, top_chunks, show_sources=False).