Gemini commited on
Commit
7c8e447
·
1 Parent(s): 708c39d

fix: Reorder endpoints to fix 404 error on /api/chat

Browse files
Files changed (1) hide show
  1. main.py +26 -31
main.py CHANGED
@@ -16,6 +16,31 @@ app = FastAPI()
16
  # Mount the static directory to serve CSS and JS files
17
  app.mount("/static", StaticFiles(directory="static"), name="static")
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  # API v1 Router (secured)
20
  api_v1 = APIRouter()
21
 
@@ -52,40 +77,10 @@ async def proxy_v1(request: Request, path: str):
52
 
53
  app.include_router(api_v1, prefix="/api/v1")
54
 
55
- # Chat API endpoint (not secured)
56
- @app.post("/api/chat")
57
- async def chat_endpoint(request: Request):
58
- body = await request.json()
59
- model = body.get("model", "llama3")
60
- prompt = body.get("prompt")
61
-
62
- if not prompt:
63
- raise HTTPException(status_code=400, detail="Prompt is required")
64
-
65
- url = f"{OLLAMA_API_URL}/api/generate"
66
-
67
- try:
68
- response = requests.post(
69
- url=url,
70
- json={"model": model, "prompt": prompt, "stream": False}
71
- )
72
-
73
- return JSONResponse(
74
- status_code=response.status_code,
75
- content=response.json()
76
- )
77
- except Exception as e:
78
- raise HTTPException(status_code=500, detail=str(e))
79
-
80
-
81
- @app.get("/hello")
82
- async def hello():
83
- return {"message": "Hello, world!"}
84
-
85
  @app.get("/")
86
  async def root():
87
  return RedirectResponse(url="/chat")
88
 
89
  @app.get("/chat")
90
  async def chat_page():
91
- return FileResponse('chat.html')
 
16
  # Mount the static directory to serve CSS and JS files
17
  app.mount("/static", StaticFiles(directory="static"), name="static")
18
 
19
+ # Chat API endpoint (not secured)
20
+ @app.post("/api/chat")
21
+ async def chat_endpoint(request: Request):
22
+ body = await request.json()
23
+ model = body.get("model", "llama3")
24
+ prompt = body.get("prompt")
25
+
26
+ if not prompt:
27
+ raise HTTPException(status_code=400, detail="Prompt is required")
28
+
29
+ url = f"{OLLAMA_API_URL}/api/generate"
30
+
31
+ try:
32
+ response = requests.post(
33
+ url=url,
34
+ json={"model": model, "prompt": prompt, "stream": False}
35
+ )
36
+
37
+ return JSONResponse(
38
+ status_code=response.status_code,
39
+ content=response.json()
40
+ )
41
+ except Exception as e:
42
+ raise HTTPException(status_code=500, detail=str(e))
43
+
44
  # API v1 Router (secured)
45
  api_v1 = APIRouter()
46
 
 
77
 
78
  app.include_router(api_v1, prefix="/api/v1")
79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  @app.get("/")
81
  async def root():
82
  return RedirectResponse(url="/chat")
83
 
84
  @app.get("/chat")
85
  async def chat_page():
86
+ return FileResponse('chat.html')