byteplus-python-sdk-v2 3.0.59__py2.py3-none-any.whl → 3.0.60__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. {byteplus_python_sdk_v2-3.0.59.dist-info → byteplus_python_sdk_v2-3.0.60.dist-info}/METADATA +3 -1
  2. {byteplus_python_sdk_v2-3.0.59.dist-info → byteplus_python_sdk_v2-3.0.60.dist-info}/RECORD +88 -53
  3. byteplussdkaidap/__init__.py +35 -0
  4. byteplussdkaidap/api/aidap_api.py +1177 -13
  5. byteplussdkaidap/models/__init__.py +35 -0
  6. byteplussdkaidap/models/bind_pages_project_request.py +230 -0
  7. byteplussdkaidap/models/bind_pages_project_response.py +95 -0
  8. byteplussdkaidap/models/binding_for_describe_pages_binding_output.py +253 -0
  9. byteplussdkaidap/models/branch_settings_for_create_branch_input.py +29 -3
  10. byteplussdkaidap/models/branch_usage_for_create_branch_output.py +79 -1
  11. byteplussdkaidap/models/branch_usage_for_describe_branch_detail_output.py +79 -1
  12. byteplussdkaidap/models/branch_usage_for_describe_branches_list_output.py +79 -1
  13. byteplussdkaidap/models/branch_usage_for_describe_branches_output.py +79 -1
  14. byteplussdkaidap/models/branch_usage_for_describe_child_branches_output.py +79 -1
  15. byteplussdkaidap/models/branch_usage_for_describe_default_branch_list_output.py +79 -1
  16. byteplussdkaidap/models/branch_usage_for_describe_default_branch_output.py +79 -1
  17. byteplussdkaidap/models/branch_usage_for_describe_restorable_branches_output.py +79 -1
  18. byteplussdkaidap/models/branch_usage_for_reset_branch_output.py +79 -1
  19. byteplussdkaidap/models/branch_usage_for_set_as_default_branch_output.py +79 -1
  20. byteplussdkaidap/models/branch_usage_for_update_branch_output.py +79 -1
  21. byteplussdkaidap/models/create_sql_concurrency_control_rule_request.py +442 -0
  22. byteplussdkaidap/models/create_sql_concurrency_control_rule_response.py +175 -0
  23. byteplussdkaidap/models/custom_quota_for_describe_baas_ai_user_summaries_output.py +27 -1
  24. byteplussdkaidap/models/custom_quota_for_modify_baas_ai_user_quotas_input.py +27 -1
  25. byteplussdkaidap/models/delete_sql_concurrency_control_rule_request.py +238 -0
  26. byteplussdkaidap/models/delete_sql_concurrency_control_rule_response.py +149 -0
  27. byteplussdkaidap/models/describe_pages_binding_request.py +151 -0
  28. byteplussdkaidap/models/describe_pages_binding_response.py +149 -0
  29. byteplussdkaidap/models/describe_sql_concurrency_control_rules_request.py +466 -0
  30. byteplussdkaidap/models/describe_sql_concurrency_control_rules_response.py +201 -0
  31. byteplussdkaidap/models/describe_workspaces_request.py +27 -1
  32. byteplussdkaidap/models/describe_workspaces_response.py +53 -1
  33. byteplussdkaidap/models/limit_config_for_create_sql_concurrency_control_rule_input.py +260 -0
  34. byteplussdkaidap/models/limit_config_for_modify_sql_concurrency_control_rule_input.py +260 -0
  35. byteplussdkaidap/models/list_pages_project_request.py +227 -0
  36. byteplussdkaidap/models/list_pages_project_response.py +201 -0
  37. byteplussdkaidap/models/match_for_create_sql_concurrency_control_rule_input.py +182 -0
  38. byteplussdkaidap/models/modify_compute_parameters_request.py +243 -0
  39. byteplussdkaidap/models/modify_compute_parameters_response.py +95 -0
  40. byteplussdkaidap/models/modify_compute_spec_request.py +34 -1
  41. byteplussdkaidap/models/modify_sql_concurrency_control_rule_request.py +264 -0
  42. byteplussdkaidap/models/modify_sql_concurrency_control_rule_response.py +149 -0
  43. byteplussdkaidap/models/pages_project_for_describe_pages_binding_output.py +357 -0
  44. byteplussdkaidap/models/parameter_for_modify_compute_parameters_input.py +149 -0
  45. byteplussdkaidap/models/project_for_list_pages_project_output.py +357 -0
  46. byteplussdkaidap/models/restore_settings_for_branch_restore_input.py +27 -1
  47. byteplussdkaidap/models/rule_for_describe_sql_concurrency_control_rules_output.py +565 -0
  48. byteplussdkaidap/models/start_workspace_request.py +27 -1
  49. byteplussdkaidap/models/supabase_binding_for_describe_pages_binding_output.py +253 -0
  50. byteplussdkaidap/models/supabase_binding_for_list_pages_project_output.py +253 -0
  51. byteplussdkaidap/models/suspend_workspace_request.py +124 -0
  52. byteplussdkaidap/models/suspend_workspace_response.py +95 -0
  53. byteplussdkaidap/models/switch_sql_concurrency_control_rule_request.py +265 -0
  54. byteplussdkaidap/models/switch_sql_concurrency_control_rule_response.py +149 -0
  55. byteplussdkaidap/models/sync_pages_deploy_env_vars_request.py +178 -0
  56. byteplussdkaidap/models/sync_pages_deploy_env_vars_response.py +95 -0
  57. byteplussdkaidap/models/unbind_pages_project_request.py +151 -0
  58. byteplussdkaidap/models/unbind_pages_project_response.py +95 -0
  59. byteplussdkaidap/models/usage_for_describe_baas_ai_user_summaries_output.py +53 -1
  60. byteplussdkaidap/models/user_default_quota_for_describe_baas_ai_global_quota_output.py +27 -1
  61. byteplussdkaidap/models/user_default_quota_for_modify_baas_ai_global_quota_input.py +27 -1
  62. byteplussdkaidap/models/volc_supabase_filter_for_list_pages_project_input.py +149 -0
  63. byteplussdkaidap/models/workspace_usage_for_create_workspace_output.py +81 -3
  64. byteplussdkaidap/models/workspace_usage_for_describe_workspace_detail_list_output.py +81 -3
  65. byteplussdkaidap/models/workspace_usage_for_describe_workspace_detail_output.py +81 -3
  66. byteplussdkaidap/models/workspace_usage_for_describe_workspaces_output.py +81 -3
  67. byteplussdkaidap/models/workspace_usage_for_list_workspace_usage_top_output.py +81 -3
  68. byteplussdkaidap/models/workspace_usage_for_modify_compute_settings_output.py +81 -3
  69. byteplussdkaidap/models/workspace_usage_for_modify_workspace_name_output.py +81 -3
  70. byteplussdkaidap/models/workspace_usage_for_modify_workspace_settings_output.py +81 -3
  71. byteplussdkcore/api_client.py +18 -14
  72. byteplussdkcore/auth/providers/sts_provider.py +0 -2
  73. byteplussdkcore/configuration.py +91 -21
  74. byteplussdkcore/interceptor/chain.py +23 -0
  75. byteplussdkcore/interceptor/interceptors/build_request_interceptor.py +2 -0
  76. byteplussdkcore/interceptor/interceptors/interceptor.py +2 -0
  77. byteplussdkcore/interceptor/interceptors/request.py +7 -2
  78. byteplussdkcore/interceptor/interceptors/resolve_endpoint_interceptor.py +2 -0
  79. byteplussdkcore/interceptor/interceptors/runtime_options_interceptor.py +28 -15
  80. byteplussdkcore/interceptor/interceptors/sign_request_interceptor.py +20 -0
  81. byteplussdkcore/retryer/retryer.py +89 -10
  82. byteplussdkllmshield/__init__.py +8 -0
  83. byteplussdkllmshield/api/llm_shield_sdk_v2.py +53 -5
  84. byteplussdkllmshield/models/llm_shield_sign.py +3 -3
  85. {byteplus_python_sdk_v2-3.0.59.dist-info → byteplus_python_sdk_v2-3.0.60.dist-info}/WHEEL +0 -0
  86. {byteplus_python_sdk_v2-3.0.59.dist-info → byteplus_python_sdk_v2-3.0.60.dist-info}/licenses/LICENSE.txt +0 -0
  87. {byteplus_python_sdk_v2-3.0.59.dist-info → byteplus_python_sdk_v2-3.0.60.dist-info}/licenses/NOTICE.md +0 -0
  88. {byteplus_python_sdk_v2-3.0.59.dist-info → byteplus_python_sdk_v2-3.0.60.dist-info}/top_level.txt +0 -0
