Update app.py
Browse files6 unmodified lines7891011122 unmodified lines1516171819209 unmodified lines30313233343536373839404142434445462 unmodified lines495051525354555657585960616 unmodified linesfrom sentence_transformers import SentenceTransformerimport osapp = FastAPI()app.add_middleware(2 unmodified linesallow_methods=["*"],allow_headers=["*"])docs = ["Welcome to my bakery. We sell cookies and cakes.","Our delivery policy: we deliver within 2 days in Karachi.",9 unmodified linesids=[str(i)])co = cohere.Client(os.getenv("COHERE_API_KEY"))# Chroma client (NO folders, HF compatible)client = Client()collection = client.get_or_create_collection(name="docs")embed_model = SentenceTransformer("all-MiniLM-L6-v2")class Query(BaseModel):message: str@app.get("/", response_class=HTMLResponse)def serve_ui():with open("index.html", "r", encoding="utf-8") as f:2 unmodified lines@app.post("/chat")def chat(query: Query):query_emb = embed_model.encode(query.message).tolist()results = collection.query(query_embeddings=[query_emb],n_results=3)docs = results["documents"][0]context = "\n".join(docs)prompt = f"""Use the context below to answer the question.6 unmodified lines7891011121314152 unmodified lines18192021222324252627282930313233343536379 unmodified lines4748495051525354555657585960612 unmodified lines64656667686970717273747576776 unmodified linesfrom sentence_transformers import SentenceTransformerimport os# --------------------# App setup# --------------------app = FastAPI()app.add_middleware(2 unmodified linesallow_methods=["*"],allow_headers=["*"])# --------------------# Clients & models# --------------------co = cohere.Client(os.getenv("COHERE_API_KEY"))client = Client() # HF-compatible (no folders)collection = client.get_or_create_collection(name="docs")embed_model = SentenceTransformer("all-MiniLM-L6-v2")# --------------------# One-time ingestion# --------------------docs = ["Welcome to my bakery. We sell cookies and cakes.","Our delivery policy: we deliver within 2 days in Karachi.",9 unmodified linesids=[str(i)])# --------------------# Schemas# --------------------class Query(BaseModel):message: str# --------------------# Routes# --------------------@app.get("/", response_class=HTMLResponse)def serve_ui():with open("index.html", "r", encoding="utf-8") as f:2 unmodified lines@app.post("/chat")def chat(query: Query):query_emb = embed_model.encode(query.message).tolist()results = collection.query(query_embeddings=[query_emb],n_results=3)context_docs = results["documents"][0]context = "\n".join(context_docs)prompt = f"""Use the context below to answer the question.
app.py+26-10