Explorar el Código

fix: api tool encoding (#2296)

tags/0.5.3
Yeuoly hace 1 año
padre
commit
6d24a2cb87
No account linked to committer's email address

+ 21
- 2
api/core/tools/tool/api_tool.py Ver fichero

@@ -8,6 +8,7 @@ from core.tools.errors import ToolProviderCredentialValidationError

import httpx
import requests
import json

class ApiTool(Tool):
api_bundle: ApiBasedToolBundle
@@ -79,11 +80,29 @@ class ApiTool(Tool):
if isinstance(response, httpx.Response):
if response.status_code >= 400:
raise ToolProviderCredentialValidationError(f"Request failed with status code {response.status_code}")
return response.text
if not response.content:
return 'Empty response from the tool, please check your parameters and try again.'
try:
response = response.json()
try:
return json.dumps(response, ensure_ascii=False)
except Exception as e:
return json.dumps(response)
except Exception as e:
return response.text
elif isinstance(response, requests.Response):
if not response.ok:
raise ToolProviderCredentialValidationError(f"Request failed with status code {response.status_code}")
return response.text
if not response.content:
return 'Empty response from the tool, please check your parameters and try again.'
try:
response = response.json()
try:
return json.dumps(response, ensure_ascii=False)
except Exception as e:
return json.dumps(response)
except Exception as e:
return response.text
else:
raise ValueError(f'Invalid response type {type(response)}')

+ 4
- 0
api/core/tools/utils/parser.py Ver fichero

@@ -114,6 +114,10 @@ class ApiBasedToolSchemaParser:
if count > 1:
warning['duplicated_parameter'] = f'Parameter {name} is duplicated.'

# check if there is a operation id, use $path_$method as operation id if not
if 'operationId' not in interface['operation']:
interface['operation']['operationId'] = f'{interface["path"]}_{interface["method"]}'

bundles.append(ApiBasedToolBundle(
server_url=server_url + interface['path'],
method=interface['method'],

+ 4
- 4
api/services/tools_manage_service.py Ver fichero

@@ -485,10 +485,10 @@ class ToolManageService:
if schema_type not in [member.value for member in ApiProviderSchemaType]:
raise ValueError(f'invalid schema type {schema_type}')
if schema_type == ApiProviderSchemaType.OPENAPI.value:
tool_bundles = ApiBasedToolSchemaParser.parse_openapi_yaml_to_tool_bundle(schema)
else:
raise ValueError(f'invalid schema type {schema_type}')
try:
tool_bundles, _ = ApiBasedToolSchemaParser.auto_parse_to_tool_bundle(schema)
except Exception as e:
raise ValueError(f'invalid schema')
# get tool bundle
tool_bundle = next(filter(lambda tb: tb.operation_id == tool_name, tool_bundles), None)

Cargando…
Cancelar
Guardar