跳到主要内容

现场题(实战)

目录

答题通用结构

现场题统一按 4 步走:

  1. 目标(解决什么)
  2. 流程(怎么做)
  3. 风险(哪里会失败)
  4. 指标(如何证明)

题目 1:流式 + 中断

要点:

  • 前端 fetch + reader 读流
  • AbortController 支持中断
  • 服务端监听 req.close 终止上游

题目 2:RAG 设计

要点:

  • 文档切分 + Embedding + TopK 检索
  • 回答必须带引用编号
  • 前端展示证据面板

面试时建议你先口头说明这张图要表达什么:
“我们先把文档转成可检索结构,再根据问题检索证据,最后带证据生成答案。”
这样面试官会先确认你理解流程,再看你是否能补齐细节(参数、异常、指标)。

题目 3:成本控制方案

要点:

  • 限制输入长度与 max_tokens
  • 历史摘要代替长上下文
  • 设定小模型兜底路径

可直接背的代码片段

流式中断(前端)

const controller = new AbortController();
const res = await fetch("/api/chat/stream", { method: "POST", signal: controller.signal });
// 用户点击停止
controller.abort();

RAG 回答(后端)

app.post("/rag/answer", async (req, res) => {
const qVec = await embed(req.body.question);
const evidence = await search(qVec, { topK: 8, topN: 4 });
const answer = await callLLM(buildPrompt(req.body.question, evidence));
res.json({ answer, evidence });
});

成本守卫(后端)

function enforceBudget(inputTokens: number, maxInput = 4000) {
if (inputTokens > maxInput) {
throw new Error("E_TOKEN_BUDGET_EXCEEDED");
}
}