Spaces:
Running
Running
Added changes to /search endpoint
Browse files- app/api.py +12 -2
app/api.py
CHANGED
@@ -4,7 +4,7 @@ app = FastAPI()
|
|
4 |
|
5 |
|
6 |
@app.get("/search")
|
7 |
-
def search(query: str, n: int = Query(5, ge=1
|
8 |
"""
|
9 |
Endpoint for querying the search engine.
|
10 |
Args:
|
@@ -13,9 +13,19 @@ def search(query: str, n: int = Query(5, ge=1, le=10)):
|
|
13 |
Returns:
|
14 |
dict: Query results.
|
15 |
"""
|
|
|
|
|
|
|
16 |
search_engine = app.state.search_engine
|
17 |
if not search_engine:
|
18 |
raise HTTPException(status_code=500, detail="Search engine not initialized.")
|
19 |
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
return {"query": query, "results": results}
|
|
|
4 |
|
5 |
|
6 |
@app.get("/search")
|
7 |
+
def search(query: str, n: int = Query(5, ge=1)):
|
8 |
"""
|
9 |
Endpoint for querying the search engine.
|
10 |
Args:
|
|
|
13 |
Returns:
|
14 |
dict: Query results.
|
15 |
"""
|
16 |
+
if not query.strip():
|
17 |
+
raise HTTPException(status_code=400, detail="Query cannot be empty.")
|
18 |
+
|
19 |
search_engine = app.state.search_engine
|
20 |
if not search_engine:
|
21 |
raise HTTPException(status_code=500, detail="Search engine not initialized.")
|
22 |
|
23 |
+
try:
|
24 |
+
results = search_engine.most_similar(query, n)
|
25 |
+
except Exception as e:
|
26 |
+
raise HTTPException(
|
27 |
+
status_code=500,
|
28 |
+
detail=f"An unexpected error occurred: {str(e)}"
|
29 |
+
)
|
30 |
+
|
31 |
return {"query": query, "results": results}
|