Skip to content

Commit a132c43

Browse files
committed
Add option to return STAC search params only
Will be useful for adding natural language search to external clients like STAC browser that can query a STAC api on their own
1 parent ee241ac commit a132c43

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

stac_search/agents/items_search.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Context:
1717
query: str
1818
location: str | None = None
1919
top_k: int = 5
20+
return_search_params_only: bool = False
2021

2122

2223
@dataclass
@@ -216,7 +217,8 @@ def get_polygon_from_geodini(location: str):
216217

217218
@dataclass
218219
class ItemSearchResult:
219-
items: List[Dict[str, Any]]
220+
items: List[Dict[str, Any]] | None = None
221+
search_params: Dict[str, Any] | None = None
220222
aoi: Dict[str, Any] | None = None
221223
explanation: str = ""
222224

@@ -271,9 +273,12 @@ async def item_search(ctx: Context) -> ItemSearchResult:
271273

272274
# print(f"Searching with params: {params}")
273275

274-
items = list(client.search(**params).items_as_dicts())
276+
if ctx.return_search_params_only:
277+
print("Returning STAC query parameters only")
278+
return ItemSearchResult(search_params=params, aoi=polygon, explanation=explanation)
275279

276-
return ItemSearchResult(items=items, aoi=polygon, explanation=explanation)
280+
items = list(client.search(**params).items_as_dicts())
281+
return ItemSearchResult(items=items, aoi=polygon, explanation=explanation, search_params=params)
277282

278283

279284
async def main():

stac_search/api.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,27 @@
2020
# Define request model
2121
class QueryRequest(BaseModel):
2222
query: str
23-
limit: int = 5
23+
24+
25+
class STACItemsRequest(BaseModel):
26+
query: str
27+
return_search_params_only: bool = False
2428

2529

2630
# Define search endpoint
2731
@app.post("/search")
2832
async def search(request: QueryRequest):
2933
"""Search for STAC collections using natural language"""
30-
results = collection_search(request.query, top_k=request.limit)
34+
results = collection_search(request.query)
3135
return {"results": results}
3236

3337

3438
@app.post("/items/search")
35-
async def search_items(request: QueryRequest):
39+
async def search_items(request: STACItemsRequest):
3640
"""Search for STAC items using natural language"""
37-
ctx = ItemSearchContext(query=request.query, top_k=request.limit)
41+
ctx = ItemSearchContext(
42+
query=request.query, return_search_params_only=request.return_search_params_only
43+
)
3844
results = await item_search(ctx)
3945
return {"results": results}
4046

0 commit comments

Comments
 (0)