Spaces:
Sleeping
Sleeping
File size: 15,517 Bytes
09321b6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
import os
import re
from typing import List, Optional
import json
import requests
from jsonschema import RefResolver
from pydantic import BaseModel, ValidationError
from requests.exceptions import RequestException, Timeout
from .tool import Tool
MAX_RETRY_TIMES = 3
class ParametersSchema(BaseModel):
name: str
description: str
required: Optional[bool] = True
class ToolSchema(BaseModel):
name: str
description: str
parameters: List[ParametersSchema]
class OpenAPIPluginTool(Tool):
"""
openapi schema tool
"""
name: str = 'api tool'
description: str = 'This is a api tool that ...'
parameters: list = []
def __init__(self, cfg, name):
self.name = name
self.cfg = cfg.get(self.name, {})
self.is_remote_tool = self.cfg.get('is_remote_tool', False)
# remote call
self.url = self.cfg.get('url', '')
self.token = self.cfg.get('token', '')
self.header = self.cfg.get('header', '')
self.method = self.cfg.get('method', '')
self.parameters = self.cfg.get('parameters', [])
self.description = self.cfg.get('description',
'This is a api tool that ...')
self.responses_param = self.cfg.get('responses_param', [])
try:
all_para = {
'name': self.name,
'description': self.description,
'parameters': self.parameters
}
self.tool_schema = ToolSchema(**all_para)
except ValidationError:
raise ValueError(f'Error when parsing parameters of {self.name}')
self._str = self.tool_schema.model_dump_json()
self._function = self.parse_pydantic_model_to_openai_function(all_para)
def _remote_call(self, *args, **kwargs):
if self.url == '':
raise ValueError(
f"Could not use remote call for {self.name} since this tool doesn't have a remote endpoint"
)
remote_parsed_input = json.dumps(
self._remote_parse_input(*args, **kwargs))
origin_result = None
if self.method == 'POST':
retry_times = MAX_RETRY_TIMES
while retry_times:
retry_times -= 1
try:
print(f'data: {kwargs}')
print(f'header: {self.header}')
response = requests.request(
'POST',
url=self.url,
headers=self.header,
data=remote_parsed_input)
if response.status_code != requests.codes.ok:
response.raise_for_status()
origin_result = json.loads(
response.content.decode('utf-8'))
final_result = self._parse_output(
origin_result, remote=True)
return final_result
except Timeout:
continue
except RequestException as e:
raise ValueError(
f'Remote call failed with error code: {e.response.status_code},\
error message: {e.response.content.decode("utf-8")}')
raise ValueError(
'Remote call max retry times exceeded! Please try to use local call.'
)
elif self.method == 'GET':
retry_times = MAX_RETRY_TIMES
new_url = self.url
matches = re.findall(r'\{(.*?)\}', self.url)
for match in matches:
if match in kwargs:
new_url = new_url.replace('{' + match + '}', kwargs[match])
else:
print(
f'The parameter {match} was not generated by the model.'
)
while retry_times:
retry_times -= 1
try:
print('GET:', new_url)
print('GET:', self.url)
response = requests.request(
'GET',
url=new_url,
headers=self.header,
params=remote_parsed_input)
if response.status_code != requests.codes.ok:
response.raise_for_status()
origin_result = json.loads(
response.content.decode('utf-8'))
final_result = self._parse_output(
origin_result, remote=True)
return final_result
except Timeout:
continue
except RequestException as e:
raise ValueError(
f'Remote call failed with error code: {e.response.status_code},\
error message: {e.response.content.decode("utf-8")}')
raise ValueError(
'Remote call max retry times exceeded! Please try to use local call.'
)
else:
raise ValueError(
'Remote call method is invalid!We have POST and GET method.')
def _remote_parse_input(self, *args, **kwargs):
restored_dict = {}
for key, value in kwargs.items():
if '.' in key:
# Split keys by "." and create nested dictionary structures
keys = key.split('.')
temp_dict = restored_dict
for k in keys[:-1]:
temp_dict = temp_dict.setdefault(k, {})
temp_dict[keys[-1]] = value
else:
# f the key does not contain ".", directly store the key-value pair into restored_dict
restored_dict[key] = value
kwargs = restored_dict
print('传给tool的参数:', kwargs)
return kwargs
# openapi_schema_convert,register to tool_config.json
def extract_references(schema_content):
references = []
if isinstance(schema_content, dict):
if '$ref' in schema_content:
references.append(schema_content['$ref'])
for key, value in schema_content.items():
references.extend(extract_references(value))
elif isinstance(schema_content, list):
for item in schema_content:
references.extend(extract_references(item))
return references
def parse_nested_parameters(param_name, param_info, parameters_list, content):
param_type = param_info['type']
param_description = param_info.get('description',
f'用户输入的{param_name}') # 按需更改描述
param_required = param_name in content['required']
try:
if param_type == 'object':
properties = param_info.get('properties')
if properties:
# If the argument type is an object and has a non-empty "properties" field,
# its internal properties are parsed recursively
for inner_param_name, inner_param_info in properties.items():
inner_param_type = inner_param_info['type']
inner_param_description = inner_param_info.get(
'description', f'用户输入的{param_name}.{inner_param_name}')
inner_param_required = param_name.split(
'.')[0] in content['required']
# Recursively call the function to handle nested objects
if inner_param_type == 'object':
parse_nested_parameters(
f'{param_name}.{inner_param_name}',
inner_param_info, parameters_list, content)
else:
parameters_list.append({
'name':
f'{param_name}.{inner_param_name}',
'description':
inner_param_description,
'required':
inner_param_required,
'type':
inner_param_type,
'value':
inner_param_info.get('enum', '')
})
else:
# Non-nested parameters are added directly to the parameter list
parameters_list.append({
'name': param_name,
'description': param_description,
'required': param_required,
'type': param_type,
'value': param_info.get('enum', '')
})
except Exception as e:
raise ValueError(f'{e}:schema结构出错')
def parse_responses_parameters(param_name, param_info, parameters_list):
param_type = param_info['type']
param_description = param_info.get('description',
f'调用api返回的{param_name}') # 按需更改描述
try:
if param_type == 'object':
properties = param_info.get('properties')
if properties:
# If the argument type is an object and has a non-empty "properties"
# field, its internal properties are parsed recursively
for inner_param_name, inner_param_info in properties.items():
param_type = inner_param_info['type']
param_description = inner_param_info.get(
'description',
f'调用api返回的{param_name}.{inner_param_name}')
parameters_list.append({
'name': f'{param_name}.{inner_param_name}',
'description': param_description,
'type': param_type,
})
else:
# Non-nested parameters are added directly to the parameter list
parameters_list.append({
'name': param_name,
'description': param_description,
'type': param_type,
})
except Exception as e:
raise ValueError(f'{e}:schema结构出错')
def openapi_schema_convert(schema, auth):
resolver = RefResolver.from_schema(schema)
servers = schema.get('servers', [])
if servers:
servers_url = servers[0].get('url')
else:
print('No URL found in the schema.')
# Extract endpoints
endpoints = schema.get('paths', {})
description = schema.get('info', {}).get('description',
'This is a api tool that ...')
config_data = {}
# Iterate over each endpoint and its contents
for endpoint_path, methods in endpoints.items():
for method, details in methods.items():
summary = details.get('summary', 'No summary').replace(' ', '_')
name = details.get('operationId', 'No operationId')
url = f'{servers_url}{endpoint_path}'
security = details.get('security', [{}])
# Security (Bearer Token)
authorization = ''
if security:
for sec in security:
if 'BearerAuth' in sec:
api_token = auth.get('apikey', os.environ['apikey'])
api_token_type = auth.get('apikey_type',
os.environ['apikey_type'])
authorization = f'{api_token_type} {api_token}'
if method.upper() == 'POST':
requestBody = details.get('requestBody', {})
if requestBody:
for content_type, content_details in requestBody.get(
'content', {}).items():
schema_content = content_details.get('schema', {})
references = extract_references(schema_content)
for reference in references:
resolved_schema = resolver.resolve(reference)
content = resolved_schema[1]
parameters_list = []
for param_name, param_info in content[
'properties'].items():
parse_nested_parameters(
param_name, param_info, parameters_list,
content)
X_DashScope_Async = requestBody.get(
'X-DashScope-Async', '')
if X_DashScope_Async == '':
config_entry = {
'name': name,
'description': description,
'is_active': True,
'is_remote_tool': True,
'url': url,
'method': method.upper(),
'parameters': parameters_list,
'header': {
'Content-Type': content_type,
'Authorization': authorization
}
}
else:
config_entry = {
'name': name,
'description': description,
'is_active': True,
'is_remote_tool': True,
'url': url,
'method': method.upper(),
'parameters': parameters_list,
'header': {
'Content-Type': content_type,
'Authorization': authorization,
'X-DashScope-Async': 'enable'
}
}
else:
config_entry = {
'name': name,
'description': description,
'is_active': True,
'is_remote_tool': True,
'url': url,
'method': method.upper(),
'parameters': [],
'header': {
'Content-Type': 'application/json',
'Authorization': authorization
}
}
elif method.upper() == 'GET':
parameters_list = []
parameters_list = details.get('parameters', [])
config_entry = {
'name': name,
'description': description,
'is_active': True,
'is_remote_tool': True,
'url': url,
'method': method.upper(),
'parameters': parameters_list,
'header': {
'Authorization': authorization
}
}
else:
raise 'method is not POST or GET'
config_data[summary] = config_entry
return config_data
|