mirror of
http://112.124.100.131/huang.ze/ebiz-dify-ai.git
synced 2025-12-20 16:26:53 +08:00
chore(api/core): apply ruff reformatting (#7624)
This commit is contained in:
@@ -9,17 +9,19 @@ from core.tools.provider.builtin_tool_provider import BuiltinToolProviderControl
|
||||
class GaodeProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: dict) -> None:
|
||||
try:
|
||||
if 'api_key' not in credentials or not credentials.get('api_key'):
|
||||
if "api_key" not in credentials or not credentials.get("api_key"):
|
||||
raise ToolProviderCredentialValidationError("Gaode API key is required.")
|
||||
|
||||
try:
|
||||
response = requests.get(url="https://restapi.amap.com/v3/geocode/geo?address={address}&key={apikey}"
|
||||
"".format(address=urllib.parse.quote('广东省广州市天河区广州塔'),
|
||||
apikey=credentials.get('api_key')))
|
||||
if response.status_code == 200 and (response.json()).get('info') == 'OK':
|
||||
response = requests.get(
|
||||
url="https://restapi.amap.com/v3/geocode/geo?address={address}&key={apikey}" "".format(
|
||||
address=urllib.parse.quote("广东省广州市天河区广州塔"), apikey=credentials.get("api_key")
|
||||
)
|
||||
)
|
||||
if response.status_code == 200 and (response.json()).get("info") == "OK":
|
||||
pass
|
||||
else:
|
||||
raise ToolProviderCredentialValidationError((response.json()).get('info'))
|
||||
raise ToolProviderCredentialValidationError((response.json()).get("info"))
|
||||
except Exception as e:
|
||||
raise ToolProviderCredentialValidationError("Gaode API Key is invalid. {}".format(e))
|
||||
except Exception as e:
|
||||
|
||||
@@ -8,50 +8,57 @@ from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class GaodeRepositoriesTool(BuiltinTool):
|
||||
def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
def _invoke(
|
||||
self, user_id: str, tool_parameters: dict[str, Any]
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
invoke tools
|
||||
"""
|
||||
city = tool_parameters.get('city', '')
|
||||
city = tool_parameters.get("city", "")
|
||||
if not city:
|
||||
return self.create_text_message('Please tell me your city')
|
||||
return self.create_text_message("Please tell me your city")
|
||||
|
||||
if 'api_key' not in self.runtime.credentials or not self.runtime.credentials.get('api_key'):
|
||||
if "api_key" not in self.runtime.credentials or not self.runtime.credentials.get("api_key"):
|
||||
return self.create_text_message("Gaode API key is required.")
|
||||
|
||||
try:
|
||||
s = requests.session()
|
||||
api_domain = 'https://restapi.amap.com/v3'
|
||||
city_response = s.request(method='GET', headers={"Content-Type": "application/json; charset=utf-8"},
|
||||
url="{url}/config/district?keywords={keywords}"
|
||||
"&subdistrict=0&extensions=base&key={apikey}"
|
||||
"".format(url=api_domain, keywords=city,
|
||||
apikey=self.runtime.credentials.get('api_key')))
|
||||
api_domain = "https://restapi.amap.com/v3"
|
||||
city_response = s.request(
|
||||
method="GET",
|
||||
headers={"Content-Type": "application/json; charset=utf-8"},
|
||||
url="{url}/config/district?keywords={keywords}" "&subdistrict=0&extensions=base&key={apikey}" "".format(
|
||||
url=api_domain, keywords=city, apikey=self.runtime.credentials.get("api_key")
|
||||
),
|
||||
)
|
||||
City_data = city_response.json()
|
||||
if city_response.status_code == 200 and City_data.get('info') == 'OK':
|
||||
if len(City_data.get('districts')) > 0:
|
||||
CityCode = City_data['districts'][0]['adcode']
|
||||
weatherInfo_response = s.request(method='GET',
|
||||
url="{url}/weather/weatherInfo?city={citycode}&extensions=all&key={apikey}&output=json"
|
||||
"".format(url=api_domain, citycode=CityCode,
|
||||
apikey=self.runtime.credentials.get('api_key')))
|
||||
if city_response.status_code == 200 and City_data.get("info") == "OK":
|
||||
if len(City_data.get("districts")) > 0:
|
||||
CityCode = City_data["districts"][0]["adcode"]
|
||||
weatherInfo_response = s.request(
|
||||
method="GET",
|
||||
url="{url}/weather/weatherInfo?city={citycode}&extensions=all&key={apikey}&output=json"
|
||||
"".format(url=api_domain, citycode=CityCode, apikey=self.runtime.credentials.get("api_key")),
|
||||
)
|
||||
weatherInfo_data = weatherInfo_response.json()
|
||||
if weatherInfo_response.status_code == 200 and weatherInfo_data.get('info') == 'OK':
|
||||
if weatherInfo_response.status_code == 200 and weatherInfo_data.get("info") == "OK":
|
||||
contents = []
|
||||
if len(weatherInfo_data.get('forecasts')) > 0:
|
||||
for item in weatherInfo_data['forecasts'][0]['casts']:
|
||||
if len(weatherInfo_data.get("forecasts")) > 0:
|
||||
for item in weatherInfo_data["forecasts"][0]["casts"]:
|
||||
content = {}
|
||||
content['date'] = item.get('date')
|
||||
content['week'] = item.get('week')
|
||||
content['dayweather'] = item.get('dayweather')
|
||||
content['daytemp_float'] = item.get('daytemp_float')
|
||||
content['daywind'] = item.get('daywind')
|
||||
content['nightweather'] = item.get('nightweather')
|
||||
content['nighttemp_float'] = item.get('nighttemp_float')
|
||||
content["date"] = item.get("date")
|
||||
content["week"] = item.get("week")
|
||||
content["dayweather"] = item.get("dayweather")
|
||||
content["daytemp_float"] = item.get("daytemp_float")
|
||||
content["daywind"] = item.get("daywind")
|
||||
content["nightweather"] = item.get("nightweather")
|
||||
content["nighttemp_float"] = item.get("nighttemp_float")
|
||||
contents.append(content)
|
||||
s.close()
|
||||
return self.create_text_message(self.summary(user_id=user_id, content=json.dumps(contents, ensure_ascii=False)))
|
||||
return self.create_text_message(
|
||||
self.summary(user_id=user_id, content=json.dumps(contents, ensure_ascii=False))
|
||||
)
|
||||
s.close()
|
||||
return self.create_text_message(f'No weather information for {city} was found.')
|
||||
return self.create_text_message(f"No weather information for {city} was found.")
|
||||
except Exception as e:
|
||||
return self.create_text_message("Gaode API Key and Api Version is invalid. {}".format(e))
|
||||
|
||||
Reference in New Issue
Block a user