-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcapstone_api.py
More file actions
84 lines (66 loc) · 2.09 KB
/
capstone_api.py
File metadata and controls
84 lines (66 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
"""FastAPI wrapper for Internship Copilot.
Run with:
uvicorn capstone_api:app --reload --port 8000
"""
from __future__ import annotations
from typing import List, Optional
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from agent import CHUNKS, ask, get_collection, kb_topics
app = FastAPI(
title="Internship Copilot API",
description="LangGraph + RAG + memory + live web search internship assistant",
version="1.0.0",
)
# Allow Streamlit frontend to call this API
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
class AskRequest(BaseModel):
question: str = Field(..., min_length=1, description="User question")
thread_id: str = Field("default", description="Conversation thread id")
provider: Optional[str] = Field(
None, description="openai, groq, openrouter, or auto"
)
model: Optional[str] = Field(None, description="Provider-specific model override")
class AskResponse(BaseModel):
answer: str
route: str
sources: List[str]
source_urls: List[str]
tool_type: str
faithfulness: float
thread_id: str
@app.get("/health")
def health() -> dict:
collection = get_collection()
return {
"status": "ok",
"docs": len(kb_topics()),
"chunks": len(CHUNKS),
"vector_rows": collection.count(),
}
@app.get("/topics")
def topics() -> dict:
return {"topics": kb_topics()}
@app.post("/ask", response_model=AskResponse)
def ask_question(request: AskRequest) -> AskResponse:
result = ask(
request.question,
thread_id=request.thread_id,
provider=request.provider,
model=request.model,
)
return AskResponse(
answer=result.get("answer", ""),
route=result.get("route", "retrieve"),
sources=result.get("sources", []),
source_urls=result.get("source_urls", []),
tool_type=result.get("tool_type", ""),
faithfulness=float(result.get("faithfulness", 0.0) or 0.0),
thread_id=request.thread_id,
)