Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,22 +46,15 @@ class PDOKLocationInfo(BaseModel):
|
|
| 46 |
centroide_rd: str
|
| 47 |
|
| 48 |
@tool
|
| 49 |
-
def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional[str] = None, street_name: Optional[str] = None, city: Optional[str] = None) ->
|
| 50 |
-
"""
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
A postalcode and housenumber or a streetname, housenumber and city are required.
|
| 59 |
-
|
| 60 |
-
Ask the user for additional input if you miss an element to fullfill the requirements of this tool, don't fill in these elements on your own.
|
| 61 |
-
|
| 62 |
-
Returns:
|
| 63 |
-
A PDOKLocationInfo object containing the location information, or None if no results are found.
|
| 64 |
-
"""
|
| 65 |
|
| 66 |
base_url = "https://api.pdok.nl/bzk/locatieserver/search/v3_1/free"
|
| 67 |
headers = {"accept": "application/json"}
|
|
@@ -69,12 +62,14 @@ def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional
|
|
| 69 |
input_data = PDOKLocationSearchInput(postal_code=postal_code, house_number=house_number, street_name=street_name, city=city)
|
| 70 |
query_string = input_data.construct_query()
|
| 71 |
|
|
|
|
|
|
|
| 72 |
if not query_string:
|
| 73 |
-
return
|
| 74 |
|
| 75 |
params = {
|
| 76 |
"q": query_string,
|
| 77 |
-
"fl": "*",
|
| 78 |
"fq": "type:(gemeente OR woonplaats OR weg OR postcode OR adres)",
|
| 79 |
"df": "tekst",
|
| 80 |
"bq": "type:provincie^1.5",
|
|
@@ -89,32 +84,32 @@ def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional
|
|
| 89 |
"wt": "json",
|
| 90 |
}
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
try:
|
| 93 |
response = requests.get(base_url, params=params, headers=headers)
|
|
|
|
|
|
|
|
|
|
| 94 |
response.raise_for_status()
|
| 95 |
data = response.json()
|
| 96 |
|
| 97 |
docs = data.get("response", {}).get("docs", [])
|
|
|
|
|
|
|
| 98 |
if not docs:
|
| 99 |
-
return
|
| 100 |
|
| 101 |
first_result = docs[0]
|
| 102 |
location_info = PDOKLocationInfo(**first_result)
|
| 103 |
-
|
| 104 |
-
# Format the output in a more user-friendly way
|
| 105 |
-
print(f"Information about: {location_info.weergavenaam}")
|
| 106 |
-
print("-" * 30)
|
| 107 |
-
for field, value in location_info.dict().items():
|
| 108 |
-
print(f" {field.capitalize()}: {value}")
|
| 109 |
-
|
| 110 |
return location_info
|
| 111 |
|
| 112 |
except requests.exceptions.RequestException as e:
|
| 113 |
-
|
| 114 |
-
return None
|
| 115 |
except (ValueError, KeyError) as e:
|
| 116 |
-
|
| 117 |
-
return None
|
| 118 |
|
| 119 |
@tool
|
| 120 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
|
|
|
| 46 |
centroide_rd: str
|
| 47 |
|
| 48 |
@tool
|
| 49 |
+
def pdok_location_info(postal_code: Optional[str] = None, house_number: Optional[str] = None, street_name: Optional[str] = None, city: Optional[str] = None) -> Union[PDOKLocationInfo, str]:
|
| 50 |
+
"""[Original docstring remains the same]"""
|
| 51 |
|
| 52 |
+
debug_info = []
|
| 53 |
+
debug_info.append(f"Input values:\n"
|
| 54 |
+
f" Postal code: {postal_code}\n"
|
| 55 |
+
f" House number: {house_number}\n"
|
| 56 |
+
f" Street name: {street_name}\n"
|
| 57 |
+
f" City: {city}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
base_url = "https://api.pdok.nl/bzk/locatieserver/search/v3_1/free"
|
| 60 |
headers = {"accept": "application/json"}
|
|
|
|
| 62 |
input_data = PDOKLocationSearchInput(postal_code=postal_code, house_number=house_number, street_name=street_name, city=city)
|
| 63 |
query_string = input_data.construct_query()
|
| 64 |
|
| 65 |
+
debug_info.append(f"Constructed query string: {query_string}")
|
| 66 |
+
|
| 67 |
if not query_string:
|
| 68 |
+
return f"Error: Empty query string\nDebug info:\n" + "\n".join(debug_info)
|
| 69 |
|
| 70 |
params = {
|
| 71 |
"q": query_string,
|
| 72 |
+
"fl": "*",
|
| 73 |
"fq": "type:(gemeente OR woonplaats OR weg OR postcode OR adres)",
|
| 74 |
"df": "tekst",
|
| 75 |
"bq": "type:provincie^1.5",
|
|
|
|
| 84 |
"wt": "json",
|
| 85 |
}
|
| 86 |
|
| 87 |
+
debug_info.append(f"API request details:\n"
|
| 88 |
+
f" URL: {base_url}\n"
|
| 89 |
+
f" Params: {params}")
|
| 90 |
+
|
| 91 |
try:
|
| 92 |
response = requests.get(base_url, params=params, headers=headers)
|
| 93 |
+
debug_info.append(f"Response status code: {response.status_code}\n"
|
| 94 |
+
f"Response URL: {response.url}")
|
| 95 |
+
|
| 96 |
response.raise_for_status()
|
| 97 |
data = response.json()
|
| 98 |
|
| 99 |
docs = data.get("response", {}).get("docs", [])
|
| 100 |
+
debug_info.append(f"Number of docs found: {len(docs)}")
|
| 101 |
+
|
| 102 |
if not docs:
|
| 103 |
+
return f"Error: No results found\nDebug info:\n" + "\n".join(debug_info)
|
| 104 |
|
| 105 |
first_result = docs[0]
|
| 106 |
location_info = PDOKLocationInfo(**first_result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
return location_info
|
| 108 |
|
| 109 |
except requests.exceptions.RequestException as e:
|
| 110 |
+
return f"Error during API request: {e}\nDebug info:\n" + "\n".join(debug_info)
|
|
|
|
| 111 |
except (ValueError, KeyError) as e:
|
| 112 |
+
return f"Error processing API response: {e}\nDebug info:\n" + "\n".join(debug_info)
|
|
|
|
| 113 |
|
| 114 |
@tool
|
| 115 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|