Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,88 @@ from tools.final_answer import FinalAnswerTool
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
@tool
|
12 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
13 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
@@ -55,7 +136,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
+
|
11 |
+
from typing import Optional, Dict, Any
|
12 |
+
import requests
|
13 |
+
from pydantic import BaseModel, Field, validator
|
14 |
+
|
15 |
+
class PDOKLocationSearchInput(BaseModel):
|
16 |
+
"""Input for the PDOK location search tool."""
|
17 |
+
postal_code: Optional[str] = Field(None, description="Postal code in the format '1234 AA'.")
|
18 |
+
house_number: Optional[str] = Field(None, description="House number.")
|
19 |
+
street_name: Optional[str] = Field(None, description="Street name.")
|
20 |
+
city: Optional[str] = Field(None, description="City name.")
|
21 |
+
|
22 |
+
@validator("postal_code")
|
23 |
+
def validate_postal_code(cls, v):
|
24 |
+
if v is not None and (len(v) != 7 or not v[0:4].isdigit() or v[4] != " " or not v[5:7].isalpha()):
|
25 |
+
raise ValueError("Invalid postal code format. It must be '1234 AA'.")
|
26 |
+
return v
|
27 |
+
|
28 |
+
def construct_query(self) -> str:
|
29 |
+
"""Constructs the query string based on provided inputs."""
|
30 |
+
if self.postal_code and self.house_number:
|
31 |
+
return f"{self.postal_code} {self.house_number}"
|
32 |
+
elif self.street_name and self.city and self.house_number:
|
33 |
+
return f"{self.street_name} {self.house_number}, {self.city}"
|
34 |
+
else:
|
35 |
+
return "" # Will raise error in the tool itself
|
36 |
+
|
37 |
+
@tool
|
38 |
+
def pdok_location_search(input_data: PDOKLocationSearchInput) -> str:
|
39 |
+
"""A tool that queries the PDOK API to find the province related to an adress in the Netherlands.
|
40 |
+
|
41 |
+
Args:
|
42 |
+
input_data: Input parameters, provide either 'postal_code' and 'house_number' OR 'street_name', 'city', and 'house_number'.
|
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", # Only fetch provincienaam
|
54 |
+
"fq": "type:(gemeente OR woonplaats OR weg OR postcode OR adres)",
|
55 |
+
"df": "tekst",
|
56 |
+
"bq": "type:provincie^1.5",
|
57 |
+
"bq": "type:gemeente^1.5",
|
58 |
+
"bq": "type:woonplaats^1.5",
|
59 |
+
"bq": "type:weg^1.5",
|
60 |
+
"bq": "type:postcode^0.5",
|
61 |
+
"bq": "type:adres^1",
|
62 |
+
"start": 0,
|
63 |
+
"rows": 10,
|
64 |
+
"sort": "score desc,sortering asc,weergavenaam asc",
|
65 |
+
"wt": "json",
|
66 |
+
}
|
67 |
+
|
68 |
+
try:
|
69 |
+
response = requests.get(base_url, params=params, headers=headers)
|
70 |
+
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
|
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 |
+
|
81 |
+
if provincie_naam:
|
82 |
+
return f"The province is: {provincie_naam}"
|
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:
|
90 |
+
return f"Error processing API response: {e}"
|
91 |
+
|
92 |
@tool
|
93 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
94 |
#Keep this format for the description / args / args description but feel free to modify the tool
|
|
|
136 |
|
137 |
agent = CodeAgent(
|
138 |
model=model,
|
139 |
+
tools=[final_answer, get_current_time_in_timezone, pdok_location_search], ## add your tools here (don't remove final answer)
|
140 |
max_steps=6,
|
141 |
verbosity_level=1,
|
142 |
grammar=None,
|