@@ -2,6 +2,14 @@
2
2
  from .interceptors import RequestInterceptor, ResponseInterceptor
3
3
 
4
4
 
5
+ _RETRY_AUTH_HEADERS = frozenset((
6
+ 'authorization',
7
+ 'x-content-sha256',
8
+ 'x-date',
9
+ 'x-security-token',
10
+ ))
11
+
12
+
5
13
  def check_request_interceptor(interceptor):
6
14
  if not issubclass(interceptor.__class__, RequestInterceptor):
7
15
  raise Exception("interceptor is not for request")
@@ -56,6 +64,21 @@ class InterceptorChain:
56
64
 
57
65
  return context
58
66
 
67
+ def execute_retry(self, context):
68
+ request = context.request
69
+ if request.auth_settings and not request.is_presign:
70
+ headers = request.header_params
71
+ if headers:
72
+ for key in list(headers):
73
+ if key.lower() in _RETRY_AUTH_HEADERS:
74
+ del headers[key]
75
+
76
+ for interceptor in self.request_interceptors:
77
+ if getattr(interceptor, 'run_on_retry', True):
78
+ context = interceptor.intercept(context)
79
+
80
+ return context
81
+
59
82
  def execute_response(self, context):
60
83
  for interceptor in self.response_interceptors:
