Преглед изворни кода

feat: add a Tianditu tool (#6320)

Co-authored-by: crazywoola <100913391+crazywoola@users.noreply.github.com>
listeng пре 9 месеци
родитељ
комит
d5dca46854

+ 1 - 0
api/core/tools/provider/_position.yaml

@@ -30,3 +30,4 @@
 - feishu
 - feishu_base
 - slack
+- tianditu

Разлика између датотеке није приказан због своје велике величине
+ 6 - 0
api/core/tools/provider/builtin/tianditu/_assets/icon.svg


+ 21 - 0
api/core/tools/provider/builtin/tianditu/tianditu.py

@@ -0,0 +1,21 @@
+from typing import Any
+
+from core.tools.errors import ToolProviderCredentialValidationError
+from core.tools.provider.builtin.tianditu.tools.poisearch import PoiSearchTool
+from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
+
+
+class TiandituProvider(BuiltinToolProviderController):
+    def _validate_credentials(self, credentials: dict[str, Any]) -> None:
+        try:
+            PoiSearchTool().fork_tool_runtime(
+                runtime={
+                    "credentials": credentials,
+                }
+            ).invoke(user_id='',
+                        tool_parameters={
+                        'content': '北京',
+                        'specify': '156110000',
+                    })
+        except Exception as e:
+            raise ToolProviderCredentialValidationError(str(e))

+ 32 - 0
api/core/tools/provider/builtin/tianditu/tianditu.yaml

@@ -0,0 +1,32 @@
+identity:
+  author: Listeng
+  name: tianditu
+  label:
+    en_US: Tianditu
+    zh_Hans: 天地图
+    pt_BR: Tianditu
+  description:
+    en_US: The Tianditu tool provided the functions of place name search, geocoding, static maps generation, etc. in China region.
+    zh_Hans: 天地图工具可以调用天地图的接口,实现中国区域内的地名搜索、地理编码、静态地图等功能。
+    pt_BR: The Tianditu tool provided the functions of place name search, geocoding, static maps generation, etc. in China region.
+  icon: icon.svg
+  tags:
+    - utilities
+    - travel
+credentials_for_provider:
+  tianditu_api_key:
+    type: secret-input
+    required: true
+    label:
+      en_US: Tianditu API Key
+      zh_Hans: 天地图Key
+      pt_BR: Tianditu API key
+    placeholder:
+      en_US: Please input your Tianditu API key
+      zh_Hans: 请输入你的天地图Key
+      pt_BR: Please input your Tianditu API key
+    help:
+      en_US: Get your Tianditu API key from Tianditu
+      zh_Hans: 获取您的天地图Key
+      pt_BR: Get your Tianditu API key from Tianditu
+    url: http://lbs.tianditu.gov.cn/home.html

+ 33 - 0
api/core/tools/provider/builtin/tianditu/tools/geocoder.py

@@ -0,0 +1,33 @@
+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 GeocoderTool(BuiltinTool):
+    
+    def _invoke(self,
+                user_id: str,
+                tool_parameters: dict[str, Any],
+                ) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
+        """
+            invoke tools
+        """
+        base_url = 'http://api.tianditu.gov.cn/geocoder'
+        
+        keyword = tool_parameters.get('keyword', '')
+        if not keyword:
+            return self.create_text_message('Invalid parameter keyword')
+        
+        tk = self.runtime.credentials['tianditu_api_key']
+        
+        params = {
+            'keyWord': keyword,
+        }
+        
+        result = requests.get(base_url + '?ds=' + json.dumps(params, ensure_ascii=False) + '&tk=' + tk).json()
+
+        return self.create_json_message(result)

+ 26 - 0
api/core/tools/provider/builtin/tianditu/tools/geocoder.yaml

@@ -0,0 +1,26 @@
+identity:
+  name: geocoder
+  author: Listeng
+  label:
+    en_US: Get coords converted from address name
+    zh_Hans: 地理编码
+    pt_BR: Get coords converted from address name
+description:
+  human:
+    en_US: Geocoder
+    zh_Hans: 中国区域地理编码查询
+    pt_BR: Geocoder
+  llm: A tool for geocoder in China
+parameters:
+  - name: keyword
+    type: string
+    required: true
+    label:
+      en_US: keyword
+      zh_Hans: 搜索的关键字
+      pt_BR: keyword
+    human_description:
+      en_US: keyword
+      zh_Hans: 搜索的关键字
+      pt_BR: keyword
+    form: llm

+ 45 - 0
api/core/tools/provider/builtin/tianditu/tools/poisearch.py

@@ -0,0 +1,45 @@
+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 PoiSearchTool(BuiltinTool):
+    
+    def _invoke(self,
+                user_id: str,
+                tool_parameters: dict[str, Any],
+                ) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
+        """
+            invoke tools
+        """
+        geocoder_base_url = 'http://api.tianditu.gov.cn/geocoder'
+        base_url = 'http://api.tianditu.gov.cn/v2/search'
+        
+        keyword = tool_parameters.get('keyword', '')
+        if not keyword:
+            return self.create_text_message('Invalid parameter keyword')
+        
+        baseAddress = tool_parameters.get('baseAddress', '')
+        if not baseAddress:
+            return self.create_text_message('Invalid parameter baseAddress')
+        
+        tk = self.runtime.credentials['tianditu_api_key']
+        
+        base_coords = requests.get(geocoder_base_url + '?ds=' + json.dumps({'keyWord': baseAddress,}, ensure_ascii=False) + '&tk=' + tk).json()
+        
+        params = {
+            'keyWord': keyword,
+            'queryRadius': 5000,
+            'queryType': 3,
+            'pointLonlat': base_coords['location']['lon'] + ',' + base_coords['location']['lat'],
+            'start': 0,
+            'count': 100,
+        }
+        
+        result = requests.get(base_url + '?postStr=' + json.dumps(params, ensure_ascii=False) + '&type=query&tk=' + tk).json()
+
+        return self.create_json_message(result)

+ 38 - 0
api/core/tools/provider/builtin/tianditu/tools/poisearch.yaml

@@ -0,0 +1,38 @@
+identity:
+  name: point_of_interest_search
+  author: Listeng
+  label:
+    en_US: Point of Interest search
+    zh_Hans: 兴趣点搜索
+    pt_BR: Point of Interest search
+description:
+  human:
+    en_US: Search for certain types of points of interest around a location
+    zh_Hans: 搜索某个位置周边的5公里内某种类型的兴趣点
+    pt_BR: Search for certain types of points of interest around a location
+  llm: A tool for searching for certain types of points of interest around a location
+parameters:
+  - name: keyword
+    type: string
+    required: true
+    label:
+      en_US: poi keyword
+      zh_Hans: 兴趣点的关键字
+      pt_BR: poi keyword
+    human_description:
+      en_US: poi keyword
+      zh_Hans: 兴趣点的关键字
+      pt_BR: poi keyword
+    form: llm
+  - name: baseAddress
+    type: string
+    required: true
+    label:
+      en_US: base current point
+      zh_Hans: 当前位置的关键字
+      pt_BR: base current point
+    human_description:
+      en_US: base current point
+      zh_Hans: 当前位置的关键字
+      pt_BR: base current point
+    form: llm

+ 36 - 0
api/core/tools/provider/builtin/tianditu/tools/staticmap.py

@@ -0,0 +1,36 @@
+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 PoiSearchTool(BuiltinTool):
+    
+    def _invoke(self,
+                user_id: str,
+                tool_parameters: dict[str, Any],
+                ) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
+        """
+            invoke tools
+        """
+        
+        geocoder_base_url = 'http://api.tianditu.gov.cn/geocoder'
+        base_url = 'http://api.tianditu.gov.cn/staticimage'
+        
+        keyword = tool_parameters.get('keyword', '')
+        if not keyword:
+            return self.create_text_message('Invalid parameter keyword')
+        
+        tk = self.runtime.credentials['tianditu_api_key']
+        
+        keyword_coords = requests.get(geocoder_base_url + '?ds=' + json.dumps({'keyWord': keyword,}, ensure_ascii=False) + '&tk=' + tk).json()
+        coords = keyword_coords['location']['lon'] + ',' + keyword_coords['location']['lat']
+        
+        result = requests.get(base_url + '?center=' + coords + '&markers=' + coords + '&width=400&height=300&zoom=14&tk=' + tk).content
+
+        return self.create_blob_message(blob=result,
+                                            meta={'mime_type': 'image/png'},
+                                            save_as=self.VARIABLE_KEY.IMAGE.value)

+ 26 - 0
api/core/tools/provider/builtin/tianditu/tools/staticmap.yaml

@@ -0,0 +1,26 @@
+identity:
+  name: generate_static_map
+  author: Listeng
+  label:
+    en_US: Generate a static map
+    zh_Hans: 生成静态地图
+    pt_BR: Generate a static map
+description:
+  human:
+    en_US: Generate a static map
+    zh_Hans: 生成静态地图
+    pt_BR: Generate a static map
+  llm: A tool for generate a static map
+parameters:
+  - name: keyword
+    type: string
+    required: true
+    label:
+      en_US: keyword
+      zh_Hans: 搜索的关键字
+      pt_BR: keyword
+    human_description:
+      en_US: keyword
+      zh_Hans: 搜索的关键字
+      pt_BR: keyword
+    form: llm

Неке датотеке нису приказане због велике количине промена