1212load_dotenv ()
1313
1414
15+ _GPDB_PUBLIC_SHARED_REGIONS = (
16+ "cn-beijing" ,
17+ "cn-hangzhou" ,
18+ "cn-shanghai" ,
19+ "cn-shenzhen" ,
20+ "cn-hongkong" ,
21+ "ap-southeast-1" ,
22+ )
23+
24+
1525def get_env_with_default (default : str , * key : str ) -> str :
1626 """从环境变量获取值,支持多个候选键 / Get value from environment variables with multiple fallback keys
1727
@@ -29,6 +39,15 @@ def get_env_with_default(default: str, *key: str) -> str:
2939 return default
3040
3141
42+ def get_env_flag (* key : str ) -> bool :
43+ """从环境变量读取布尔开关 / Read boolean flag from environment variables"""
44+ for k in key :
45+ v = os .getenv (k )
46+ if v is not None :
47+ return v .strip ().lower () in ("1" , "true" , "yes" , "on" )
48+ return False
49+
50+
3251class Config :
3352 """AgentRun SDK 全局配置类 / AgentRun SDK Global Configuration Class
3453
@@ -62,6 +81,7 @@ class Config:
6281 "_data_endpoint" ,
6382 "_devs_endpoint" ,
6483 "_bailian_endpoint" ,
84+ "_use_vpc_endpoint" ,
6585 "_headers" ,
6686 "__weakref__" ,
6787 )
@@ -80,6 +100,7 @@ def __init__(
80100 data_endpoint : Optional [str ] = None ,
81101 devs_endpoint : Optional [str ] = None ,
82102 bailian_endpoint : Optional [str ] = None ,
103+ use_vpc_endpoint : Optional [bool ] = None ,
83104 headers : Optional [Dict [str , str ]] = None ,
84105 ) -> None :
85106 """初始化配置 / Initialize configuration
@@ -103,7 +124,9 @@ def __init__(
103124 read_timeout: 读取超时时间(秒),默认 100000 / Read timeout in seconds, defaults to 100000
104125 control_endpoint: 自定义控制链路端点,可选 / Custom control endpoint, optional
105126 data_endpoint: 自定义数据链路端点,可选 / Custom data endpoint, optional
106- devs_endpoint: 自定义 DevS 端点,可选 / Custom DevS endpoint, optional
127+ devs_endpoint: 自定义 Devs 端点,可选 / Custom DevS endpoint, optional
128+ use_vpc_endpoint: 知识库检索是否使用 VPC 内网 endpoint,默认 false
129+ 未提供时从环境变量读取: AGENTRUN_KB_USE_VPC
107130 headers: 自定义请求头,可选 / Custom request headers, optional
108131 """
109132
@@ -139,6 +162,8 @@ def __init__(
139162 devs_endpoint = get_env_with_default ("" , "DEVS_ENDPOINT" )
140163 if bailian_endpoint is None :
141164 bailian_endpoint = get_env_with_default ("" , "BAILIAN_ENDPOINT" )
165+ if use_vpc_endpoint is None :
166+ use_vpc_endpoint = get_env_flag ("AGENTRUN_KB_USE_VPC" )
142167
143168 self ._access_key_id = access_key_id
144169 self ._access_key_secret = access_key_secret
@@ -152,6 +177,7 @@ def __init__(
152177 self ._data_endpoint = data_endpoint
153178 self ._devs_endpoint = devs_endpoint
154179 self ._bailian_endpoint = bailian_endpoint
180+ self ._use_vpc_endpoint = use_vpc_endpoint
155181 self ._headers = headers or {}
156182
157183 @classmethod
@@ -263,8 +289,33 @@ def get_bailian_endpoint(self) -> str:
263289 if self ._bailian_endpoint :
264290 return self ._bailian_endpoint
265291
292+ if self ._use_vpc_endpoint :
293+ return f"bailian-vpc.{ self .get_region_id ()} .aliyuncs.com"
294+
266295 return "https://bailian.cn-beijing.aliyuncs.com"
267296
297+ def get_gpdb_endpoint (self ) -> str :
298+ """获取 GPDB (ADB) OpenAPI 端点 / Get GPDB (ADB) OpenAPI endpoint"""
299+ region_id = self .get_region_id ()
300+ if self ._use_vpc_endpoint :
301+ return f"gpdb-vpc.{ region_id } .aliyuncs.com"
302+ if region_id in _GPDB_PUBLIC_SHARED_REGIONS :
303+ return "gpdb.aliyuncs.com"
304+ return f"gpdb.{ region_id } .aliyuncs.com"
305+
306+ def get_ots_endpoint (self , instance_name : str ) -> str :
307+ """获取 OTS endpoint / Get OTS endpoint"""
308+ region_id = self .get_region_id ()
309+ if self ._use_vpc_endpoint :
310+ return (
311+ f"https://{ instance_name } .{ region_id } .vpc.tablestore.aliyuncs.com"
312+ )
313+ return f"http://ots-{ region_id } .aliyuncs.com"
314+
315+ def get_use_vpc_endpoint (self ) -> bool :
316+ """知识库检索是否使用 VPC 内网 endpoint"""
317+ return self ._use_vpc_endpoint
318+
268319 def get_headers (self ) -> Dict [str , str ]:
269320 """获取自定义请求头"""
270321 return self ._headers or {}
0 commit comments