Skip to main content

Personal knowledge base Q&A · three-stage RAG

Turn company policies and personal notes into a queryable knowledge base, and ask in plain language to get the right entry.

Disclaimer: The content shown on this page is generated by an AI model and is for reference only. It does not constitute any guarantee of effectiveness. Actual output and Credits consumption are subject to "Usage Details" in the console.
This page has qwen3.7-plus produce an entire three-stage RAG script in one shot and runs it through three plain-language questions like "how to get the travel money back": the colloquial question is first rewritten into standard retrieval terms, then locally recalled and re-ranked for an answer, and all three hit the correct entry. The key is not keyword matching but first translating the colloquial phrasing into standard retrieval terms—this is the most practical approach when landing RAG (retrieval-augmented generation). Call chain: (1) Query rewriting (qwen3.7-plus turns the colloquial question → standard retrieval terms) → (2) Local recall (numpy implementing TF-IDF + cosine similarity; runs locally and consumes no Credits) → (3) Re-rank and answer (qwen3.7-plus picks the correct entry from the Top5 candidates and answers).

Steps: Have the model directly produce the full RAG script

Send the following instruction to qwen3.7-plus:
Please write a complete, runnable Python script for "personal knowledge base Q&A (three-stage RAG)":
Stage 1 Query rewriting (LLM): rewrite the user's colloquial question into 3-5 standard retrieval terms, concatenate them with the original question, then retrieve;
Stage 2 Local recall: use only numpy to implement Chinese-character bigram + TF-IDF + L2 normalization + cosine similarity, recall Top5;
Stage 3 Re-rank and answer (LLM): hand the Top5 candidates to the model, have it pick the entry that truly answers the question and answer in one sentence.
The knowledge base has 10 built-in Chinese FAQs (reimbursement / leave / annual leave / password reset / equipment request / onboarding / social security / comp time / invoice / travel);
use urllib to call the Token Plan API (api_key read from an environment variable, model=qwen3.7-plus), and tally tokens cumulatively;
main runs 3 colloquial queries "with wording completely different from the original text" and prints question → rewritten terms → recalled Top3 → final answer;
at the end, assert a self-check on whether all three hit the correct entry, and print SELF-CHECK PASSED on success.
Question: How do I get the money back after a business trip, and do I need to paste receipts?
Rewritten retrieval terms: travel expense reimbursement process receipt pasting rules travel expense reimbursement policy invoice reimbursement requirements
Recalled Top3:
  1. ID 1 | similarity 0.3536 | Expense reimbursement: travel expenses must submit a reimbursement form within 5 working days after return…
  2. ID 10 | similarity 0.0879 | Travel standard: domestic travel lodging standard for first-tier cities ¥500 per night,…
  3. ID 9 | similarity 0.0591 | Invoice issuance: the company's billing information must be requested from Finance, VAT special invoice…
Final answer: After a business trip you must submit a reimbursement form within 5 working days after return, and attach receipts such as invoices and itinerary for reimbursement.

Question: I've worked a full year and want to take a few days off to travel—how many days can I take?
Rewritten retrieval terms: paid annual leave days one full year of service annual leave day-count standard employee paid annual leave regulation
Recalled Top3:
  1. ID 3 | similarity 0.2258 | Annual leave: 1 year of service gives 5 days of paid annual leave; 10 years gives 10 days…
  2. ID 10 | similarity 0.0856 | Travel standard: domestic travel lodging standard for first-tier cities ¥500 per night,…
  3. ID 9 | similarity 0.0600 | Invoice issuance: the company's billing information must be requested from Finance, VAT special invoice…
Final answer: 1 year of service gives 5 days of paid annual leave.

Question: I reported on the 18th, mid-month—does my social security and housing fund start deducting this month or next month?
Rewritten retrieval terms: social security and housing fund payment time new employee social security start month mid-month onboarding social security housing fund deduction start month
Recalled Top3:
  1. ID 7 | similarity 0.3273 | Social security payment: social security is paid from the month of onboarding; onboard before the 15th and that month…
  2. ID 5 | similarity 0.1422 | Equipment request: new employees may apply for one laptop, requires department head…
  3. ID 8 | similarity 0.1019 | Overtime comp time: weekday overtime may apply for comp time, exchanged 1:1,…
Final answer: Because you reported on the 18th, which is after the 15th, your social security and housing fund will take effect and be deducted starting next month.

SELF-CHECK PASSED

Notes

  • Consumption: This playbook involves text generation (qwen3.7-plus), consumed in two phases—generating the script produces a 6.6 KB runnable script in one shot; at runtime each question needs 2 calls (query rewriting + re-rank and answer), so more questions mean higher consumption. The local TF-IDF recall step consumes no Credits.
  • The role of query rewriting: Across two rounds of testing, directly using the colloquial "how to get the money back after a business trip" for retrieval gives a local recall similarity of 0.0000 (complete recall failure, with the answer drifting to travel standards); after adding query rewriting, the same question's similarity rises to 0.3536 and correctly hits the "expense reimbursement" entry. All three questions hit the correct entry, and the script's self-check outputs SELF-CHECK PASSED.
Model Playground
Fine-tuning
Model Compression
Statistics and Monitoring
Support