Chuyển đến nội dung

Bài 36 · Projects and Practice

Project: Research Assistant Agent

Build a research assistant agent end to end: plan sub-questions, search a document set, take notes, cite sources and write a report, with runnable Python stand-ins.

In this project you will build a research assistant: you give it a question, and it breaks the question into sub-questions, searches a small collection of documents, collects notes with sources, and writes a short report with citations. It combines ideas from earlier lessons: planning, retrieval (RAG), the orchestrator pattern (orchestrator and workers) and a final check for unsupported claims.

No real model or web search is available here. The planner, summarizer and writer are deterministic stand-ins, and the "web" is a tiny in-memory document set. Where a real system would call a model, the code marks the spot, so you can replace it later. The architecture is the real part.

The design

flowchart TD
  Q["Research question"] --> P["Planner: sub-questions"]
  P --> S["Searcher: find documents"]
  S --> N["Note taker: facts with sources"]
  N --> W["Writer: report with citations"]
  W --> C["Checker: every claim has a source"]
  C -->|"gaps"| P
  C -->|"ok"| R["Final report"]

A loop: if the checker finds gaps, the planner adds sub-questions.

Requirements:

  1. Split the question into sub-questions.
  2. Search the document set for each.
  3. Record notes as {fact, source} pairs.
  4. Write a report where each claim carries a source id.
  5. Refuse to include a claim without a source.
  6. Cap the work: at most three rounds and five notes per sub-question.
Python A tiny searchable corpus

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last query returns an empty list. That matters: the agent must cope with "nothing found".

Step 2: plan and take notes

The planner turns the question into sub-questions (a stand-in rule here). The note taker pulls the sentence from each document that best matches the sub-question and saves it with its source id.

Python Plan, search and take notes

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 3: write with citations and check

The writer builds the report only from notes. The checker verifies that every sentence in the report ends with a known source id and that the cited note really contains that claim. A report line without a source is rejected.

Python Write a cited report and verify it

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The checker catches the invented last sentence. With a real model the writer is the part that may invent things, which is exactly why you verify against the notes rather than trusting it.

Step 4: the full loop with a round limit

Put the pieces together: plan, gather notes, write, check. If sub-questions found nothing, report the gap honestly instead of filling it in.

Python The complete research loop

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Extending the project

  • Replace the stand-in planner and writer with real model calls that return JSON, and keep the checker unchanged.
  • Swap search for the vector index from embeddings and vector search.
  • Run sub-questions in parallel with a thread pool.
  • Add a budget guard (see deploying agents).
  • Write evals: questions whose answers you know, plus unanswerable questions that should produce a decline (see evaluating agents).
  • Treat retrieved text as untrusted (see guardrails).

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why must the report writer only use the collected notes?

    Grounding in notes makes verification possible.
  2. 2. What should the agent do when a sub-question finds no documents?

    An honest gap is better than a fabricated fact.
  3. 3. What does the checker verify?

    Lines without a matching note are flagged as unsupported.
  4. 4. Why cap the number of research rounds?

    Every loop needs a limit in code.

Summary

  • The assistant plans sub-questions, searches, records notes with sources, writes and checks.
  • Grounding the report in notes and verifying citations catches invented claims.
  • Missing information is reported, never filled in.
  • Caps on rounds and notes keep the run bounded.
  • Replace the stand-ins with real model calls and a real retriever to take it further.

Next: Project: Customer Support Agent

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.