feat: Add tools for open weather search and image generation using the Spark API. (#2845)
This commit is contained in:
12
api/core/tools/provider/builtin/openweather/_assets/icon.svg
Normal file
12
api/core/tools/provider/builtin/openweather/_assets/icon.svg
Normal file
@@ -0,0 +1,12 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" fill="none">
|
||||
<g clip-path="url(#clip0_16624_62807)">
|
||||
<path d="M7.11111 0.888889C7.11111 0.888889 7.11111 0 8 0C8.88889 0 8.88889 0.888889 8.88889 0.888889V1.77778C8.88889 1.77778 8.88889 2.66667 8 2.66667C7.11111 2.66667 7.11111 1.77778 7.11111 1.77778V0.888889ZM15.1111 7.11111C15.1111 7.11111 16 7.11111 16 8C16 8.88889 15.1111 8.88889 15.1111 8.88889H14.2222C14.2222 8.88889 13.3333 8.88889 13.3333 8C13.3333 7.11111 14.2222 7.11111 14.2222 7.11111H15.1111ZM1.77778 7.11111C1.77778 7.11111 2.66667 7.11111 2.66667 8C2.66667 8.88889 1.77778 8.88889 1.77778 8.88889H0.888889C0.888889 8.88889 0 8.88889 0 8C0 7.11111 0.888889 7.11111 0.888889 7.11111H1.77778ZM4.05378 3.24133C4.05378 3.24133 4.68222 3.86978 4.05378 4.49822C3.42533 5.12667 2.79689 4.49822 2.79689 4.49822L2.168 3.87022C2.168 3.87022 1.53956 3.24178 2.168 2.61289C2.79689 1.98444 3.42533 2.61289 3.42533 2.61289L4.05378 3.24133ZM13.2036 4.49822C13.2036 4.49822 12.5751 5.12667 11.9467 4.49822C11.3182 3.86978 11.9467 3.24133 11.9467 3.24133L12.5751 2.61289C12.5751 2.61289 13.2036 1.98444 13.832 2.61289C14.4604 3.24133 13.832 3.86978 13.832 3.86978L13.2036 4.49822ZM3.87022 13.8316C3.87022 13.8316 3.24178 14.46 2.61333 13.8316C1.98489 13.2031 2.61333 12.5747 2.61333 12.5747L3.24178 11.9462C3.24178 11.9462 3.87022 11.3178 4.49867 11.9462C5.12711 12.5747 4.49867 13.2031 4.49867 13.2031L3.87022 13.8316Z" fill="#FFCF27"/>
|
||||
<path d="M8.00011 12.4446C10.4547 12.4446 12.4446 10.4547 12.4446 8.00011C12.4446 5.54551 10.4547 3.55566 8.00011 3.55566C5.54551 3.55566 3.55566 5.54551 3.55566 8.00011C3.55566 10.4547 5.54551 12.4446 8.00011 12.4446Z" fill="#FFCB13"/>
|
||||
<path d="M13.2343 10.3111C12.949 10.3111 12.6743 10.3556 12.4152 10.4378C12.1094 9.53647 11.2774 8.88892 10.2966 8.88892C9.24411 8.88892 8.36322 9.63469 8.11922 10.6387C7.85878 10.436 7.53744 10.3116 7.18544 10.3116C6.32633 10.3116 5.62989 11.0276 5.62989 11.9116C5.62989 12.1262 5.67255 12.3298 5.74722 12.5174C5.59878 12.4742 5.44544 12.4445 5.28411 12.4445C4.32944 12.4445 3.55566 13.2405 3.55566 14.2222C3.55566 15.204 4.32944 16 5.28411 16H13.2348C14.7619 16 16.0001 14.7271 16.0001 13.1556C16.0001 11.5845 14.7619 10.3111 13.2343 10.3111Z" fill="#E9F6FF"/>
|
||||
</g>
|
||||
<defs>
|
||||
<clipPath id="clip0_16624_62807">
|
||||
<rect width="16" height="16" fill="white"/>
|
||||
</clipPath>
|
||||
</defs>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 2.4 KiB |
36
api/core/tools/provider/builtin/openweather/openweather.py
Normal file
36
api/core/tools/provider/builtin/openweather/openweather.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import requests
|
||||
|
||||
from core.tools.errors import ToolProviderCredentialValidationError
|
||||
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
|
||||
|
||||
|
||||
def query_weather(city="Beijing", units="metric", language="zh_cn", api_key=None):
|
||||
|
||||
url = "https://api.openweathermap.org/data/2.5/weather"
|
||||
params = {"q": city, "appid": api_key, "units": units, "lang": language}
|
||||
|
||||
return requests.get(url, params=params)
|
||||
|
||||
|
||||
class OpenweatherProvider(BuiltinToolProviderController):
|
||||
def _validate_credentials(self, credentials: dict) -> None:
|
||||
try:
|
||||
if "api_key" not in credentials or not credentials.get("api_key"):
|
||||
raise ToolProviderCredentialValidationError(
|
||||
"Open weather API key is required."
|
||||
)
|
||||
apikey = credentials.get("api_key")
|
||||
try:
|
||||
response = query_weather(api_key=apikey)
|
||||
if response.status_code == 200:
|
||||
pass
|
||||
else:
|
||||
raise ToolProviderCredentialValidationError(
|
||||
(response.json()).get("info")
|
||||
)
|
||||
except Exception as e:
|
||||
raise ToolProviderCredentialValidationError(
|
||||
"Open weather API Key is invalid. {}".format(e)
|
||||
)
|
||||
except Exception as e:
|
||||
raise ToolProviderCredentialValidationError(str(e))
|
||||
29
api/core/tools/provider/builtin/openweather/openweather.yaml
Normal file
29
api/core/tools/provider/builtin/openweather/openweather.yaml
Normal file
@@ -0,0 +1,29 @@
|
||||
identity:
|
||||
author: Onelevenvy
|
||||
name: openweather
|
||||
label:
|
||||
en_US: Open weather query
|
||||
zh_Hans: Open Weather
|
||||
pt_BR: Consulta de clima open weather
|
||||
description:
|
||||
en_US: Weather query toolkit based on Open Weather
|
||||
zh_Hans: 基于open weather的天气查询工具包
|
||||
pt_BR: Kit de consulta de clima baseado no Open Weather
|
||||
icon: icon.svg
|
||||
credentials_for_provider:
|
||||
api_key:
|
||||
type: secret-input
|
||||
required: true
|
||||
label:
|
||||
en_US: API Key
|
||||
zh_Hans: API Key
|
||||
pt_BR: Fogo a chave
|
||||
placeholder:
|
||||
en_US: Please enter your open weather API Key
|
||||
zh_Hans: 请输入你的open weather API Key
|
||||
pt_BR: Insira sua chave de API open weather
|
||||
help:
|
||||
en_US: Get your API Key from open weather
|
||||
zh_Hans: 从open weather获取您的 API Key
|
||||
pt_BR: Obtenha sua chave de API do open weather
|
||||
url: https://openweathermap.org
|
||||
60
api/core/tools/provider/builtin/openweather/tools/weather.py
Normal file
60
api/core/tools/provider/builtin/openweather/tools/weather.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import json
|
||||
from typing import Any, Union
|
||||
|
||||
import requests
|
||||
|
||||
from core.tools.entities.tool_entities import ToolInvokeMessage
|
||||
from core.tools.tool.builtin_tool import BuiltinTool
|
||||
|
||||
|
||||
class OpenweatherTool(BuiltinTool):
|
||||
def _invoke(
|
||||
self, user_id: str, tool_parameters: dict[str, Any]
|
||||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
|
||||
"""
|
||||
invoke tools
|
||||
"""
|
||||
city = tool_parameters.get("city", "")
|
||||
if not 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")
|
||||
):
|
||||
return self.create_text_message("OpenWeather API key is required.")
|
||||
|
||||
units = tool_parameters.get("units", "metric")
|
||||
lang = tool_parameters.get("lang", "zh_cn")
|
||||
try:
|
||||
# request URL
|
||||
url = "https://api.openweathermap.org/data/2.5/weather"
|
||||
|
||||
# request parmas
|
||||
params = {
|
||||
"q": city,
|
||||
"appid": self.runtime.credentials.get("api_key"),
|
||||
"units": units,
|
||||
"lang": lang,
|
||||
}
|
||||
response = requests.get(url, params=params)
|
||||
|
||||
if response.status_code == 200:
|
||||
|
||||
data = response.json()
|
||||
return self.create_text_message(
|
||||
self.summary(
|
||||
user_id=user_id, content=json.dumps(data, ensure_ascii=False)
|
||||
)
|
||||
)
|
||||
else:
|
||||
error_message = {
|
||||
"error": f"failed:{response.status_code}",
|
||||
"data": response.text,
|
||||
}
|
||||
# return error
|
||||
return json.dumps(error_message)
|
||||
|
||||
except Exception as e:
|
||||
return self.create_text_message(
|
||||
"Openweather API Key is invalid. {}".format(e)
|
||||
)
|
||||
@@ -0,0 +1,80 @@
|
||||
identity:
|
||||
name: weather
|
||||
author: Onelevenvy
|
||||
label:
|
||||
en_US: Open Weather Query
|
||||
zh_Hans: 天气查询
|
||||
pt_BR: Previsão do tempo
|
||||
icon: icon.svg
|
||||
description:
|
||||
human:
|
||||
en_US: Weather forecast inquiry
|
||||
zh_Hans: 天气查询
|
||||
pt_BR: Inquérito sobre previsão meteorológica
|
||||
llm: A tool when you want to ask about the weather or weather-related question
|
||||
parameters:
|
||||
- name: city
|
||||
type: string
|
||||
required: true
|
||||
label:
|
||||
en_US: city
|
||||
zh_Hans: 城市
|
||||
pt_BR: cidade
|
||||
human_description:
|
||||
en_US: Target city for weather forecast query
|
||||
zh_Hans: 天气预报查询的目标城市
|
||||
pt_BR: Cidade de destino para consulta de previsão do tempo
|
||||
llm_description: If you don't know you can extract the city name from the
|
||||
question or you can reply:Please tell me your city. You have to extract
|
||||
the Chinese city name from the question.If the input region is in Chinese
|
||||
characters for China, it should be replaced with the corresponding English
|
||||
name, such as '北京' for correct input is 'Beijing'
|
||||
form: llm
|
||||
- name: lang
|
||||
type: select
|
||||
required: true
|
||||
human_description:
|
||||
en_US: language
|
||||
zh_Hans: 语言
|
||||
pt_BR: language
|
||||
label:
|
||||
en_US: language
|
||||
zh_Hans: 语言
|
||||
pt_BR: language
|
||||
form: form
|
||||
options:
|
||||
- value: zh_cn
|
||||
label:
|
||||
en_US: cn
|
||||
zh_Hans: 中国
|
||||
pt_BR: cn
|
||||
- value: en_us
|
||||
label:
|
||||
en_US: usa
|
||||
zh_Hans: 美国
|
||||
pt_BR: usa
|
||||
default: zh_cn
|
||||
- name: units
|
||||
type: select
|
||||
required: true
|
||||
human_description:
|
||||
en_US: units for temperature
|
||||
zh_Hans: 温度单位
|
||||
pt_BR: units for temperature
|
||||
label:
|
||||
en_US: units
|
||||
zh_Hans: 单位
|
||||
pt_BR: units
|
||||
form: form
|
||||
options:
|
||||
- value: metric
|
||||
label:
|
||||
en_US: metric
|
||||
zh_Hans: ℃
|
||||
pt_BR: metric
|
||||
- value: imperial
|
||||
label:
|
||||
en_US: imperial
|
||||
zh_Hans: ℉
|
||||
pt_BR: imperial
|
||||
default: metric
|
||||
Reference in New Issue
Block a user