- added three iwm articles as test data
- added simple webview and FastAPI server
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
"""
|
||||
FastAPI server for Local RAG with chat GUI.
|
||||
Run with: uvicorn server:app --reload
|
||||
"""
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.responses import HTMLResponse
|
||||
from pydantic import BaseModel
|
||||
|
||||
from local_rag import LocalRAG
|
||||
|
||||
# Initialize RAG once at startup
|
||||
VECTORSTORE_PATH = "./vectorstore"
|
||||
OLLAMA_MODEL = "mistral:7b"
|
||||
rag = LocalRAG(vectorstore_path=VECTORSTORE_PATH, ollama_model=OLLAMA_MODEL)
|
||||
|
||||
app = FastAPI(title="Local RAG Chat", version="1.0.0")
|
||||
|
||||
|
||||
class ChatRequest(BaseModel):
|
||||
message: str
|
||||
|
||||
|
||||
class ChatResponse(BaseModel):
|
||||
answer: str
|
||||
error: str | None = None
|
||||
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
def chat_view():
|
||||
"""Serve the chat GUI."""
|
||||
html_path = Path(__file__).parent / "templates" / "chat.html"
|
||||
if not html_path.exists():
|
||||
raise HTTPException(status_code=500, detail="Chat template not found")
|
||||
return HTMLResponse(content=html_path.read_text(encoding="utf-8"))
|
||||
|
||||
|
||||
@app.post("/api/chat", response_model=ChatResponse)
|
||||
def chat(request: ChatRequest):
|
||||
"""Handle a chat message and return the RAG answer."""
|
||||
if not request.message or not request.message.strip():
|
||||
return ChatResponse(answer="", error="Message cannot be empty")
|
||||
try:
|
||||
answer = rag.query(request.message.strip())
|
||||
return ChatResponse(answer=answer)
|
||||
except Exception as e:
|
||||
return ChatResponse(answer="", error=str(e))
|
||||
|
||||
|
||||
@app.get("/api/health")
|
||||
def health():
|
||||
"""Health check and vector store status."""
|
||||
has_docs = rag.vectorstore is not None
|
||||
return {"status": "ok", "vectorstore_loaded": has_docs}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||
Reference in New Issue
Block a user