鼎稔道學館
← 開源 LLM/Cookbook/00
00

How we use the RAG API

CC0

從零開始:30 行 Python 跑通 RAG → top-N 檢索 → 注入 LLM context。

RAG APIPythoncurlTypeScript
🔒 重現條件鎖
MODEL    = "lius-cc/Daoism-Qwen3.5-9B"
DATASET  = "lius-cc/daoism-knowledge-rag@v1"
RAG_API  = "https://lius.cc/api/llm-rag"
SNAPSHOT = "2026-05-17"

1 · Bare-minimum call

只需要 `requests`,沒有其他依賴。

import requests

RAG_API = "https://lius.cc/api/llm-rag"  # 30 req/min · free

res = requests.post(RAG_API, json={"q": "三朝醮", "n": 5, "types": ["ritual", "paper"]}, timeout=10)
res.raise_for_status()
rag = res.json()

for hit in rag.get("hits", []):
    print(f"[{hit['type']:9}] {hit['name']:30}  score={hit['score']}  {hit['url']}")

2 · Feed RAG results into an LLM

Works with any OpenAI-compatible endpoint (vLLM, llama-cpp-python, Ollama).

import requests
from openai import OpenAI

client = OpenAI(api_key="dummy", base_url="http://localhost:8000/v1")

def ask(question: str):
    res = requests.post(
        "https://lius.cc/api/llm-rag",
        json={"q": question, "n": 5},
        timeout=10,
    )
    res.raise_for_status()
    rag = res.json()
    blocks = rag.get("context_blocks", [])
    if not blocks:
        return "No published LIUS context found for this question."

    ctx = "\n\n---\n\n".join(
        f"{b.get('ref', '')} {b.get('title', '')} ({b.get('type', '')})\n{b.get('text', '')[:1800]}"
        for b in blocks
    )

    return client.chat.completions.create(
        model="lius-cc/Daoism-Qwen3.5-9B",
        messages=[
            {"role": "system", "content": f"Answer based on these canonical entries:\n\n{ctx}"},
            {"role": "user", "content": question},
        ],
        max_tokens=4096,
    ).choices[0].message.content

3 · Reproducibility lock

若要在論文裡引用,請鎖以下變數:

REPRO = {
    "model":   "lius-cc/Daoism-Qwen3.5-9B",
    "dataset": "lius-cc/daoism-knowledge-rag@v1",
    "rag_api": "https://lius.cc/api/llm-rag",
    "snapshot": "2026-05-17",
    "max_tokens": 4096,
    "temperature": 0.0,
}

📜 本 recipe 採 CC0 1.0 · 改一行 prompt → 寫到論文裡 → 引用 LIUS API

引用建議:We use the open-source Daoism-Qwen3.5-9B with the public RAG API at https://lius.cc/api/llm-rag (Liu & Dingren Daoxue Lab, 2026).

← 回 Cookbook 索引