Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -35,22 +35,28 @@ class PDOKLocationSearchInput(BaseModel):
|
|
| 35 |
return "" # Will raise error in the tool itself
|
| 36 |
|
| 37 |
@tool
|
| 38 |
-
def pdok_location_search(
|
| 39 |
-
"""A tool that queries the PDOK API to find
|
| 40 |
|
| 41 |
Args:
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
base_url = "https://api.pdok.nl/bzk/locatieserver/search/v3_1/free"
|
| 45 |
headers = {"accept": "application/json"}
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
query_string = input_data.construct_query()
|
| 48 |
if not query_string:
|
| 49 |
return "Please provide either a postal code and house number, or a street name, city, and house number."
|
| 50 |
-
|
| 51 |
params = {
|
| 52 |
"q": query_string,
|
| 53 |
-
"fl": "provincienaam",
|
| 54 |
"fq": "type:(gemeente OR woonplaats OR weg OR postcode OR adres)",
|
| 55 |
"df": "tekst",
|
| 56 |
"bq": "type:provincie^1.5",
|
|
@@ -67,14 +73,13 @@ def pdok_location_search(input_data: PDOKLocationSearchInput) -> str:
|
|
| 67 |
|
| 68 |
try:
|
| 69 |
response = requests.get(base_url, params=params, headers=headers)
|
| 70 |
-
response.raise_for_status()
|
| 71 |
data = response.json()
|
| 72 |
|
| 73 |
docs = data.get("response", {}).get("docs", [])
|
| 74 |
if not docs:
|
| 75 |
return "No results found for the given query."
|
| 76 |
|
| 77 |
-
# Extract and return the 'provincienaam' from the first result.
|
| 78 |
first_result = docs[0]
|
| 79 |
provincie_naam = first_result.get("provincienaam")
|
| 80 |
|
|
@@ -83,7 +88,6 @@ def pdok_location_search(input_data: PDOKLocationSearchInput) -> str:
|
|
| 83 |
else:
|
| 84 |
return "Provincienaam not found in the API response."
|
| 85 |
|
| 86 |
-
|
| 87 |
except requests.exceptions.RequestException as e:
|
| 88 |
return f"Error during API request: {e}"
|
| 89 |
except (ValueError, KeyError) as e:
|
|
|
|
| 35 |
return "" # Will raise error in the tool itself
|
| 36 |
|
| 37 |
@tool
|
| 38 |
+
def pdok_location_search(postal_code: Optional[str] = None, house_number: Optional[str] = None, street_name: Optional[str] = None, city: Optional[str] = None) -> str:
|
| 39 |
+
"""A tool that queries the PDOK API to find location information in the Netherlands.
|
| 40 |
|
| 41 |
Args:
|
| 42 |
+
postal_code: Postal code in the format '1234 AA'.
|
| 43 |
+
house_number: House number.
|
| 44 |
+
street_name: Street name.
|
| 45 |
+
city: City name.
|
| 46 |
"""
|
| 47 |
base_url = "https://api.pdok.nl/bzk/locatieserver/search/v3_1/free"
|
| 48 |
headers = {"accept": "application/json"}
|
| 49 |
|
| 50 |
+
# Create an instance of the Pydantic model from the function arguments
|
| 51 |
+
input_data = PDOKLocationSearchInput(postal_code=postal_code, house_number=house_number, street_name=street_name, city=city)
|
| 52 |
+
|
| 53 |
query_string = input_data.construct_query()
|
| 54 |
if not query_string:
|
| 55 |
return "Please provide either a postal code and house number, or a street name, city, and house number."
|
| 56 |
+
|
| 57 |
params = {
|
| 58 |
"q": query_string,
|
| 59 |
+
"fl": "provincienaam",
|
| 60 |
"fq": "type:(gemeente OR woonplaats OR weg OR postcode OR adres)",
|
| 61 |
"df": "tekst",
|
| 62 |
"bq": "type:provincie^1.5",
|
|
|
|
| 73 |
|
| 74 |
try:
|
| 75 |
response = requests.get(base_url, params=params, headers=headers)
|
| 76 |
+
response.raise_for_status()
|
| 77 |
data = response.json()
|
| 78 |
|
| 79 |
docs = data.get("response", {}).get("docs", [])
|
| 80 |
if not docs:
|
| 81 |
return "No results found for the given query."
|
| 82 |
|
|
|
|
| 83 |
first_result = docs[0]
|
| 84 |
provincie_naam = first_result.get("provincienaam")
|
| 85 |
|
|
|
|
| 88 |
else:
|
| 89 |
return "Provincienaam not found in the API response."
|
| 90 |
|
|
|
|
| 91 |
except requests.exceptions.RequestException as e:
|
| 92 |
return f"Error during API request: {e}"
|
| 93 |
except (ValueError, KeyError) as e:
|