61
84
  context = interceptor.intercept(context)
@@ -113,6 +113,8 @@ def sanitize_for_serialization(obj):
113
113
 
114
114
  class BuildRequestInterceptor(RequestInterceptor):
115
115
 
116
+ run_on_retry = False
117
+
116
118
  def name(self):
117
119
  return 'byteplus-build-request-interceptor'
118
120
 
@@ -4,6 +4,8 @@ import abc
4
4
 
5
5
  class RequestInterceptor(object):
6
6
 
7
+ run_on_retry = True
8
+
7
9
  @abc.abstractmethod
8
10
  def name(self):
9
11
  raise NotImplementedError()
@@ -1,3 +1,6 @@
1
+ import uuid
2
+
3
+
1
4
  class RuntimeOption(object):
2
5
  def __init__(self, client_side_validation, ak=None, sk=None, session_token=None, region=None, scheme=None,
3
6
  endpoint_provider=None, connect_timeout=None, read_timeout=None, auto_retry=None, num_max_retries=None,
@@ -58,10 +61,12 @@ class Request(object):
58
61
  self.custom_bootstrap_region = configuration.custom_bootstrap_region
59
62
  self.use_dual_stack = configuration.use_dual_stack
60
63
 
61
- # retryer setting, default use global configration if value not set
64
+ # ApiClient injects its private base retryer before interception.
62
65
  self.auto_retry = configuration.auto_retry
63
- self.retryer = configuration.retryer
66
+ self.retryer = None
64
67
  self.credential_provider = configuration.credential_provider
68
+ self.invocation_id = str(uuid.uuid4())
69
+ self.retry_count = 0
65
70
 
66
71
  # Presign support
67
72
  self.is_presign = False
@@ -4,6 +4,8 @@ from byteplussdkcore.observability.debugger import sdk_core_logger
4
4
 
5
5
  class ResolveEndpointInterceptor(RequestInterceptor):
6
6
 
7
+ run_on_retry = False
8
+
7
9
  def name(self):
8
10
  return 'byteplus-resolve-endpoint-interceptor'
9
11
 
@@ -2,11 +2,15 @@
2
2
 
3
3
  from urllib3 import Timeout
4
4
 
5
+ from byteplussdkcore.retryer.retryer import new_retryer
6
+
5
7
  from .interceptor import RequestInterceptor
6
8
 
7
9
 
8
10
  class RuntimeOptionsInterceptor(RequestInterceptor):
9
11
 
12
+ run_on_retry = False
13
+
10
14
  def name(self):
11
15
  return 'byteplus-runtime-options-interceptor'
12
16
 
@@ -39,19 +43,28 @@ class RuntimeOptionsInterceptor(RequestInterceptor):
39
43
 
40
44
  @staticmethod
41
45
  def __update_retryer(context, opt):
42
- min_retry_delay_ms = opt.min_retry_delay_ms \
43
- if opt.min_retry_delay_ms is not None else context.request.retryer.backoff_strategy.min_retry_delay_ms
44
- max_retry_delay_ms = opt.max_retry_delay_ms \
45
- if opt.max_retry_delay_ms is not None else context.request.retryer.backoff_strategy.max_retry_delay_ms
46
- retry_error_codes = opt.retry_error_codes \
47
- if opt.retry_error_codes is not None else context.request.retryer.retry_condition.retry_error_codes
48
46
  context.request.auto_retry = opt.auto_retry if opt.auto_retry is not None else context.request.auto_retry
49
- context.request.retryer.num_max_retries = opt.num_max_retries \
50
- if opt.num_max_retries is not None else context.request.retryer.num_max_retries
51
- context.request.retryer.backoff_strategy = opt.backoff_strategy \
52
- if opt.backoff_strategy is not None else context.request.retryer.backoff_strategy
53
- context.request.retryer.backoff_strategy.min_retry_delay_ms = min_retry_delay_ms
54
- context.request.retryer.backoff_strategy.max_retry_delay_ms = max_retry_delay_ms
55
- context.request.retryer.retry_condition = opt.retry_condition \
56
- if opt.retry_condition is not None else context.request.retryer.retry_condition
57
- context.request.retryer.retry_condition.retry_error_codes = retry_error_codes
47
+
48
+ retry_fields = (
49
+ opt.num_max_retries,
50
+ opt.backoff_strategy,
51
+ opt.retry_condition,
52
+ opt.retry_error_codes,
53
+ opt.min_retry_delay_ms,
54
+ opt.max_retry_delay_ms,
55
+ )
56
+ if all(value is None for value in retry_fields):
57
+ return
58
+
59
+ base_retryer = context.request.retryer
60
+ context.request.retryer = new_retryer(
61
+ num_max_retries=(opt.num_max_retries
62
+ if opt.num_max_retries is not None else base_retryer.num_max_retries),
63
+ backoff_strategy=(opt.backoff_strategy
64
+ if opt.backoff_strategy is not None else base_retryer.backoff_strategy),
65
+ retry_condition=(opt.retry_condition
66
+ if opt.retry_condition is not None else base_retryer.retry_condition),
67
+ retry_error_codes=opt.retry_error_codes,
68
+ min_retry_delay_ms=opt.min_retry_delay_ms,
69
+ max_retry_delay_ms=opt.max_retry_delay_ms,
70
+ )
@@ -10,10 +10,23 @@ _DEFAULT_PROVIDER_INIT_LOCK = threading.Lock()
10
10
 
11
11
  class SignRequestInterceptor(RequestInterceptor):
12
12
 
13
+ run_on_retry = True
14
+
15
+ RETRY_INVOCATION_ID_HEADER = 'X-Sdk-Invocation-Id'
16
+ RETRY_ATTEMPT_HEADER = 'X-Sdk-Request'
17
+
13
18
  def name(self):
14
19
  return 'byteplus-sign-request-interceptor'
15
20
 
16
21
  def intercept(self, context):
22
+ if not context.request.is_presign:
23
+ max_attempts = context.request.retryer.num_max_retries + 1 \
24
+ if context.request.auto_retry and context.request.retryer is not None else 1
25
+ self._set_header(context.request.header_params, self.RETRY_INVOCATION_ID_HEADER,
26
+ context.request.invocation_id)
27
+ self._set_header(context.request.header_params, self.RETRY_ATTEMPT_HEADER,
28
+ 'attempt={}; max={}'.format(context.request.retry_count + 1, max_attempts))
29
+
17
30
  # Auto-inject DefaultCredentialProvider when no credentials are configured
18
31
  if context.request.credential_provider is None and not context.request.ak and not context.request.sk:
19
32
  from byteplussdkcore.auth.providers.default_provider import DefaultCredentialProvider
@@ -60,6 +73,13 @@ class SignRequestInterceptor(RequestInterceptor):
60
73
  region=context.request.region)
