🆕 程式碼
/**
問題:${originalQ}\n請輸出一個 JSON 陣列,例如 ["問法1","問法2",...],不要多餘說明。
}/**
for (const q of queries) {
const { sources } = await answerWithRAG({ tenant, ns, query: q, topK });
for (const s of sources) {
if (!seen.has(s.id)) {
seen.set(s.id, s);
results.push(s);
}
}
}
// 按 score 排序,取前 topK
results.sort((a, b) => (b.score || 0) - (a.score || 0));
return results.slice(0, topK);
}
只貼新增的分支,保留 Day 19 / 23 的 default & section 分支:
import { retrieveWithQRewrite } from "../../../../../src/day24_qrewrite.js";
...
if (strategy === "qrewrite") {
const chunks = await retrieveWithQRewrite({ tenant, ns, query: q, topK: 6 });
const ctxText = chunks.map((h,i)=># 片段${i+1}(${h.docId}|${h.id.split("#")[1]})\n${h.text}
).join("\n\n");
const res = await openai.chat.completions.create({
model:"gpt-4o-mini", temperature:0.2,
messages:[
{ role:"system", content:"你是嚴謹的客服知識庫助理,僅依據提供片段回答;不足請說明。" },
{ role:"user", content:`問題:${q}\n\n片段:\n${ctxText}\n\n請用繁體中文回答。` }
]
});
const answer = res.choices?.[0]?.message?.content?.trim() || "沒有找到足夠資訊。";
return NextResponse.json({ ok:true, strategy:"qrewrite", answer, sources: chunks });
}