fix: api tool encoding (#2296)

This commit is contained in:
Yeuoly
2024-01-30 22:22:58 +08:00
committed by GitHub
parent 0a4dfaeaf9
commit 6d24a2cb87
3 changed files with 29 additions and 6 deletions

View File

@@ -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)}')

View File

@@ -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'],