61
74
  return context
62
75
 
76
+ @staticmethod
77
+ def _set_header(headers, name, value):
78
+ for key in list(headers):
79
+ if key.lower() == name.lower() and key != name:
80
+ del headers[key]
81
+ headers[name] = value
82
+
63
83
  @staticmethod
64
84
  def update_params_for_auth(host, path, method, headers, querys, auth_settings, body, post_params, service, ak,
65
85
  sk, session_token, region):
@@ -1,29 +1,106 @@
1
1
  # coding: utf-8
2
+ import copy
3
+
2
4
  try:
3
5
  from typing import TYPE_CHECKING
4
6
  except ImportError:
5
7
  TYPE_CHECKING = False
6
- from byteplussdkcore.retryer.backoff_strategy import ExponentialWithRandomJitterBackoffStrategy
8
+ from byteplussdkcore.retryer.backoff_strategy import (
9
+ ExponentialBackoffStrategy,
10
+ ExponentialWithRandomJitterBackoffStrategy,
11
+ NoBackoffStrategy,
12
+ )
7
13
  from byteplussdkcore.retryer.retry_condition import DefaultRetryCondition
8
14
  if TYPE_CHECKING:
9
15
  from byteplussdkcore.rest import RESTResponse
10
16
  from byteplussdkcore.retryer.backoff_strategy import BackoffStrategy
