Init: 第一个版本
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
# 复制为 .env 后重启服务。留空则只做检索,回答直接摘自最相关切片。
|
||||
OPENAI_API_KEY=
|
||||
OPENAI_BASE_URL=https://api.openai.com/v1
|
||||
OPENAI_MODEL=gpt-4o-mini
|
||||
|
||||
# 可选。嵌入模型默认从 Hugging Face 下载,连不上时会自动改用镜像。
|
||||
# HF_ENDPOINT=https://hf-mirror.com
|
||||
@@ -0,0 +1,9 @@
|
||||
.venv/
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
.pytest_cache/
|
||||
.mypy_cache/
|
||||
.ruff_cache/
|
||||
.env
|
||||
data/
|
||||
.DS_Store
|
||||
@@ -0,0 +1 @@
|
||||
"""知迹:本地 embedding + 向量数据库的 RAG 问答。"""
|
||||
@@ -0,0 +1,4 @@
|
||||
from app.main import main
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,46 @@
|
||||
import re
|
||||
|
||||
from app.config import CHUNK_OVERLAP, CHUNK_SIZE
|
||||
|
||||
|
||||
def chunk_text(text: str, size: int = CHUNK_SIZE, overlap: int = CHUNK_OVERLAP) -> list[str]:
|
||||
"""按中文标点和段落边界切分,相邻切片保留一小段重叠。"""
|
||||
text = re.sub(r"\r\n?", "\n", text).replace("\x00", "").strip()
|
||||
if not text:
|
||||
return []
|
||||
if len(text) <= size:
|
||||
return [text]
|
||||
|
||||
units = [unit for unit in re.split(r"(?<=[。!?;\n])", text) if unit]
|
||||
pieces: list[str] = []
|
||||
step = max(1, size - overlap)
|
||||
for unit in units:
|
||||
if len(unit) <= size:
|
||||
pieces.append(unit)
|
||||
continue
|
||||
for start in range(0, len(unit), step):
|
||||
pieces.append(unit[start : start + size])
|
||||
|
||||
chunks: list[str] = []
|
||||
buffer = ""
|
||||
for piece in pieces:
|
||||
if not buffer:
|
||||
buffer = piece
|
||||
continue
|
||||
if len(buffer) + len(piece) <= size:
|
||||
buffer += piece
|
||||
continue
|
||||
chunks.append(buffer.strip())
|
||||
tail = buffer[-overlap:] if overlap else ""
|
||||
candidate = f"{tail}{piece}"
|
||||
buffer = candidate if len(candidate) <= size else piece
|
||||
if buffer.strip():
|
||||
chunks.append(buffer.strip())
|
||||
|
||||
result: list[str] = []
|
||||
for chunk in chunks:
|
||||
chunk = re.sub(r"\n{3,}", "\n\n", chunk).strip()
|
||||
if len(chunk) < 8 or (result and chunk == result[-1]):
|
||||
continue
|
||||
result.append(chunk)
|
||||
return result or [text]
|
||||
@@ -0,0 +1,47 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parent.parent
|
||||
DATA_DIR = ROOT / "data"
|
||||
STATIC_DIR = ROOT / "static"
|
||||
|
||||
EMBEDDING_MODEL = "BAAI/bge-small-zh-v1.5"
|
||||
COLLECTION_NAME = "knowledge_bge_small_zh_v15"
|
||||
CHUNK_SIZE = 360
|
||||
CHUNK_OVERLAP = 60
|
||||
DEFAULT_TOP_K = 4
|
||||
LOW_SCORE = 0.45
|
||||
|
||||
os.environ.setdefault("ANONYMIZED_TELEMETRY", "False")
|
||||
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
|
||||
|
||||
|
||||
def load_dotenv() -> None:
|
||||
path = ROOT / ".env"
|
||||
if not path.exists():
|
||||
return
|
||||
for raw in path.read_text(encoding="utf-8").splitlines():
|
||||
line = raw.strip()
|
||||
if not line or line.startswith("#") or "=" not in line:
|
||||
continue
|
||||
key, value = line.split("=", 1)
|
||||
key = key.strip()
|
||||
value = value.strip().strip('"').strip("'")
|
||||
if key and key not in os.environ:
|
||||
os.environ[key] = value
|
||||
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
def llm_config() -> dict | None:
|
||||
key = os.getenv("OPENAI_API_KEY", "").strip()
|
||||
if not key:
|
||||
return None
|
||||
base = os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1").strip()
|
||||
model = os.getenv("OPENAI_MODEL", "gpt-4o-mini").strip()
|
||||
return {
|
||||
"key": key,
|
||||
"base": (base or "https://api.openai.com/v1").rstrip("/"),
|
||||
"model": model or "gpt-4o-mini",
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import logging
|
||||
import os
|
||||
import urllib.request
|
||||
|
||||
from app.config import EMBEDDING_MODEL
|
||||
|
||||
logger = logging.getLogger("learn_rag")
|
||||
QUERY_PREFIX = "为这个句子生成表示以用于检索相关文章:"
|
||||
|
||||
|
||||
def configure_download() -> None:
|
||||
"""Hugging Face 不可达时改用国内镜像,避免首次下载卡住。"""
|
||||
if os.getenv("HF_ENDPOINT"):
|
||||
return
|
||||
try:
|
||||
with urllib.request.urlopen("https://huggingface.co", timeout=4) as response:
|
||||
if response.status < 400:
|
||||
return
|
||||
except Exception:
|
||||
logger.info("无法连接 huggingface.co,嵌入模型改从 hf-mirror.com 下载")
|
||||
os.environ["HF_ENDPOINT"] = "https://hf-mirror.com"
|
||||
|
||||
|
||||
class Embedder:
|
||||
def __init__(self) -> None:
|
||||
configure_download()
|
||||
from fastembed import TextEmbedding
|
||||
|
||||
logger.info("正在加载嵌入模型 %s", EMBEDDING_MODEL)
|
||||
self.model = TextEmbedding(model_name=EMBEDDING_MODEL)
|
||||
probe = next(iter(self.model.embed(["书店"])))
|
||||
self.dimension = len(probe)
|
||||
logger.info("嵌入模型就绪,维度 %s", self.dimension)
|
||||
|
||||
def embed_documents(self, texts: list[str]) -> list[list[float]]:
|
||||
if not texts:
|
||||
return []
|
||||
return [vector.tolist() for vector in self.model.embed(texts)]
|
||||
|
||||
def embed_query(self, text: str) -> list[float]:
|
||||
query_embed = getattr(self.model, "query_embed", None)
|
||||
if query_embed is not None:
|
||||
try:
|
||||
return next(iter(query_embed([text]))).tolist()
|
||||
except Exception:
|
||||
logger.info("query_embed 不可用,改用查询前缀")
|
||||
prefixed = QUERY_PREFIX + text
|
||||
return next(iter(self.model.embed([prefixed]))).tolist()
|
||||
@@ -0,0 +1,85 @@
|
||||
import logging
|
||||
|
||||
import httpx
|
||||
|
||||
from app.config import LOW_SCORE, llm_config
|
||||
|
||||
logger = logging.getLogger("learn_rag")
|
||||
|
||||
SYSTEM_PROMPT = """你是「知迹」的知识库问答助手。只能根据给出的知识切片回答,不要补充切片之外的信息,也不要编造。
|
||||
如果切片不足以回答,就说明知识库里没有这项信息。
|
||||
用自然的中文直接回答问题,不要使用 Markdown,不要写标题。
|
||||
引用事实时在相关句末标注编号,格式必须是〔来源1〕这种全角括号。编号只能使用给定切片的编号。"""
|
||||
|
||||
|
||||
def build_context(chunks: list[dict]) -> str:
|
||||
blocks = []
|
||||
for chunk in chunks:
|
||||
blocks.append(
|
||||
f"〔来源{chunk['n']}〕《{chunk['title']}》相关度 {chunk['score']:.0%}\n{chunk['text']}"
|
||||
)
|
||||
return "\n\n".join(blocks)
|
||||
|
||||
|
||||
def extractive_answer(chunks: list[dict], low_confidence: bool) -> str:
|
||||
if not chunks:
|
||||
return "知识库里还没有可以检索的内容。"
|
||||
lead = ""
|
||||
if low_confidence:
|
||||
lead = "最接近的切片和问题只是部分相关,下面按原文摘出,请以右侧切片为准。\n\n"
|
||||
lines = [lead + chunks[0]["text"].strip() + f"〔来源{chunks[0]['n']}〕"]
|
||||
for chunk in chunks[1:2]:
|
||||
if chunks[0]["score"] - chunk["score"] <= 0.08:
|
||||
lines.append("\n" + chunk["text"].strip() + f"〔来源{chunk['n']}〕")
|
||||
return "\n".join(lines).strip()
|
||||
|
||||
|
||||
def generate(question: str, chunks: list[dict]) -> tuple[str, str, str | None, bool]:
|
||||
low_confidence = not chunks or chunks[0]["score"] < LOW_SCORE
|
||||
if not chunks:
|
||||
return "知识库里还没有可以检索的内容。", "extractive", None, True
|
||||
|
||||
settings = llm_config()
|
||||
if settings is None:
|
||||
notice = "还没有配置大模型,回答直接摘自相关度最高的切片。在项目目录的 .env 中填入 OPENAI_API_KEY 后重启,即可生成连贯回答。"
|
||||
return extractive_answer(chunks, low_confidence), "extractive", notice, low_confidence
|
||||
|
||||
caution = ""
|
||||
if low_confidence:
|
||||
caution = "注意:最高相关度偏低。如果切片不能直接支持答案,请明确说知识库里没有这项信息。\n\n"
|
||||
user_prompt = f"{caution}知识切片如下:\n\n{build_context(chunks)}\n\n问题:{question}"
|
||||
try:
|
||||
answer = call_llm(settings, user_prompt)
|
||||
except Exception:
|
||||
logger.exception("大模型调用失败")
|
||||
notice = "大模型暂时不可用,已改为直接摘录检索到的切片。"
|
||||
return extractive_answer(chunks, low_confidence), "extractive", notice, low_confidence
|
||||
return answer, "llm", None, low_confidence
|
||||
|
||||
|
||||
def call_llm(settings: dict, user_prompt: str) -> str:
|
||||
response = httpx.post(
|
||||
f"{settings['base']}/chat/completions",
|
||||
headers={"Authorization": f"Bearer {settings['key']}"},
|
||||
json={
|
||||
"model": settings["model"],
|
||||
"temperature": 0.2,
|
||||
"max_tokens": 800,
|
||||
"messages": [
|
||||
{"role": "system", "content": SYSTEM_PROMPT},
|
||||
{"role": "user", "content": user_prompt},
|
||||
],
|
||||
},
|
||||
timeout=httpx.Timeout(60.0, connect=10.0),
|
||||
)
|
||||
response.raise_for_status()
|
||||
message = response.json()["choices"][0]["message"]
|
||||
content = message.get("content")
|
||||
if isinstance(content, list):
|
||||
content = "".join(
|
||||
part.get("text", "") if isinstance(part, dict) else str(part) for part in content
|
||||
)
|
||||
text = (content or "").strip()
|
||||
if not text:
|
||||
raise ValueError("模型返回了空回答")
|
||||
return text
|
||||
+215
@@ -0,0 +1,215 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI, File, HTTPException, Request, UploadFile
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.config import (
|
||||
CHUNK_OVERLAP,
|
||||
CHUNK_SIZE,
|
||||
DEFAULT_TOP_K,
|
||||
EMBEDDING_MODEL,
|
||||
STATIC_DIR,
|
||||
llm_config,
|
||||
)
|
||||
from app.embeddings import Embedder
|
||||
from app.generator import generate
|
||||
from app.store import KnowledgeBase
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s %(name)s %(message)s",
|
||||
)
|
||||
logger = logging.getLogger("learn_rag")
|
||||
|
||||
|
||||
class AskRequest(BaseModel):
|
||||
question: str = Field(min_length=1, max_length=2000)
|
||||
top_k: int = Field(default=DEFAULT_TOP_K, ge=1, le=8)
|
||||
|
||||
|
||||
class DocumentIn(BaseModel):
|
||||
title: str = Field(min_length=1, max_length=80)
|
||||
content: str = Field(min_length=1, max_length=200_000)
|
||||
|
||||
|
||||
def decode_text(raw: bytes) -> str:
|
||||
if len(raw) > 1_000_000:
|
||||
raise HTTPException(status_code=413, detail="文件超过 1MB")
|
||||
for encoding in ("utf-8-sig", "utf-8", "gb18030"):
|
||||
try:
|
||||
return raw.decode(encoding)
|
||||
except UnicodeDecodeError:
|
||||
continue
|
||||
raise HTTPException(status_code=400, detail="无法识别文件编码,请使用 UTF-8 或 GB18030")
|
||||
|
||||
|
||||
def clean_document(title: str, content: str) -> tuple[str, str]:
|
||||
title = " ".join(title.replace("\x00", "").split())
|
||||
content = content.replace("\x00", "").strip()
|
||||
if not title:
|
||||
raise HTTPException(status_code=400, detail="标题不能为空")
|
||||
if len(title) > 80:
|
||||
title = title[:80]
|
||||
if len(content) < 10:
|
||||
raise HTTPException(status_code=400, detail="正文至少需要 10 个字")
|
||||
return title, content
|
||||
|
||||
|
||||
def build_runtime() -> KnowledgeBase:
|
||||
embedder = Embedder()
|
||||
knowledge = KnowledgeBase(embedder)
|
||||
knowledge.startup()
|
||||
documents, chunks = knowledge.counts()
|
||||
logger.info("知识库就绪:%s 篇文档,%s 个切片", documents, chunks)
|
||||
return knowledge
|
||||
|
||||
|
||||
async def boot(application: FastAPI) -> None:
|
||||
if application.state.booting or application.state.ready:
|
||||
return
|
||||
application.state.booting = True
|
||||
application.state.error = None
|
||||
try:
|
||||
application.state.kb = await asyncio.to_thread(build_runtime)
|
||||
application.state.ready = True
|
||||
except Exception as exc:
|
||||
logger.exception("初始化失败")
|
||||
application.state.error = f"初始化失败:{exc}"
|
||||
finally:
|
||||
application.state.booting = False
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(application: FastAPI):
|
||||
application.state.ready = False
|
||||
application.state.booting = False
|
||||
application.state.error = None
|
||||
application.state.kb = None
|
||||
asyncio.create_task(boot(application))
|
||||
yield
|
||||
|
||||
|
||||
app = FastAPI(title="知迹", lifespan=lifespan)
|
||||
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
||||
|
||||
|
||||
def require_kb(request: Request) -> KnowledgeBase:
|
||||
if not request.app.state.ready or request.app.state.kb is None:
|
||||
detail = request.app.state.error or "嵌入模型还在准备,请稍后再试"
|
||||
raise HTTPException(status_code=503, detail=detail)
|
||||
return request.app.state.kb
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def index() -> FileResponse:
|
||||
return FileResponse(STATIC_DIR / "index.html")
|
||||
|
||||
|
||||
@app.get("/api/status")
|
||||
def status(request: Request) -> dict:
|
||||
knowledge = request.app.state.kb
|
||||
documents, chunks = knowledge.counts() if knowledge is not None else (0, 0)
|
||||
llm = llm_config()
|
||||
return {
|
||||
"ready": bool(request.app.state.ready),
|
||||
"booting": bool(request.app.state.booting),
|
||||
"error": request.app.state.error,
|
||||
"embedding_model": EMBEDDING_MODEL,
|
||||
"vector_db": "Chroma",
|
||||
"chunk_size": CHUNK_SIZE,
|
||||
"chunk_overlap": CHUNK_OVERLAP,
|
||||
"llm_ready": llm is not None,
|
||||
"llm_model": llm["model"] if llm else None,
|
||||
"documents": documents,
|
||||
"chunks": chunks,
|
||||
}
|
||||
|
||||
|
||||
@app.post("/api/boot")
|
||||
async def boot_again(request: Request) -> dict:
|
||||
if request.app.state.ready:
|
||||
return {"ok": True}
|
||||
asyncio.create_task(boot(request.app))
|
||||
return {"ok": True, "booting": True}
|
||||
|
||||
|
||||
@app.get("/api/documents")
|
||||
def list_documents(request: Request) -> dict:
|
||||
knowledge = request.app.state.kb
|
||||
if knowledge is None:
|
||||
return {"documents": []}
|
||||
return {"documents": knowledge.list_documents()}
|
||||
|
||||
|
||||
@app.post("/api/documents")
|
||||
def create_document(body: DocumentIn, request: Request) -> dict:
|
||||
knowledge = require_kb(request)
|
||||
title, content = clean_document(body.title, body.content)
|
||||
try:
|
||||
return knowledge.add(title, content)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@app.post("/api/documents/upload")
|
||||
async def upload_document(request: Request, file: UploadFile = File(...)) -> dict:
|
||||
knowledge = require_kb(request)
|
||||
raw = await file.read()
|
||||
content = decode_text(raw)
|
||||
filename = (file.filename or "未命名资料").rsplit("/", 1)[-1]
|
||||
title = filename.rsplit(".", 1)[0]
|
||||
title, content = clean_document(title, content)
|
||||
try:
|
||||
return knowledge.add(title, content)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
|
||||
|
||||
@app.delete("/api/documents/{doc_id}")
|
||||
def delete_document(doc_id: str, request: Request) -> dict:
|
||||
knowledge = require_kb(request)
|
||||
if not knowledge.delete(doc_id):
|
||||
raise HTTPException(status_code=404, detail="没有找到这篇资料")
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@app.post("/api/ask")
|
||||
async def ask(body: AskRequest, request: Request) -> dict:
|
||||
knowledge = require_kb(request)
|
||||
question = body.question.strip()
|
||||
if not question:
|
||||
raise HTTPException(status_code=400, detail="请输入问题")
|
||||
started = time.perf_counter()
|
||||
|
||||
def run() -> dict:
|
||||
chunks = knowledge.search(question, body.top_k)
|
||||
logger.info(
|
||||
"检索「%s」→ %s",
|
||||
question,
|
||||
[(chunk["title"], chunk["score"]) for chunk in chunks],
|
||||
)
|
||||
answer, mode, notice, low_confidence = generate(question, chunks)
|
||||
return {
|
||||
"question": question,
|
||||
"answer": answer,
|
||||
"mode": mode,
|
||||
"notice": notice,
|
||||
"low_confidence": low_confidence,
|
||||
"elapsed_ms": int((time.perf_counter() - started) * 1000),
|
||||
"chunks": chunks,
|
||||
}
|
||||
|
||||
return await asyncio.to_thread(run)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
port = int(os.getenv("PORT", "8000"))
|
||||
logger.info("知迹已启动:http://127.0.0.1:%s", port)
|
||||
uvicorn.run(app, host="127.0.0.1", port=port, log_level="info")
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
"""预置的虚构书店规章。首次启动、知识库为空时写入。"""
|
||||
|
||||
SAMPLES: list[dict[str, str]] = [
|
||||
{
|
||||
"title": "门店与营业时间",
|
||||
"content": """云栖书店位于杭州市西湖区北山街 128 号,出门即是白堤,步行到断桥大约八分钟。门店电话是 0571-8800-2218,邮箱是 hello@yunqi-books.example。
|
||||
|
||||
平日周一至周五的营业时间是上午 10:00 到晚上 21:30。周六、周日和法定节假日的营业时间是上午 9:30 到晚上 22:00。元旦当天以及春节除夕提前到 18:00 结束营业,正月初一全天闭店。
|
||||
|
||||
每周二上午 10:00 到 12:00 是闭架整理时段。这段时间不接待散客购书,只有松卡和柏卡会员可以入内取书、借阅和参加已经预约的活动。12:00 起恢复正常开放。
|
||||
|
||||
书店一共三层。一楼出售新书、人文社科和旅行读物,服务台也在一楼,负责会员、退换和停车券。二楼是文学、艺术、童书和绘本角。三楼是阅读区,以及可容纳 40 人的活动厅。地下一层是仓库,不对顾客开放。
|
||||
|
||||
雨天大堂提供一次性伞套。店内不接受大件行李寄存,折叠伞和手提包可以带入。""",
|
||||
},
|
||||
{
|
||||
"title": "会员等级与权益",
|
||||
"content": """云栖书店的会员分为竹卡、松卡、柏卡三级,都在一楼服务台办理,当天生效。
|
||||
|
||||
竹卡免费。消费按 1 元积 1 分,不足 1 元的部分不计分。积分满 200 分可以兑换一张 10 元购书券,兑换后扣掉相应积分。购书券自发放之日起 90 天内有效,过期作废,不能兑现。竹卡购书没有折扣,也不能在周二闭架整理时段进店。
|
||||
|
||||
松卡年费 128 元。持卡购书享受 9 折,特价区除外。松卡每个月可以免费参加一场书店自己举办的读书会,名额要提前在服务台预约。松卡可以在周二闭架整理时段入店。
|
||||
|
||||
柏卡年费 368 元。持卡购书享受 8.5 折,特价区除外。柏卡可以同时借阅 5 本店内藏书,借期 21 天,并且每次进入三楼阅读区时可以免费带一位同行友人。友人没有独立的座位延时资格。
|
||||
|
||||
升级按自然年累计实付金额计算,用掉折扣和购书券之后的金额才计入。当年实付满 800 元,竹卡可以升为松卡;当年实付满 2400 元,可以升为柏卡。升级时年费按剩余月份折算,也可以用超出的消费直接抵扣。积分跨年不清零。会员到期前 15 天,服务台会用短信提醒续费。""",
|
||||
},
|
||||
{
|
||||
"title": "购书退换与发票",
|
||||
"content": """未拆封、无折痕、无污渍、不影响二次销售的图书,自购买之日起 7 天内可以凭购物小票退货或换货。已经盖了云栖书店店章的书、有明显折角或笔记的书,以及特价区图书,不支持退换。
|
||||
|
||||
期刊、报纸、文创商品和饮品一经售出,不退不换。如果图书有缺页、倒装、严重破损等质量问题,不受 7 天限制,凭小票在 30 天内免费更换同一书号的新书。没有同款库存时,按原支付方式退款。
|
||||
|
||||
退货走原支付路径。现金支付的订单退成购书余额,不退现金。购书券抵扣的部分退回原券,沿用原来的有效期,不重新计算 90 天。
|
||||
|
||||
发票可以在购买当天到服务台开具,也可以在成交后 30 天内凭小票补开电子发票。需要税号时请当场提供。书店不把图书发票改成其他报销品类。""",
|
||||
},
|
||||
{
|
||||
"title": "藏书借阅",
|
||||
"content": """只有柏卡会员可以借阅店内藏书。店内藏书和正在出售的新书不是同一批,书脊贴有「藏」字蓝标,放在二楼西墙。可以外借的书,书目卡上会写明「可借」。
|
||||
|
||||
每次最多同时持有 5 本。单本借期 21 天,到期前可以续借 1 次,续借再延长 14 天。续借要把书带到服务台,不能打电话续借。
|
||||
|
||||
借阅押金是该书定价的 30%,四舍五入到元。还书检查无损后,押金退回原支付方式。逾期滞纳金是每本每天 1 元,从逾期的次日开始计算。逾期超过 30 天视为买断,按图书定价扣款,押金可以冲抵一部分,不够的需要补齐。买断之后的书不必归还。
|
||||
|
||||
外借图书不能批注、折角,也不能撕掉书标。丢失或严重污损时,按定价的 1.2 倍赔偿,押金优先抵扣。赔偿完成后,原书如果后来找到,书店不再收回,也不退还赔偿金。
|
||||
|
||||
借出的藏书仍然属于书店,不能转借给非会员。转借造成的污损由办卡人承担责任。""",
|
||||
},
|
||||
{
|
||||
"title": "阅读区与餐饮",
|
||||
"content": """三楼阅读区的入场条件是:当天在店内消费满 38 元,或者持有松卡、柏卡。竹卡和没有办卡的读者,如果没有达到消费金额,不能只为了占座进入阅读区。柏卡可以免费带一位同行友人,但友人不能单独延长座位时间。
|
||||
|
||||
普通日子每个座位限时 3 小时。周六、周日和法定节假日的 13:00 到 17:00 是高峰,限时改为 2 小时。服务台不会到点强制清场,不过有人排队时,超时的座位需要让出来。
|
||||
|
||||
阅读区供应手冲咖啡、桂花乌龙和巴斯克蛋糕。可以自带有盖的杯装饮品。不可以携带外来热食、盒饭、炸物,以及气味浓重的食物。一楼门口有饮水机,杯子需要自备,或者购买店内饮品。
|
||||
|
||||
阅读区可以使用笔记本和平板电脑,请调成静音。打电话请到楼梯间。儿童进入阅读区需要成人陪同。绘本角在二楼东侧,不属于阅读区,不设 3 小时限制;周末请不要长时间占用绘本角的矮桌。
|
||||
|
||||
每周六下午 15:00 有一小时亲子共读,地点在二楼绘本角,不需要会员,也不收费。需要提前在一楼服务台预约,每次限额 12 组家庭。迟到超过 10 分钟,名额让给现场等候的家庭。""",
|
||||
},
|
||||
{
|
||||
"title": "活动停车与其他服务",
|
||||
"content": """2026 年 10 月的读书会主题是「河流与城市」,嘉宾是作家林晚晴。时间是 10 月 18 日周六晚上 19:30,地点在三楼活动厅。松卡和柏卡会员免费,但要提前预约。非会员票价 49 元,包含一杯桂花乌龙。活动大约 90 分钟,19:20 开始签到,开始 15 分钟后停止入场。
|
||||
|
||||
11 月 2 日周日下午 13:00 到 17:00,书店门口举办旧书交换市集。读者可以带不超过 5 本品相良好的旧书来交换,不收摊位费,也不保证一定能换到想要的书。教材、破损书和期刊不接受。
|
||||
|
||||
书店不提供打印和复印。需要复印的读者,可以步行前往附近的浙江图书馆。店内无线网络名称是 Yunqi-Guest,密码写在服务台立牌上,只供应阅读区,不保证视频会议的带宽。
|
||||
|
||||
停车可以使用北山街地下停车场。当天在店内消费满 68 元,可以在一楼服务台领取 2 小时停车券。每车每天限领 1 张,需要出示停车票根。书店门口没有非机动车棚,共享单车不要停在台阶上。""",
|
||||
},
|
||||
]
|
||||
+226
@@ -0,0 +1,226 @@
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import threading
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
import chromadb
|
||||
from chromadb.config import Settings
|
||||
|
||||
from app.chunker import chunk_text
|
||||
from app.config import COLLECTION_NAME, DATA_DIR, EMBEDDING_MODEL
|
||||
from app.embeddings import Embedder
|
||||
from app.seed import SAMPLES
|
||||
|
||||
logger = logging.getLogger("learn_rag")
|
||||
|
||||
|
||||
class DisabledEmbedding:
|
||||
"""占位嵌入函数。向量全部由 fastembed 预先算好,避免 Chroma 再下载一套模型。"""
|
||||
|
||||
@staticmethod
|
||||
def name() -> str:
|
||||
return "precomputed"
|
||||
|
||||
def __call__(self, input):
|
||||
raise RuntimeError("向量由 fastembed 预先计算")
|
||||
|
||||
def embed_documents(self, input):
|
||||
raise RuntimeError("向量由 fastembed 预先计算")
|
||||
|
||||
def embed_query(self, input):
|
||||
raise RuntimeError("向量由 fastembed 预先计算")
|
||||
|
||||
|
||||
def to_score(distance: float) -> float:
|
||||
if distance <= 0:
|
||||
return 1.0
|
||||
if distance <= 1:
|
||||
return 1 - distance
|
||||
return 1 / (1 + distance)
|
||||
|
||||
|
||||
class KnowledgeBase:
|
||||
def __init__(self, embedder: Embedder) -> None:
|
||||
self.embedder = embedder
|
||||
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||||
self.path = DATA_DIR / "documents.json"
|
||||
self._lock = threading.Lock()
|
||||
self.client = chromadb.PersistentClient(
|
||||
path=str(DATA_DIR / "chroma"),
|
||||
settings=Settings(anonymized_telemetry=False),
|
||||
)
|
||||
self.collection = self._open_collection()
|
||||
self.docs: list[dict] = []
|
||||
|
||||
def _open_collection(self):
|
||||
return self.client.get_or_create_collection(
|
||||
name=COLLECTION_NAME,
|
||||
embedding_function=DisabledEmbedding(),
|
||||
metadata={"hnsw:space": "cosine"},
|
||||
)
|
||||
|
||||
def _reset_collection(self) -> None:
|
||||
try:
|
||||
self.client.delete_collection(COLLECTION_NAME)
|
||||
except Exception:
|
||||
logger.info("向量集合尚不存在,直接创建")
|
||||
self.collection = self._open_collection()
|
||||
|
||||
def startup(self) -> None:
|
||||
needs_seed = not self.path.exists()
|
||||
payload = {"model": EMBEDDING_MODEL, "documents": []}
|
||||
if self.path.exists():
|
||||
payload = json.loads(self.path.read_text(encoding="utf-8"))
|
||||
self.docs = payload.get("documents", [])
|
||||
if needs_seed:
|
||||
logger.info("写入示例知识库:云栖书店")
|
||||
self._reset_collection()
|
||||
for sample in SAMPLES:
|
||||
self._index(sample["title"], sample["content"])
|
||||
self._save()
|
||||
return
|
||||
if payload.get("model") != EMBEDDING_MODEL:
|
||||
logger.info("嵌入模型已更换,重新构建向量库")
|
||||
previous = list(self.docs)
|
||||
self.docs = []
|
||||
self._reset_collection()
|
||||
for doc in previous:
|
||||
self._index(doc["title"], doc["content"], doc["id"], doc["created_at"])
|
||||
self._save()
|
||||
return
|
||||
self._reconcile()
|
||||
|
||||
def _save(self) -> None:
|
||||
payload = {"model": EMBEDDING_MODEL, "documents": self.docs}
|
||||
temporary = self.path.with_suffix(".json.tmp")
|
||||
temporary.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
temporary.replace(self.path)
|
||||
|
||||
def _summary(self, doc: dict) -> dict:
|
||||
preview = re.sub(r"\s+", " ", doc["content"]).strip()[:80]
|
||||
return {
|
||||
"id": doc["id"],
|
||||
"title": doc["title"],
|
||||
"chunk_count": doc["chunk_count"],
|
||||
"created_at": doc["created_at"],
|
||||
"preview": preview,
|
||||
}
|
||||
|
||||
def _index(
|
||||
self,
|
||||
title: str,
|
||||
content: str,
|
||||
doc_id: str | None = None,
|
||||
created_at: str | None = None,
|
||||
) -> dict:
|
||||
chunks = chunk_text(content)
|
||||
if not chunks:
|
||||
raise ValueError("正文无法切分")
|
||||
doc_id = doc_id or uuid.uuid4().hex
|
||||
created_at = created_at or datetime.now(timezone.utc).isoformat(timespec="seconds")
|
||||
vectors = self.embedder.embed_documents(chunks)
|
||||
ids = [f"{doc_id}:{index}" for index in range(len(chunks))]
|
||||
metadatas = [
|
||||
{
|
||||
"doc_id": doc_id,
|
||||
"title": title,
|
||||
"chunk_index": index,
|
||||
"chunk_total": len(chunks),
|
||||
}
|
||||
for index in range(len(chunks))
|
||||
]
|
||||
self.collection.add(
|
||||
ids=ids,
|
||||
documents=chunks,
|
||||
embeddings=vectors,
|
||||
metadatas=metadatas,
|
||||
)
|
||||
record = {
|
||||
"id": doc_id,
|
||||
"title": title,
|
||||
"content": content,
|
||||
"created_at": created_at,
|
||||
"chunk_count": len(chunks),
|
||||
}
|
||||
self.docs.append(record)
|
||||
return record
|
||||
|
||||
def _reconcile(self) -> None:
|
||||
stored = self.collection.get(include=["metadatas"])
|
||||
present = {
|
||||
meta["doc_id"]
|
||||
for meta in (stored.get("metadatas") or [])
|
||||
if meta and meta.get("doc_id")
|
||||
}
|
||||
wanted = {doc["id"] for doc in self.docs}
|
||||
for doc_id in present - wanted:
|
||||
self.collection.delete(where={"doc_id": doc_id})
|
||||
for doc in self.docs:
|
||||
if doc["id"] not in present:
|
||||
logger.info("重新索引《%s》", doc["title"])
|
||||
self.docs = [item for item in self.docs if item["id"] != doc["id"]]
|
||||
self._index(doc["title"], doc["content"], doc["id"], doc["created_at"])
|
||||
self._save()
|
||||
|
||||
def list_documents(self) -> list[dict]:
|
||||
with self._lock:
|
||||
return [self._summary(doc) for doc in self.docs]
|
||||
|
||||
def counts(self) -> tuple[int, int]:
|
||||
with self._lock:
|
||||
return len(self.docs), int(self.collection.count())
|
||||
|
||||
def add(self, title: str, content: str) -> dict:
|
||||
with self._lock:
|
||||
record = self._index(title, content)
|
||||
try:
|
||||
self._save()
|
||||
except Exception:
|
||||
self.docs.pop()
|
||||
self.collection.delete(where={"doc_id": record["id"]})
|
||||
raise
|
||||
logger.info("新增《%s》,%s 个切片", title, record["chunk_count"])
|
||||
return self._summary(record)
|
||||
|
||||
def delete(self, doc_id: str) -> bool:
|
||||
with self._lock:
|
||||
if not any(doc["id"] == doc_id for doc in self.docs):
|
||||
return False
|
||||
self.collection.delete(where={"doc_id": doc_id})
|
||||
self.docs = [doc for doc in self.docs if doc["id"] != doc_id]
|
||||
self._save()
|
||||
return True
|
||||
|
||||
def search(self, question: str, top_k: int) -> list[dict]:
|
||||
with self._lock:
|
||||
count = int(self.collection.count())
|
||||
if count == 0:
|
||||
return []
|
||||
vector = self.embedder.embed_query(question)
|
||||
found = self.collection.query(
|
||||
query_embeddings=[vector],
|
||||
n_results=min(top_k, count),
|
||||
include=["documents", "metadatas", "distances"],
|
||||
)
|
||||
documents = (found.get("documents") or [[]])[0] or []
|
||||
metadatas = (found.get("metadatas") or [[]])[0] or []
|
||||
distances = (found.get("distances") or [[]])[0] or []
|
||||
chunks = []
|
||||
for text, meta, distance in zip(documents, metadatas, distances):
|
||||
meta = meta or {}
|
||||
chunks.append(
|
||||
{
|
||||
"id": f"{meta.get('doc_id', '')}:{meta.get('chunk_index', 0)}",
|
||||
"title": meta.get("title") or "未命名资料",
|
||||
"text": text or "",
|
||||
"score": round(to_score(float(distance)), 4),
|
||||
"chunk_index": int(meta.get("chunk_index") or 0) + 1,
|
||||
"chunk_total": int(meta.get("chunk_total") or 1),
|
||||
}
|
||||
)
|
||||
chunks.sort(key=lambda item: item["score"], reverse=True)
|
||||
for number, chunk in enumerate(chunks, start=1):
|
||||
chunk["n"] = number
|
||||
return chunks
|
||||
@@ -0,0 +1,16 @@
|
||||
[project]
|
||||
name = "learn-rag"
|
||||
version = "0.1.0"
|
||||
description = "基于 embedding 与 Chroma 的本地 RAG 问答"
|
||||
requires-python = ">=3.11,<3.14"
|
||||
dependencies = [
|
||||
"fastapi>=0.115",
|
||||
"uvicorn>=0.32",
|
||||
"chromadb>=0.5.23",
|
||||
"fastembed>=0.4",
|
||||
"httpx>=0.27",
|
||||
"python-multipart>=0.0.12",
|
||||
]
|
||||
|
||||
[tool.uv]
|
||||
package = false
|
||||
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")"
|
||||
export HF_ENDPOINT="${HF_ENDPOINT:-https://hf-mirror.com}"
|
||||
if [[ ! -x .venv/bin/python ]]; then
|
||||
uv sync --python 3.13 --index-url https://pypi.tuna.tsinghua.edu.cn/simple
|
||||
fi
|
||||
exec uv run --python 3.13 python -m app
|
||||
+399
@@ -0,0 +1,399 @@
|
||||
const EXAMPLES = [
|
||||
"周末几点开门,几点关门?",
|
||||
"柏卡会员年费多少,能借几本书?",
|
||||
"买的书几天内可以退?",
|
||||
"阅读区可以带外面的食物吗?",
|
||||
"十月读书会的主题和票价是什么?",
|
||||
];
|
||||
|
||||
const state = {
|
||||
ready: false,
|
||||
failed: false,
|
||||
sending: false,
|
||||
documents: [],
|
||||
turns: [],
|
||||
};
|
||||
|
||||
const messages = document.querySelector("#messages");
|
||||
const questionInput = document.querySelector("#question");
|
||||
const sendButton = document.querySelector("#send");
|
||||
const topK = document.querySelector("#top-k");
|
||||
const docForm = document.querySelector("#doc-form");
|
||||
const docTitle = document.querySelector("#doc-title");
|
||||
const docContent = document.querySelector("#doc-content");
|
||||
const saveDoc = document.querySelector("#save-doc");
|
||||
const pickFile = document.querySelector("#pick-file");
|
||||
const fileInput = document.querySelector("#file");
|
||||
const docList = document.querySelector("#doc-list");
|
||||
const pills = document.querySelector("#pills");
|
||||
const facts = document.querySelector("#facts");
|
||||
const libraryMeta = document.querySelector("#library-meta");
|
||||
|
||||
messages.addEventListener("click", (event) => {
|
||||
const chip = event.target.closest("[data-question]");
|
||||
if (chip) {
|
||||
ask(chip.dataset.question);
|
||||
return;
|
||||
}
|
||||
const cite = event.target.closest(".cite");
|
||||
if (!cite) return;
|
||||
const turn = cite.closest(".turn");
|
||||
turn.querySelectorAll(".cite").forEach((node) => node.classList.remove("is-on"));
|
||||
turn.querySelectorAll(".slice").forEach((node) => node.classList.remove("is-active"));
|
||||
cite.classList.add("is-on");
|
||||
const slice = turn.querySelector(`.slice[data-n="${cite.dataset.n}"]`);
|
||||
if (slice) {
|
||||
slice.classList.add("is-active");
|
||||
slice.scrollIntoView({ behavior: "smooth", block: "nearest" });
|
||||
}
|
||||
});
|
||||
|
||||
docList.addEventListener("click", (event) => {
|
||||
const button = event.target.closest("[data-delete]");
|
||||
if (!button) return;
|
||||
removeDocument(button.dataset.delete, button.dataset.title);
|
||||
});
|
||||
|
||||
document.querySelector("#composer").addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
ask(questionInput.value);
|
||||
});
|
||||
|
||||
questionInput.addEventListener("keydown", (event) => {
|
||||
if (event.key === "Enter" && !event.shiftKey) {
|
||||
event.preventDefault();
|
||||
document.querySelector("#composer").requestSubmit();
|
||||
}
|
||||
});
|
||||
|
||||
docForm.addEventListener("submit", async (event) => {
|
||||
event.preventDefault();
|
||||
await createDocument(docTitle.value, docContent.value);
|
||||
});
|
||||
|
||||
pickFile.addEventListener("click", () => fileInput.click());
|
||||
fileInput.addEventListener("change", async () => {
|
||||
const file = fileInput.files && fileInput.files[0];
|
||||
fileInput.value = "";
|
||||
if (!file) return;
|
||||
const body = new FormData();
|
||||
body.append("file", file);
|
||||
try {
|
||||
const response = await fetch("/api/documents/upload", { method: "POST", body });
|
||||
if (!response.ok) throw new Error(await readError(response));
|
||||
const doc = await response.json();
|
||||
toast(`已把《${doc.title}》切成 ${doc.chunk_count} 片并写入向量库`);
|
||||
await refreshDocs();
|
||||
await refreshStatus();
|
||||
} catch (error) {
|
||||
toast(error.message);
|
||||
}
|
||||
});
|
||||
|
||||
function el(tag, className, text) {
|
||||
const node = document.createElement(tag);
|
||||
if (className) node.className = className;
|
||||
if (text != null) node.textContent = text;
|
||||
return node;
|
||||
}
|
||||
|
||||
function toast(message) {
|
||||
const node = document.querySelector("#toast");
|
||||
node.textContent = message;
|
||||
node.classList.add("is-on");
|
||||
clearTimeout(toast.timer);
|
||||
toast.timer = setTimeout(() => node.classList.remove("is-on"), 3400);
|
||||
}
|
||||
|
||||
async function readError(response) {
|
||||
try {
|
||||
const data = await response.json();
|
||||
if (typeof data.detail === "string") return data.detail;
|
||||
} catch {
|
||||
/* 响应体不是 JSON */
|
||||
}
|
||||
return `请求失败(${response.status})`;
|
||||
}
|
||||
|
||||
function setBusy(busy) {
|
||||
state.sending = busy;
|
||||
const locked = busy || !state.ready;
|
||||
questionInput.disabled = locked;
|
||||
sendButton.disabled = locked;
|
||||
topK.disabled = locked;
|
||||
docTitle.disabled = !state.ready;
|
||||
docContent.disabled = !state.ready;
|
||||
saveDoc.disabled = !state.ready;
|
||||
pickFile.disabled = !state.ready;
|
||||
sendButton.textContent = busy ? "检索中" : "提问";
|
||||
}
|
||||
|
||||
function renderStatus(data) {
|
||||
pills.replaceChildren();
|
||||
const model = el("span", "pill");
|
||||
if (data.error) {
|
||||
model.classList.add("is-bad");
|
||||
model.textContent = "模型准备失败";
|
||||
} else if (data.ready) {
|
||||
model.classList.add("is-ok");
|
||||
model.textContent = data.embedding_model;
|
||||
} else {
|
||||
model.classList.add("is-wait");
|
||||
model.textContent = "正在下载并加载嵌入模型…";
|
||||
}
|
||||
const store = el("span", "pill");
|
||||
store.append(`${data.vector_db} · `);
|
||||
const strong = el("strong", null, `${data.documents} 篇 / ${data.chunks} 片`);
|
||||
store.append(strong);
|
||||
const llm = el("span", "pill");
|
||||
if (data.llm_ready) {
|
||||
llm.classList.add("is-ok");
|
||||
llm.textContent = `生成模型 ${data.llm_model}`;
|
||||
} else {
|
||||
llm.textContent = "未配置大模型,先摘录原文";
|
||||
}
|
||||
pills.append(model, store, llm);
|
||||
if (data.error) {
|
||||
const retry = el("button", "chip", "重试");
|
||||
retry.type = "button";
|
||||
retry.addEventListener("click", async () => {
|
||||
state.failed = false;
|
||||
await fetch("/api/boot", { method: "POST" });
|
||||
poll();
|
||||
});
|
||||
pills.append(retry);
|
||||
}
|
||||
|
||||
facts.replaceChildren();
|
||||
const rows = [
|
||||
["嵌入", data.embedding_model],
|
||||
["向量库", data.vector_db],
|
||||
["切片", `${data.chunk_size} 字,重叠 ${data.chunk_overlap} 字`],
|
||||
];
|
||||
for (const [key, value] of rows) {
|
||||
facts.append(el("dt", null, key), el("dd", null, value));
|
||||
}
|
||||
libraryMeta.textContent = data.ready
|
||||
? "示例资料是虚构的云栖书店。可以继续追加你自己的文本,提问时会一起检索。"
|
||||
: "第一次启动会下载大约一百兆的中文嵌入模型,页面可以先开着。";
|
||||
}
|
||||
|
||||
function renderDocs() {
|
||||
docList.replaceChildren();
|
||||
if (!state.documents.length) {
|
||||
docList.append(el("p", "empty-docs quiet", "知识库还是空的。"));
|
||||
return;
|
||||
}
|
||||
for (const doc of state.documents) {
|
||||
const card = el("article", "doc");
|
||||
const title = el("h3", "doc-title", doc.title);
|
||||
const button = el("button", "icon-btn", "删");
|
||||
button.type = "button";
|
||||
button.dataset.delete = doc.id;
|
||||
button.dataset.title = doc.title;
|
||||
button.setAttribute("aria-label", `删除《${doc.title}》`);
|
||||
const preview = el("p", "doc-preview", `${doc.chunk_count} 个切片 · ${doc.preview}`);
|
||||
card.append(title, button, preview);
|
||||
docList.append(card);
|
||||
}
|
||||
}
|
||||
|
||||
function appendRichText(container, text) {
|
||||
const pattern = /〔来源(\d+)〕/g;
|
||||
let cursor = 0;
|
||||
for (const match of String(text || "").matchAll(pattern)) {
|
||||
const index = match.index ?? 0;
|
||||
if (index > cursor) container.append(text.slice(cursor, index));
|
||||
const cite = el("button", "cite", `来源 ${match[1]}`);
|
||||
cite.type = "button";
|
||||
cite.dataset.n = match[1];
|
||||
cite.title = `定位到第 ${match[1]} 条切片`;
|
||||
container.append(cite);
|
||||
cursor = index + match[0].length;
|
||||
}
|
||||
if (cursor < text.length) container.append(text.slice(cursor));
|
||||
}
|
||||
|
||||
function renderSlice(chunk) {
|
||||
const item = el("li", "slice");
|
||||
item.dataset.n = String(chunk.n);
|
||||
const top = el("div", "slice-top");
|
||||
top.append(el("span", "slice-no", `来源 ${chunk.n}`));
|
||||
const bar = el("div", "bar");
|
||||
const fill = document.createElement("span");
|
||||
fill.style.width = `${Math.max(6, Math.round(chunk.score * 100))}%`;
|
||||
bar.append(fill);
|
||||
top.append(bar, el("span", "slice-no", `${Math.round(chunk.score * 100)}%`));
|
||||
const title = el("p", "slice-title", `《${chunk.title}》 · 第 ${chunk.chunk_index}/${chunk.chunk_total} 片`);
|
||||
const body = el("p", "slice-text", chunk.text);
|
||||
item.append(top, title, body);
|
||||
return item;
|
||||
}
|
||||
|
||||
function renderTurn(turn) {
|
||||
const article = el("article", "turn");
|
||||
const question = el("div", "question");
|
||||
question.append(el("span", "label", "提问"), el("p", null, turn.question));
|
||||
const row = el("div", "answer-row");
|
||||
const answer = el("div", "answer");
|
||||
answer.append(el("span", "label", "回答"));
|
||||
if (turn.pending) {
|
||||
answer.append(el("div", "pending"), el("div", "pending short"));
|
||||
} else if (turn.error) {
|
||||
answer.append(el("p", "warn", turn.error));
|
||||
} else {
|
||||
const body = el("div", "answer-body");
|
||||
appendRichText(body, turn.answer);
|
||||
answer.append(body);
|
||||
const foot = el("div", "answer-foot");
|
||||
const mode = el("span", turn.mode === "llm" ? "tag" : "tag is-extract", turn.mode === "llm" ? "模型生成" : "原文摘录");
|
||||
foot.append(mode, el("span", null, `${turn.chunks.length} 条切片 · ${formatElapsed(turn.elapsed_ms)}`));
|
||||
answer.append(foot);
|
||||
if (turn.notice) answer.append(el("p", "quiet", turn.notice));
|
||||
}
|
||||
|
||||
const slices = el("aside", "slices");
|
||||
const head = el("div", "slices-head");
|
||||
head.append(el("h3", null, "检索到的知识切片"), el("span", "quiet", turn.pending ? "检索中" : `${(turn.chunks || []).length} 条`));
|
||||
slices.append(head);
|
||||
if (turn.pending) {
|
||||
slices.append(el("div", "pending"), el("div", "pending"));
|
||||
} else if (turn.chunks && turn.chunks.length) {
|
||||
if (turn.low_confidence) slices.append(el("p", "warn", "最高相关度偏低,请核对原文后再采信。"));
|
||||
const list = el("ol", "slice-list");
|
||||
turn.chunks.forEach((chunk) => list.append(renderSlice(chunk)));
|
||||
slices.append(list);
|
||||
} else if (!turn.error) {
|
||||
slices.append(el("p", "quiet", "这次没有检索到切片。"));
|
||||
}
|
||||
row.append(answer, slices);
|
||||
article.append(question, row);
|
||||
return article;
|
||||
}
|
||||
|
||||
function renderThread() {
|
||||
messages.replaceChildren();
|
||||
if (!state.turns.length) {
|
||||
const empty = el("div", "empty");
|
||||
empty.append(
|
||||
el("h2", null, "先问一句,看证据从哪来"),
|
||||
el("p", null, "预置了一家虚构书店的规章。回答出现在左侧,右侧是向量库检索到的原文切片。"),
|
||||
);
|
||||
const chips = el("div", "chips");
|
||||
for (const sample of EXAMPLES) {
|
||||
const button = el("button", "chip", sample);
|
||||
button.type = "button";
|
||||
button.dataset.question = sample;
|
||||
button.disabled = !state.ready || state.sending;
|
||||
chips.append(button);
|
||||
}
|
||||
empty.append(chips);
|
||||
messages.append(empty);
|
||||
return;
|
||||
}
|
||||
state.turns.forEach((turn) => messages.append(renderTurn(turn)));
|
||||
messages.scrollTop = messages.scrollHeight;
|
||||
}
|
||||
|
||||
function formatElapsed(ms) {
|
||||
if (ms < 1000) return `${ms} ms`;
|
||||
return `${(ms / 1000).toFixed(1)} s`;
|
||||
}
|
||||
|
||||
async function refreshStatus() {
|
||||
const response = await fetch("/api/status");
|
||||
if (!response.ok) throw new Error(await readError(response));
|
||||
const data = await response.json();
|
||||
state.ready = Boolean(data.ready);
|
||||
state.failed = Boolean(data.error);
|
||||
renderStatus(data);
|
||||
setBusy(state.sending);
|
||||
return data;
|
||||
}
|
||||
|
||||
async function refreshDocs() {
|
||||
const response = await fetch("/api/documents");
|
||||
if (!response.ok) throw new Error(await readError(response));
|
||||
const data = await response.json();
|
||||
state.documents = data.documents;
|
||||
renderDocs();
|
||||
}
|
||||
|
||||
async function poll() {
|
||||
try {
|
||||
const data = await refreshStatus();
|
||||
if (data.ready) {
|
||||
await refreshDocs();
|
||||
renderThread();
|
||||
questionInput.focus();
|
||||
return;
|
||||
}
|
||||
if (!data.error) setTimeout(poll, 1000);
|
||||
else renderThread();
|
||||
} catch {
|
||||
pills.replaceChildren(el("span", "pill is-bad", "暂时连不上问答服务"));
|
||||
setTimeout(poll, 1500);
|
||||
}
|
||||
}
|
||||
|
||||
async function ask(raw) {
|
||||
const question = (raw || "").trim();
|
||||
if (!question || state.sending || !state.ready) return;
|
||||
questionInput.value = "";
|
||||
const turn = { id: crypto.randomUUID(), question, pending: true, chunks: [] };
|
||||
state.turns.push(turn);
|
||||
renderThread();
|
||||
setBusy(true);
|
||||
try {
|
||||
const response = await fetch("/api/ask", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ question, top_k: Number(topK.value) }),
|
||||
});
|
||||
if (!response.ok) throw new Error(await readError(response));
|
||||
Object.assign(turn, await response.json(), { pending: false, error: null });
|
||||
} catch (error) {
|
||||
turn.pending = false;
|
||||
turn.error = error.message;
|
||||
turn.chunks = [];
|
||||
} finally {
|
||||
setBusy(false);
|
||||
renderThread();
|
||||
}
|
||||
}
|
||||
|
||||
async function createDocument(title, content) {
|
||||
if (!state.ready) return;
|
||||
try {
|
||||
const response = await fetch("/api/documents", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ title: title.trim(), content }),
|
||||
});
|
||||
if (!response.ok) throw new Error(await readError(response));
|
||||
const doc = await response.json();
|
||||
docTitle.value = "";
|
||||
docContent.value = "";
|
||||
toast(`已把《${doc.title}》切成 ${doc.chunk_count} 片并写入向量库`);
|
||||
await refreshDocs();
|
||||
await refreshStatus();
|
||||
} catch (error) {
|
||||
toast(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function removeDocument(id, title) {
|
||||
if (!confirm(`从向量库删除《${title}》?`)) return;
|
||||
try {
|
||||
const response = await fetch(`/api/documents/${id}`, { method: "DELETE" });
|
||||
if (!response.ok) throw new Error(await readError(response));
|
||||
toast(`已删除《${title}》`);
|
||||
await refreshDocs();
|
||||
await refreshStatus();
|
||||
} catch (error) {
|
||||
toast(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
renderThread();
|
||||
poll();
|
||||
@@ -0,0 +1,78 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>知迹 · 检索问答</title>
|
||||
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Crect fill='%231f6a52' width='32' height='32' rx='6'/%3E%3Cpath d='M8 9.5h7.2c1.6 0 2.8 1 2.8 2.6S16.8 15 15.2 15H8V9.5zm0 7h7.6c1.8 0 3.1 1.1 3.1 2.8S17.4 22 15.6 22H8v-5.5z' fill='%23f6f1e6'/%3E%3C/svg%3E" />
|
||||
<link rel="stylesheet" href="/static/styles.css?v=1" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="app">
|
||||
<header class="top">
|
||||
<div class="brand">
|
||||
<span class="mark" aria-hidden="true">知</span>
|
||||
<div>
|
||||
<p class="kicker">Learn · RAG</p>
|
||||
<h1>知迹</h1>
|
||||
</div>
|
||||
</div>
|
||||
<p class="lede">提问之后,回答旁边留下这次真正检索到的知识切片。</p>
|
||||
<div class="pills" id="pills" aria-live="polite">
|
||||
<span class="pill" id="pill-model">正在准备嵌入模型…</span>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="workspace">
|
||||
<section class="chat">
|
||||
<div id="messages" class="messages"></div>
|
||||
<form id="composer" class="composer">
|
||||
<label class="sr" for="question">问题</label>
|
||||
<textarea id="question" rows="2" maxlength="2000" placeholder="问问云栖书店,例如:柏卡会员能借几本书?" disabled></textarea>
|
||||
<div class="composer-bar">
|
||||
<label class="topk">
|
||||
切片数
|
||||
<select id="top-k" disabled>
|
||||
<option value="3">3</option>
|
||||
<option value="4" selected>4</option>
|
||||
<option value="6">6</option>
|
||||
</select>
|
||||
</label>
|
||||
<p class="hint">Enter 发送,Shift+Enter 换行</p>
|
||||
<button id="send" class="send" type="submit" disabled>提问</button>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
|
||||
<aside class="library">
|
||||
<div class="library-head">
|
||||
<div>
|
||||
<p class="kicker">知识库</p>
|
||||
<h2>写入资料,再拿去检索</h2>
|
||||
</div>
|
||||
<p id="library-meta" class="quiet">嵌入模型与向量库准备好之后,可以加入你自己的文本。</p>
|
||||
<dl class="facts" id="facts"></dl>
|
||||
</div>
|
||||
<form id="doc-form" class="doc-form">
|
||||
<label>
|
||||
标题
|
||||
<input id="doc-title" name="title" maxlength="80" placeholder="例如:退换货规则" disabled />
|
||||
</label>
|
||||
<label>
|
||||
正文
|
||||
<textarea id="doc-content" name="content" rows="6" placeholder="贴入一篇说明、规章或笔记。系统会切成小段,计算向量,存进 Chroma。" disabled></textarea>
|
||||
</label>
|
||||
<div class="doc-actions">
|
||||
<button id="save-doc" type="submit" class="ghost" disabled>写入向量库</button>
|
||||
<button id="pick-file" type="button" class="textish" disabled>导入 txt / md</button>
|
||||
<input id="file" type="file" accept=".txt,.md,.markdown,text/plain" hidden />
|
||||
</div>
|
||||
</form>
|
||||
<div id="doc-list" class="doc-list"></div>
|
||||
</aside>
|
||||
</div>
|
||||
</div>
|
||||
<div id="toast" class="toast" role="status"></div>
|
||||
<script src="/static/app.js?v=1"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,368 @@
|
||||
:root {
|
||||
--bg: #efe7d8;
|
||||
--ink: #221c16;
|
||||
--muted: #6f655b;
|
||||
--line: #e0d3bf;
|
||||
--paper: #fffaf2;
|
||||
--paper-2: #f6efe3;
|
||||
--green: #1d684f;
|
||||
--green-deep: #144a38;
|
||||
--green-soft: #e4f3ec;
|
||||
--cinnabar: #b84324;
|
||||
--gold: #8d6232;
|
||||
--shadow: 0 16px 40px rgba(68, 42, 16, 0.08);
|
||||
--serif: "Songti SC", "STSong", "Noto Serif SC", "Times New Roman", serif;
|
||||
--sans: "PingFang SC", "Hiragino Sans GB", "Avenir Next", "Segoe UI", sans-serif;
|
||||
--radius: 18px;
|
||||
}
|
||||
|
||||
* { box-sizing: border-box; }
|
||||
html, body { height: 100%; }
|
||||
body {
|
||||
margin: 0;
|
||||
color: var(--ink);
|
||||
font-family: var(--sans);
|
||||
background:
|
||||
radial-gradient(900px 420px at -10% -10%, rgba(196, 112, 52, 0.18), transparent 55%),
|
||||
radial-gradient(800px 380px at 110% -20%, rgba(29, 104, 79, 0.16), transparent 50%),
|
||||
linear-gradient(180deg, #f4eee4 0%, #e9e1d2 100%);
|
||||
}
|
||||
button, input, textarea, select { font: inherit; color: inherit; }
|
||||
button { cursor: pointer; }
|
||||
h1, h2, h3, p { margin: 0; }
|
||||
::selection { background: #d5eee3; }
|
||||
|
||||
.app {
|
||||
width: min(1240px, calc(100% - 28px));
|
||||
height: 100vh;
|
||||
margin: 0 auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
padding-top: 18px;
|
||||
}
|
||||
|
||||
.top {
|
||||
display: grid;
|
||||
grid-template-columns: auto minmax(0, 1fr) auto;
|
||||
gap: 18px 28px;
|
||||
align-items: end;
|
||||
padding: 4px 6px 16px;
|
||||
}
|
||||
.brand { display: flex; gap: 12px; align-items: center; }
|
||||
.mark {
|
||||
width: 44px;
|
||||
height: 44px;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
border-radius: 12px;
|
||||
background: var(--green-deep);
|
||||
color: #f6f0e4;
|
||||
font-family: var(--serif);
|
||||
font-size: 24px;
|
||||
}
|
||||
.kicker {
|
||||
font-size: 11px;
|
||||
letter-spacing: 0.16em;
|
||||
text-transform: uppercase;
|
||||
color: var(--gold);
|
||||
}
|
||||
h1 {
|
||||
font-family: var(--serif);
|
||||
font-size: 32px;
|
||||
font-weight: 600;
|
||||
line-height: 1;
|
||||
}
|
||||
.lede {
|
||||
max-width: 460px;
|
||||
color: var(--muted);
|
||||
line-height: 1.55;
|
||||
padding-bottom: 4px;
|
||||
}
|
||||
.pills { display: flex; flex-wrap: wrap; gap: 8px; justify-content: flex-end; }
|
||||
.pill {
|
||||
border: 1px solid var(--line);
|
||||
background: rgba(255, 250, 242, 0.8);
|
||||
border-radius: 999px;
|
||||
padding: 6px 10px;
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
.pill strong { color: var(--ink); font-weight: 600; }
|
||||
.pill.is-ok { color: var(--green-deep); border-color: #b7d7c8; background: var(--green-soft); }
|
||||
.pill.is-wait { color: var(--gold); }
|
||||
.pill.is-bad { color: var(--cinnabar); border-color: #f0c2b6; background: #fff1ec; }
|
||||
|
||||
.workspace {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) 332px;
|
||||
gap: 16px;
|
||||
padding-bottom: 16px;
|
||||
}
|
||||
.chat, .library {
|
||||
min-height: 0;
|
||||
background: rgba(255, 250, 242, 0.78);
|
||||
border: 1px solid rgba(255, 255, 255, 0.7);
|
||||
box-shadow: var(--shadow);
|
||||
border-radius: var(--radius);
|
||||
}
|
||||
.chat { display: flex; flex-direction: column; overflow: hidden; }
|
||||
.messages {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
overflow: auto;
|
||||
padding: 18px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.empty {
|
||||
margin: auto;
|
||||
max-width: 640px;
|
||||
text-align: center;
|
||||
padding: 24px 8px 12px;
|
||||
}
|
||||
.empty h2 {
|
||||
font-family: var(--serif);
|
||||
font-size: 34px;
|
||||
font-weight: 600;
|
||||
letter-spacing: -0.03em;
|
||||
}
|
||||
.empty p { margin-top: 10px; color: var(--muted); line-height: 1.7; }
|
||||
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
|
||||
.chip, .textish, .ghost, .send, .cite, .icon-btn {
|
||||
border: 1px solid var(--line);
|
||||
background: var(--paper);
|
||||
border-radius: 999px;
|
||||
}
|
||||
.chip {
|
||||
padding: 8px 12px;
|
||||
color: var(--green-deep);
|
||||
}
|
||||
.chip:hover, .icon-btn:hover, .textish:hover, .ghost:hover { border-color: #b7d7c8; background: var(--green-soft); }
|
||||
.chip:disabled, .send:disabled, .ghost:disabled, .textish:disabled, textarea:disabled, input:disabled, select:disabled {
|
||||
cursor: not-allowed;
|
||||
opacity: 0.55;
|
||||
}
|
||||
|
||||
.turn {
|
||||
border: 1px solid var(--line);
|
||||
background: var(--paper);
|
||||
border-radius: 16px;
|
||||
overflow: hidden;
|
||||
}
|
||||
.question {
|
||||
padding: 14px 16px 12px;
|
||||
border-bottom: 1px solid var(--line);
|
||||
background:
|
||||
linear-gradient(90deg, rgba(184, 67, 36, 0.08), transparent 28%),
|
||||
var(--paper-2);
|
||||
}
|
||||
.question p {
|
||||
margin-top: 4px;
|
||||
font-family: var(--serif);
|
||||
font-size: 20px;
|
||||
line-height: 1.45;
|
||||
}
|
||||
.label {
|
||||
font-size: 11px;
|
||||
letter-spacing: 0.14em;
|
||||
text-transform: uppercase;
|
||||
color: var(--gold);
|
||||
}
|
||||
.answer-row {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1.15fr) minmax(250px, 0.85fr);
|
||||
}
|
||||
.answer, .slices { padding: 14px 16px 16px; min-width: 0; }
|
||||
.slices {
|
||||
background: #fbf6ee;
|
||||
border-left: 1px solid var(--line);
|
||||
max-height: 560px;
|
||||
overflow: auto;
|
||||
}
|
||||
.answer-body {
|
||||
margin-top: 8px;
|
||||
font-family: var(--serif);
|
||||
font-size: 16.5px;
|
||||
line-height: 1.8;
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.answer-foot, .quiet, .hint, .slice-meta, .doc-preview, .warn {
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
line-height: 1.55;
|
||||
}
|
||||
.answer-foot { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 12px; }
|
||||
.tag {
|
||||
border-radius: 999px;
|
||||
padding: 2px 8px;
|
||||
background: var(--green-soft);
|
||||
color: var(--green-deep);
|
||||
}
|
||||
.tag.is-extract { background: #f8ead7; color: var(--gold); }
|
||||
.warn {
|
||||
margin: 8px 0 0;
|
||||
color: var(--cinnabar);
|
||||
}
|
||||
.cite {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
margin: 0 2px;
|
||||
padding: 0 6px;
|
||||
height: 22px;
|
||||
border-radius: 999px;
|
||||
background: var(--green-soft);
|
||||
color: var(--green-deep);
|
||||
border-color: #b7d7c8;
|
||||
font-family: var(--sans);
|
||||
font-size: 12px;
|
||||
vertical-align: 1px;
|
||||
}
|
||||
.cite:hover, .cite.is-on { background: var(--green); color: white; border-color: var(--green); }
|
||||
|
||||
.slices-head {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 8px;
|
||||
align-items: baseline;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.slices-head h3 { font-size: 14px; font-weight: 600; }
|
||||
.slice-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
|
||||
.slice {
|
||||
border: 1px solid var(--line);
|
||||
background: var(--paper);
|
||||
border-radius: 12px;
|
||||
padding: 10px 10px 11px;
|
||||
}
|
||||
.slice.is-active { border-color: var(--green); box-shadow: inset 3px 0 0 var(--green); }
|
||||
.slice-top { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
|
||||
.slice-no {
|
||||
font-variant-numeric: tabular-nums;
|
||||
font-size: 12px;
|
||||
color: var(--cinnabar);
|
||||
font-weight: 650;
|
||||
}
|
||||
.bar {
|
||||
flex: 1;
|
||||
height: 4px;
|
||||
border-radius: 999px;
|
||||
background: #efe4d4;
|
||||
overflow: hidden;
|
||||
}
|
||||
.bar span { display: block; height: 100%; background: var(--cinnabar); border-radius: inherit; }
|
||||
.slice-title { margin-top: 6px; font-size: 13px; font-weight: 600; }
|
||||
.slice-text {
|
||||
margin: 4px 0 0;
|
||||
font-size: 13px;
|
||||
line-height: 1.65;
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
.pending {
|
||||
height: 12px;
|
||||
margin-top: 12px;
|
||||
border-radius: 999px;
|
||||
background: linear-gradient(90deg, #f3eadc, #e7f3ec, #f3eadc);
|
||||
background-size: 200% 100%;
|
||||
animation: sheen 1.1s linear infinite;
|
||||
}
|
||||
.pending.short { width: 46%; }
|
||||
@keyframes sheen { to { background-position: -200% 0; } }
|
||||
|
||||
.composer { border-top: 1px solid var(--line); padding: 12px; background: rgba(255, 250, 242, 0.92); }
|
||||
textarea, input[type="text"], input:not([type="file"]), select {
|
||||
width: 100%;
|
||||
border: 1px solid var(--line);
|
||||
background: white;
|
||||
border-radius: 12px;
|
||||
padding: 10px 12px;
|
||||
outline: none;
|
||||
}
|
||||
textarea { resize: vertical; line-height: 1.55; }
|
||||
textarea:focus, input:focus, select:focus { border-color: #8fbfa8; }
|
||||
.composer textarea { min-height: 64px; }
|
||||
.composer-bar, .doc-actions { display: flex; align-items: center; gap: 10px; }
|
||||
.composer-bar { margin-top: 8px; }
|
||||
.hint { margin-right: auto; }
|
||||
.topk { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
|
||||
.topk select { width: auto; padding: 6px 8px; border-radius: 10px; }
|
||||
.send, .ghost {
|
||||
border-radius: 12px;
|
||||
padding: 9px 16px;
|
||||
background: var(--green);
|
||||
color: white;
|
||||
border-color: var(--green-deep);
|
||||
}
|
||||
.ghost { background: transparent; color: var(--green-deep); }
|
||||
.textish { background: transparent; padding: 8px 10px; }
|
||||
|
||||
.library { display: flex; flex-direction: column; overflow: hidden; }
|
||||
.library-head, .doc-form { padding: 14px 14px 0; }
|
||||
.library-head h2 { font-family: var(--serif); font-size: 22px; font-weight: 600; margin-top: 2px; }
|
||||
.library-head .quiet { margin-top: 8px; }
|
||||
.facts { display: grid; grid-template-columns: 72px 1fr; gap: 4px 8px; margin: 12px 0 0; font-size: 12px; }
|
||||
.facts dt { color: var(--muted); }
|
||||
.facts dd { margin: 0; }
|
||||
.doc-form { display: flex; flex-direction: column; gap: 8px; padding-top: 12px; }
|
||||
.doc-form label, .sr { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
|
||||
.sr {
|
||||
position: absolute;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
overflow: hidden;
|
||||
clip: rect(0 0 0 0);
|
||||
}
|
||||
.doc-actions { margin: 2px 0 12px; }
|
||||
.doc-list { flex: 1; overflow: auto; padding: 0 10px 12px; display: flex; flex-direction: column; gap: 8px; }
|
||||
.doc {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) auto;
|
||||
gap: 4px 8px;
|
||||
padding: 10px;
|
||||
border-radius: 12px;
|
||||
background: var(--paper-2);
|
||||
}
|
||||
.doc-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.doc-preview { grid-column: 1 / -1; }
|
||||
.icon-btn {
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border-radius: 8px;
|
||||
color: var(--cinnabar);
|
||||
}
|
||||
.empty-docs { padding: 8px 6px 12px; }
|
||||
|
||||
.toast {
|
||||
position: fixed;
|
||||
left: 50%;
|
||||
bottom: 22px;
|
||||
transform: translateX(-50%) translateY(12px);
|
||||
background: var(--ink);
|
||||
color: #f6f0e4;
|
||||
padding: 10px 14px;
|
||||
border-radius: 999px;
|
||||
opacity: 0;
|
||||
pointer-events: none;
|
||||
transition: opacity 0.2s ease, transform 0.2s ease;
|
||||
max-width: min(520px, calc(100% - 32px));
|
||||
}
|
||||
.toast.is-on { opacity: 1; transform: translateX(-50%) translateY(0); }
|
||||
|
||||
@media (max-width: 980px) {
|
||||
.app { height: auto; min-height: 100vh; }
|
||||
.top, .workspace, .answer-row { grid-template-columns: 1fr; }
|
||||
.pills { justify-content: flex-start; }
|
||||
.workspace { display: flex; flex-direction: column; }
|
||||
.chat { min-height: 70vh; }
|
||||
.library { min-height: 520px; }
|
||||
.slices { border-left: 0; border-top: 1px solid var(--line); max-height: none; }
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.pending { animation: none; }
|
||||
.toast { transition: none; }
|
||||
}
|
||||
Reference in New Issue
Block a user