Chapter 9: Advanced Topics: RAG, Prompt Engineering, and Fine-Tuning
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 68/100
- Loại issue
- Tài liệu
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Ít trao đổi
- Công nghệ
- bun, typescript
- Lĩnh vực
- ai, documentation
Hướng nghiên cứu
Start with chapters/09-advanced-topics.md and the five requested files under examples/09-advanced-topics/, then review Chapters 4 and 5 and the existing README.md and .vitepress/config.ts entry points. Run each sample with bun run and verify the chapter and samples meet the stated line, API, formatting, and friendliness constraints. Done means the chapter is linked, all samples run, and bun x vitepress build passes.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Part of the Build Your Own Coding Agent tutorial. One issue = one chapter (chapters/09-advanced-topics.md) plus its examples/09-advanced-topics/ samples.
Goal (1 sentence): Close the gap between a working prototype and a production-grade coding agent by combining retrieval-augmented generation (which lets your agent answer questions grounded in a private codebase or knowledge base rather than cramming everything into a context window), disciplined prompt structure, and an honest take on model customization.
After this chapter you can
- Build a RAG pipeline that chunks documents, embeds them with Voyage AI, retrieves top-k chunks by cosine similarity, and injects them into
client.messages.createas either plainusercontent or typeddocumentblocks that unlock Claude's citation output. - Apply prompt-engineering best practices - XML tags, role prompts, few-shot examples, chain-of-thought, and ordering content to match Claude's attention patterns - to reliably control Claude's output format and reasoning style.
- Use the Citations feature to ground answers in retrieved documents via typed
documentcontent blocks andcitations: { enabled: true }. - Understand
cache_controlcaching tradeoffs as a prompting-layer alternative to fine-tuning, and confirm hits viausage.cache_read_input_tokens. - Decide when to fine-tune (Bedrock, Claude Haiku) versus when RAG plus prompt engineering suffices.
What to cover (ONE paragraph, not a list)
The chapter opens by wiring up a full RAG pipeline: chunking a document corpus, calling Voyage AI's embeddings API (the Anthropic API has no embeddings endpoint), storing vectors locally, computing cosine similarity to retrieve top-k chunks, and injecting them into client.messages.create. It then contrasts two injection modes - plain text in a user turn versus typed document content blocks with source: { type: "text", media_type: "text/plain", data: "..." } (or base64, URL, and file_id variants) and citations: { enabled: true } - showing that omitting citations: { enabled: true } suppresses citation output and an invalid source shape returns a 400. Prompt structure comes next: wrapping instructions in XML tags (<instructions>, <context>, <examples>), placing stable guidance in the system field, ordering content to match Claude's attention patterns, supplying 2-4 few-shot user/assistant pairs, and eliciting chain-of-thought either via the top-level thinking parameter (no beta header needed for adaptive thinking) or an explicit scratchpad prompt. The chapter closes by covering cache_control: { type: "ephemeral" } on large byte-stable system prompts and retrieved document blocks - confirm hits via usage.cache_read_input_tokens - as a prompting-layer alternative to fine-tuning, and situating fine-tuning honestly: the first-party Anthropic API exposes no general fine-tuning endpoint; Amazon Bedrock offers it for select Claude models such as Claude Haiku; RAG plus prompt engineering covers most use cases and should be the default.
Going deeper (optional asides - keep OFF the main line)
- Bedrock fine-tuning workflow mechanics (data format, job creation, cost model).
Out of scope (defer - do NOT preview)
- Vector-database hosting and production indexing strategies (later infrastructure chapter).
- Evaluating retrieval quality (recall@k, NDCG) - deserves its own treatment.
Code samples - examples/09-advanced-topics/
-
rag-pipeline.ts- chunk + Voyage AI embeddings + top-k cosine retrieval, injected asusercontent. -
citations-demo.ts-documentblocks withsource+citations: { enabled: true }; print citations. -
prompt-engineering.ts- compare bare / XML+few-shot / chain-of-thought; logusage. -
cache-large-prompt.ts-cache_controlon a large stable prompt; checkcache_read_input_tokens. -
finetuning-tradeoffs.ts- RAG+prompt approach matching a fine-tuning use case; note when Bedrock fine-tuning is warranted.
Must-keep for a beginner (floor - never cut for brevity)
- The run command for
rag-pipeline.ts(the first sample). - "Never hardcode your key; it comes from the environment" (once, in prose) - this applies to both
ANTHROPIC_API_KEYand the Voyage AI key. - Bun auto-loads
.env; no loader config needed. - The non-obvious gotcha: omitting
citations: { enabled: true }silently suppresses citation output; an invalidsourceshape returns a 400, not a validation warning.
Friendliness floor (never cut - terse is not friendly)
- The chapter addresses the reader as "you", never "the user" or "one".
- The intro AND at least one section open with a warm, second-person sentence.
Key APIs (flat list, reference only - NOT a coverage checklist)
client.messages.create, client.messages.countTokens, system, document blocks, source, citations, cache_control, usage.cache_read_input_tokens, usage.cache_creation_input_tokens, thinking, XML prompt structure, Voyage AI embeddings endpoint, Bedrock fine-tuning (Claude Haiku)
Prerequisites
Chapters 4 and 5. RAG samples need a VOYAGE_API_KEY in .env.
Definition of done
- Chapter at
chapters/09-advanced-topics.md, <=120 lines, <=4 main-line H2s plus an optional "What's next" closer (pastewc -lANDgrep -c '^## 'in the PR). - Every sample runnable with
bun run, imported via<<< @/examples/09-advanced-topics/file.ts, <=35 lines, comment:code <=0.30. - One-home rule held: no prose sentence restates an inline code comment.
- Friendliness floor held: reader addressed as "you"; intro + >=1 section open warm.
- Samples use only real
@anthropic-ai/sdksurface; ASCII punctuation only. - Optional material lives in Going-deeper asides, not main-line H2s.
- Linked from
README.mdand the.vitepress/config.tssidebar;bun x vitepress buildpasses. - Citations
sourceschema and fine-tuning availability verified against current docs.
- Ngôn ngữ chính
- TypeScript
- Star
- 0
- Fork
- 1
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Chúng tôi chưa kiểm tra các tệp thiết lập môi trường của dự án này. Hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của yagop/coding-agents-tutorial
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
documentation
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 45/100
-
documentation
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 52/100
yagop/coding-agents-tutorial#8 · 1 bình luận ·
Tất cả issue của yagop/coding-agents-tutorial
Issue tương tự
-
ADD openalgoĐang mởtemplate
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
factory-active factory-automatic task-bug-reproduction-success task-identify-harness-labels-done task-identify-issue-type-done
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 90/100
vercel/ai#21528 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug Needs: Triage :mag:
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
microsoft/fluentui-contrib#671 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
sveltejs/acorn-typescript#150 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100