Buckets:
| from fastapi import APIRouter, HTTPException | |
| from typing import List, Dict, Any | |
| from .models import TutorConfig, ChatRequest, ChatResponse | |
| from .manager import manager # ← import the shared global instance (not the class) | |
| router = APIRouter(prefix="/tutors", tags=["tutors"]) | |
| def list_tutors(): | |
| """List all tutor configurations.""" | |
| return list(manager.list_configs().values()) | |
| def upsert_tutor(cfg: TutorConfig): | |
| """Create or update a tutor configuration.""" | |
| manager.create_or_update(cfg) | |
| return cfg | |
| async def chat(tutor_id: str, req: ChatRequest): | |
| """Chat with a tutor.""" | |
| try: | |
| rt = manager.get_runtime(tutor_id) | |
| except FileNotFoundError as e: | |
| raise HTTPException(status_code=404, detail=str(e)) | |
| result = await rt.chat( | |
| user_prompt=req.message, | |
| history=[h.model_dump() for h in (req.history or [])], | |
| use_rag=req.use_rag, | |
| max_tokens=req.max_tokens, | |
| ) | |
| return ChatResponse( | |
| tutor_id=tutor_id, | |
| answer=result["answer"], | |
| sources=result["sources"], | |
| ) | |
| def ingest(tutor_id: str, rebuild: bool = False): | |
| """Rebuild or update the RAG index for a tutor.""" | |
| rt = manager.get_runtime(tutor_id) | |
| store = rt.ensure_store() | |
| if store is None: | |
| raise HTTPException(status_code=400, detail="RAG disabled for this tutor") | |
| import glob, os | |
| src_dir = f"data/source/{tutor_id}" | |
| os.makedirs(src_dir, exist_ok=True) | |
| paths = [] | |
| for ext in ("*.txt", "*.md", "*.pdf"): | |
| paths.extend(glob.glob(os.path.join(src_dir, ext))) | |
| added = store.index_paths(paths, embed_fn=rt.embed_sync, rebuild=rebuild) | |
| return {"tutor_id": tutor_id, "files_indexed": len(paths), "chunks_added": added} | |
Xet Storage Details
- Size:
- 1.93 kB
- Xet hash:
- a3e6911af2da6834c4e9ed85fe7114f7e04a8ba98ba23bf0c98ba149668b0eac
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.