11
17
  from byteplussdkcore.retryer.retry_condition import RetryCondition
12
18
 
13
- _DEFAULT_BACKOFF_STRATEGY = ExponentialWithRandomJitterBackoffStrategy(
14
- min_retry_delay_ms=300,
15
- max_retry_delay_ms=300 * 1000,
19
+ DEFAULT_NUM_MAX_RETRIES = 3
20
+ DEFAULT_MIN_RETRY_DELAY_MS = 300
21
+ DEFAULT_MAX_RETRY_DELAY_MS = 300 * 1000
22
+ _UNSET = object()
23
+
24
+ _BUILT_IN_BACKOFF_STRATEGIES = (
25
+ NoBackoffStrategy,
26
+ ExponentialBackoffStrategy,
27
+ ExponentialWithRandomJitterBackoffStrategy,
16
28
  )
17
29
 
18
- _DEFAULT_RETRY_CONDITION = DefaultRetryCondition(retry_error_codes=set())
30
+
31
+ def new_backoff_strategy(backoff_strategy=_UNSET, min_retry_delay_ms=None, max_retry_delay_ms=None):
32
+ if backoff_strategy is _UNSET:
33
+ strategy_type = ExponentialWithRandomJitterBackoffStrategy
34
+ current_min_retry_delay_ms = DEFAULT_MIN_RETRY_DELAY_MS
35
+ current_max_retry_delay_ms = DEFAULT_MAX_RETRY_DELAY_MS
36
+ elif type(backoff_strategy) in _BUILT_IN_BACKOFF_STRATEGIES:
37
+ strategy_type = type(backoff_strategy)
38
+ current_min_retry_delay_ms = backoff_strategy.min_retry_delay_ms
39
+ current_max_retry_delay_ms = backoff_strategy.max_retry_delay_ms
40
+ else:
41
+ # Custom strategy (including subclasses of the built-in ones): keep the
42
+ # caller's instance as-is unless scalar overrides are requested, in which
43
+ # case apply them to a shallow copy so the caller's object is never mutated.
44
+ if backoff_strategy is None or (min_retry_delay_ms is None and max_retry_delay_ms is None):
45
+ return backoff_strategy
46
+ backoff_strategy = copy.copy(backoff_strategy)
47
+ if min_retry_delay_ms is not None:
48
+ backoff_strategy.min_retry_delay_ms = min_retry_delay_ms
49
+ if max_retry_delay_ms is not None:
50
+ backoff_strategy.max_retry_delay_ms = max_retry_delay_ms
51
+ return backoff_strategy
52
+
53
+ return strategy_type(
54
+ min_retry_delay_ms=(min_retry_delay_ms
55
+ if min_retry_delay_ms is not None else current_min_retry_delay_ms),
56
+ max_retry_delay_ms=(max_retry_delay_ms
57
+ if max_retry_delay_ms is not None else current_max_retry_delay_ms),
58
+ )
59
+
60
+
61
+ def new_retry_condition(retry_condition=_UNSET, retry_error_codes=None):
62
+ if retry_condition is _UNSET:
63
+ return DefaultRetryCondition(retry_error_codes=retry_error_codes)
64
+ if type(retry_condition) is DefaultRetryCondition:
65
+ error_codes = retry_error_codes \
66
+ if retry_error_codes is not None else retry_condition.retry_error_codes
67
+ return DefaultRetryCondition(retry_error_codes=error_codes)
68
+ # Custom condition (including subclasses of DefaultRetryCondition): keep the
69
+ # caller's instance as-is unless retry_error_codes is requested, in which case
70
+ # apply it to a shallow copy so the caller's object is never mutated.
71
+ if retry_condition is None or retry_error_codes is None:
72
+ return retry_condition
73
+ retry_condition = copy.copy(retry_condition)
74
+ retry_condition.retry_error_codes = set(retry_error_codes)
75
+ return retry_condition
76
+
77
+
78
+ def new_retryer(num_max_retries=DEFAULT_NUM_MAX_RETRIES,
79
+ backoff_strategy=_UNSET,
80
+ retry_condition=_UNSET,
81
+ retry_error_codes=None,
82
+ min_retry_delay_ms=None,
83
+ max_retry_delay_ms=None):
84
+ return Retryer(
85
+ num_max_retries=num_max_retries,
86
+ backoff_strategy=new_backoff_strategy(
87
+ backoff_strategy,
88
+ min_retry_delay_ms=min_retry_delay_ms,
89
+ max_retry_delay_ms=max_retry_delay_ms,
90
+ ),
91
+ retry_condition=new_retry_condition(
92
+ retry_condition,
93
+ retry_error_codes=retry_error_codes,
94
+ ),
95
+ )
19
96
 
20
97
 
21
98
  class Retryer:
22
99
  def __init__(
23
100
  self,
24
- num_max_retries=3,
25
- backoff_strategy=_DEFAULT_BACKOFF_STRATEGY,
26
- retry_condition=_DEFAULT_RETRY_CONDITION,
101
+ num_max_retries=DEFAULT_NUM_MAX_RETRIES,
102
+ backoff_strategy=_UNSET,
103
+ retry_condition=_UNSET,
27
104
  ):
28
105
  # type: (int, BackoffStrategy, RetryCondition) -> None
29
106
  """
@@ -34,8 +111,10 @@ class Retryer:
34
111
  :param retry_condition: The retry condition to use.
35
112
  """
36
113
  self.num_max_retries = num_max_retries
37
- self.backoff_strategy = backoff_strategy
38
- self.retry_condition = retry_condition
114
+ self.backoff_strategy = new_backoff_strategy() \
115
+ if backoff_strategy is _UNSET else backoff_strategy
116
+ self.retry_condition = new_retry_condition() \
117
+ if retry_condition is _UNSET else retry_condition
39
118
 
40
119
  def should_retry(
41
120
  self,
@@ -17,6 +17,7 @@ from __future__ import absolute_import
17
17
  # 导入API模块
18
18
  from byteplussdkllmshield.api.llm_shield_sdk_v2 import ClientV2, SetServiceDev, GetServiceCode
19
19
  from byteplussdkllmshield.api.llm_shield_sdk_v2 import (
20
+ OPTION_REWRITE_URL,
20
21
  ContentTypeV2,
21
22
  DecisionTypeV2,
22
23
  UserAction,
@@ -27,9 +28,12 @@ from byteplussdkllmshield.api.llm_shield_sdk_v2 import (
27
28
  ToolCall,
28
29
  MessageV2,
29
30
  ModerateV2Request,
31
+ ImagePositionInfo,
32
+ PositionInfo,
30
33
  RiskMatchV2,
31
34
  PermitMatchV2,
32
35
  RiskV2,
36
+ SourceInfoV2,
33
37
  RiskInfoV2,
34
38
  PermitV2,
35
39
  PermitInfoV2,
@@ -56,6 +60,7 @@ from byteplussdkllmshield.api.llm_shield_sdk_v2 import (
56
60
  __all__ = [
57
61
  # API客户端
58
62
  'ClientV2',
63
+ 'OPTION_REWRITE_URL',
59
64
  # 常量类
60
65
  'ContentTypeV2',
61
66
  'DecisionTypeV2',
@@ -68,9 +73,12 @@ __all__ = [
68
73
  'ToolCall',
69
74
  'MessageV2',
70
75
  'ModerateV2Request',
76
+ 'ImagePositionInfo',
77
+ 'PositionInfo',
71
78
  'RiskMatchV2',
72
79
  'PermitMatchV2',
73
80
  'RiskV2',
81
+ 'SourceInfoV2',
74
82
  'RiskInfoV2',
75
83
  'PermitV2',
76
84
  'PermitInfoV2',
@@ -12,6 +12,8 @@ from ..models.llm_shield_sign import request_sign, Version, SetServiceDev, GetSe
12
12
  LLM_STREAM_SEND_BASE_WINDOW_V2 = 10
13
13
  LLM_STREAM_SEND_EXPONENT_V2 = 2
14
14
 
15
+ OPTION_REWRITE_URL = "RewriteUrl"
16
+
15
17
 
16
18
  # 定义内容类型常量
17
19
  class ContentTypeV2:
@@ -103,6 +105,7 @@ class ModerateV2Request(BaseModel):
103
105
  scene: str = Field("", alias="Scene")
104
106
  history: List[MessageV2] = Field([], alias="History")
105
107
  extensions: Dict[str, str] = Field(default_factory=dict, alias="Extensions")
108
+ call_generate_on_optimize: Optional[bool] = Field(None, alias="CallGenerateOnOptimize")
106
109
 
107
110
  class Config:
108
111
  populate_by_name = True
@@ -126,6 +129,24 @@ class RiskMatchV2(BaseModel):
126
129
  action: Optional[int] = Field(None, alias="Action")
127
130
  source: Optional[int] = Field(None, alias="Source")
128
131
  rule_id: Optional[Any] = Field(None, alias="RuleID")
132
+ position: Optional["PositionInfo"] = Field(None, alias="Position")
133
+
134
+ class Config:
135
+ populate_by_name = True
136
+
137
+
138
+ class ImagePositionInfo(BaseModel):
139
+ x_start: str = Field("", alias="xStart")
140
+ y_start: str = Field("", alias="yStart")
141
+ x_end: str = Field("", alias="xEnd")
142
+ y_end: str = Field("", alias="yEnd")
143
+
144
+ class Config:
145
+ populate_by_name = True
146
+
147
+
148
+ class PositionInfo(BaseModel):
149
+ image_position: Optional[ImagePositionInfo] = Field(None, alias="ImagePosition")
129
150
 
130
151
  class Config:
131
152
  populate_by_name = True
@@ -148,11 +169,24 @@ class RiskV2(BaseModel):
148
169
  label: str = Field("", alias="Label")
149
170
  prob: Optional[float] = Field(None, alias="Prob")
150
171
  matches: List[RiskMatchV2] = Field([], alias="Matches")
172
+ source_infos: List["SourceInfoV2"] = Field([], alias="SourceInfos")
151
173
 
152
174
  @field_validator('matches', mode="before")
153
175
  def convert_risk_matches_none_to_list(cls, value):
154
176
  return [] if value is None else value
155
177
 
178
+ @field_validator('source_infos', mode="before")
179
+ def convert_source_infos_none_to_list(cls, value):
180
+ return [] if value is None else value
181
+
182
+ class Config:
183
+ populate_by_name = True
184
+
185
+
186
+ class SourceInfoV2(BaseModel):
187
+ source: str = Field("", alias="Source")
188
+ source_detail: Dict[str, str] = Field(default_factory=dict, alias="SourceDetail")
189
+
156
190
  class Config:
157
191
  populate_by_name = True
158
192
 
@@ -360,8 +394,10 @@ class SessionTimeout(requests.Session):
360
394
 
361
395
  # 定义客户端类
362
396
  class ClientV2:
363
- def __init__(self, url: str, ak: str, sk: str, region: str, timeout: float):
397
+ def __init__(self, url: str, ak: str, sk: str, region: str, timeout: float,
398
+ options: Optional[Dict[str, Any]] = None):
364
399
  self.url = url
400
+ self.rewrite_url = options.get(OPTION_REWRITE_URL) if options else None
365
401
  self.ak = ak
366
402
  self.sk = sk
367
403
  self.region = region
@@ -397,7 +433,9 @@ class ClientV2:
397
433
  header = {
398
434
  }
399
435
 
400
- sign_header = request_sign(header, self.ak, self.sk, self.region, self.url, path, action, request_body)
436
+ sign_header = request_sign(
437
+ header, self.ak, self.sk, self.region, self.url, path, action, request_body, self.rewrite_url
438
+ )
401
439
 
402
440
  try:
403
441
  resp = self.http_client.post(
@@ -447,7 +485,9 @@ class ClientV2:
447
485
  if session.request.message is None:
448
486
  session.request.message = MessageV2()
449
487
  session.request.message.content += request.message.content
450
- session.request.use_stream = request.use_stream
488
+ session.request.use_stream = request.use_stream
489
+ if request.call_generate_on_optimize is not None:
490
+ session.request.call_generate_on_optimize = request.call_generate_on_optimize
451
491
  session.stream_send_len += len(request.message.content)
452
492
 
453
493
  # 3. 判断是否需要发送请求到后端
@@ -470,7 +510,9 @@ class ClientV2:
470
510
  headers = {
471
511
  # "Content-Type": "application/json",
472
512
  }
473
- sign_header = request_sign(headers, self.ak, self.sk, self.region, self.url, path, action, request_body)
513
+ sign_header = request_sign(
514
+ headers, self.ak, self.sk, self.region, self.url, path, action, request_body, self.rewrite_url
515
+ )
474
516
  try:
475
517
  response = self.http_client.post(
476
518
  url=self.url + path + "?Action=" + action + "&Version=" + Version,
@@ -513,7 +555,9 @@ class ClientV2:
513
555
  # "Content-Type": "application/json",
514
556
  }
515
557
  try:
516
- sign_header = request_sign(headers, self.ak, self.sk, self.region, self.url, path, action, requestBody)
558
+ sign_header = request_sign(
559
+ headers, self.ak, self.sk, self.region, self.url, path, action, requestBody, self.rewrite_url
560
+ )
517
561
  # 发送 HTTP 请求
518
562
  resp = self.http_client.post(url=self.url + path + "?Action=" + action + "&Version=" + Version,
519
563
  data=requestBody, headers=sign_header, stream=True)
@@ -547,3 +591,7 @@ class CustomJSONEncoder(json.JSONEncoder):
547
591
  return obj.__dict__ # 返回对象的属性字典
548
592
  # 调用默认处理(会抛出TypeError)
549
593
  return super().default(obj)
594
+
595
+
596
+ RiskMatchV2.model_rebuild()
597
+ RiskV2.model_rebuild()
@@ -67,8 +67,9 @@ def hash_sha256(content: bytes):
67
67
 
68
68
 
69
69
  # 第二步:签名请求函数
70
- def request_sign(header, ak, sk, region, url, path, action, body):
71
- host = urlparse(url).netloc
70
+ def request_sign(header, ak, sk, region, url, path, action, body, rewrite_url=None):
71
+ sign_url = rewrite_url or url
72
+ host = urlparse(sign_url).netloc
72
73
  date = utc_now()
73
74
  # 第三步:创建身份证明。其中的 Service 和 Region 字段是固定的。ak 和 sk 分别代表
74
75
  # AccessKeyID 和 SecretAccessKey。同时需要初始化签名结构体。一些签名计算时需要的属性也在这里处理。
@@ -97,7 +98,6 @@ def request_sign(header, ak, sk, region, url, path, action, body):
97
98
  short_x_date = x_date[:8]
98
99
  x_content_sha256 = hash_sha256(request_param["body"])
99
100
  sign_result = {
100
- "Host": request_param["host"],
101
101
  "X-Content-Sha256": x_content_sha256,
102
102
  "X-Date": x_date,
103
103
  "Content-Type": request_param["content_type"],