mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-06-04 11:14:10 +08:00
fix: api tool encoding (#2296)
This commit is contained in:
parent
0a4dfaeaf9
commit
6d24a2cb87
@ -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)}')
|
||||
|
||||
|
@ -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'],
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user