volcengine-python-sdk-aicc 0.0.41__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.
- bytedance/__init__.py +4 -0
- bytedance/jeddak_secure_channel/__init__.py +28 -0
- bytedance/jeddak_secure_channel/client.py +305 -0
- bytedance/jeddak_secure_channel/config.py +171 -0
- bytedance/jeddak_secure_channel/consts.py +5 -0
- bytedance/jeddak_secure_channel/crypto/__init__.py +14 -0
- bytedance/jeddak_secure_channel/crypto/aes.py +174 -0
- bytedance/jeddak_secure_channel/crypto/envelope.py +395 -0
- bytedance/jeddak_secure_channel/crypto/rsa.py +164 -0
- bytedance/jeddak_secure_channel/error.py +131 -0
- bytedance/jeddak_secure_channel/log/__init__.py +23 -0
- bytedance/jeddak_secure_channel/log/config.py +31 -0
- bytedance/jeddak_secure_channel/log/logger.py +88 -0
- bytedance/jeddak_secure_channel/logger_factory.py +134 -0
- bytedance/jeddak_secure_channel/ra.py +772 -0
- bytedance/jeddak_secure_channel/server.py +745 -0
- bytedance/jeddak_secure_channel/service/__init__.py +16 -0
- bytedance/jeddak_secure_channel/service/eps.py +133 -0
- bytedance/jeddak_secure_channel/service/eps_models.py +47 -0
- bytedance/jeddak_secure_channel/service/ras.py +88 -0
- bytedance/jeddak_secure_channel/service/tks.py +51 -0
- bytedance/jeddak_secure_channel/service/tks_models.py +334 -0
- bytedance/jeddak_secure_channel/utils.py +469 -0
- bytedance/jeddak_secure_model/model_automation_tool.py +181 -0
- bytedance/jeddak_secure_model/model_encryption.py +416 -0
- bytedance/jeddak_secure_model/model_hub_admin.py +116 -0
- bytedance/jeddak_secure_model/pcc_utils.py +279 -0
- bytedance/jeddak_secure_model/rds_utils.py +63 -0
- bytedance/tks/attester.py +41 -0
- bytedance/tks/client.py +629 -0
- bytedance/tks/consts.py +122 -0
- bytedance/tks/crypto/aes.py +47 -0
- bytedance/tks/crypto/ec.py +39 -0
- bytedance/tks/exceptions.py +22 -0
- bytedance/volcengine_aicc_sdk/__init__.py +14 -0
- bytedance/volcengine_aicc_sdk/aicc_client.py +277 -0
- bytedance/volcengine_aicc_sdk/aicc_inference.py +64 -0
- bytedance/volcengine_aicc_sdk/ark_inference_client.py +343 -0
- bytedance/volcengine_aicc_sdk/asr_inference_client.py +158 -0
- bytedance/volcengine_aicc_sdk/asr_websocket_client.py +705 -0
- bytedance/volcengine_aicc_sdk/auc_inference_client.py +305 -0
- bytedance/volcengine_aicc_sdk/common/__init__.py +0 -0
- bytedance/volcengine_aicc_sdk/common/aes.py +136 -0
- bytedance/volcengine_aicc_sdk/common/constant.py +4 -0
- bytedance/volcengine_aicc_sdk/common/error.py +21 -0
- bytedance/volcengine_aicc_sdk/common/log/__init__.py +24 -0
- bytedance/volcengine_aicc_sdk/common/log/config.py +31 -0
- bytedance/volcengine_aicc_sdk/common/log/logger.py +88 -0
- bytedance/volcengine_aicc_sdk/common/tos_utils.py +341 -0
- bytedance/volcengine_aicc_sdk/common/utils.py +45 -0
- bytedance/volcengine_aicc_sdk/cu_inference_client.py +292 -0
- bytedance/volcengine_aicc_sdk/deepseek_inference_client.py +501 -0
- bytedance/volcengine_aicc_sdk/secure_http_client.py +406 -0
- bytedance/volcengine_aicc_sdk/seedance_inference_client.py +500 -0
- bytedance/volcengine_aicc_sdk/seedream_inference_client.py +213 -0
- volcengine_python_sdk_aicc-0.0.41.dist-info/METADATA +79 -0
- volcengine_python_sdk_aicc-0.0.41.dist-info/RECORD +60 -0
- volcengine_python_sdk_aicc-0.0.41.dist-info/WHEEL +5 -0
- volcengine_python_sdk_aicc-0.0.41.dist-info/licenses/LICENSE +202 -0
- volcengine_python_sdk_aicc-0.0.41.dist-info/top_level.txt +1 -0
bytedance/tks/consts.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class Conf:
|
|
5
|
+
# RELATIVE_CONST_INDEX_PATH = './templates/html/main/index.html'
|
|
6
|
+
# CONST_INDEX_PATH = './html/main/index.html'
|
|
7
|
+
|
|
8
|
+
# cookie key名称,用以保存登陆态的session token
|
|
9
|
+
SESSION_COOKIE_KEY = "session_token"
|
|
10
|
+
# 合法时间戳的最大误差,单位秒
|
|
11
|
+
TIMESTAMP_THRESHOLD = 60
|
|
12
|
+
# session token的过期时间,单位秒
|
|
13
|
+
SESSION_TOKEN_EXPIRE = 86400
|
|
14
|
+
# redis配置
|
|
15
|
+
REDIS_HOST = "localhost"
|
|
16
|
+
REDIS_PORT = 6379
|
|
17
|
+
REDIS_USERNAME = "default"
|
|
18
|
+
REDIS_PASSWORD = ""
|
|
19
|
+
# pcc后端的分流配置
|
|
20
|
+
BACKEND_MAP = {
|
|
21
|
+
"/api/tks/": ["http://localhost:10000", "appID", "AK", "SK"],
|
|
22
|
+
"/api/bgs/": ["http://localhost:10001", "UID", "AK", "SK"],
|
|
23
|
+
"/api/ras/": ["http://localhost:10002", "UID", "AK", "SK"],
|
|
24
|
+
"/api/rag/": ["http://localhost:10003", "UID", "AK", "SK"],
|
|
25
|
+
}
|
|
26
|
+
STREAM_PATH = "/api/rag/collection/query_and_generate"
|
|
27
|
+
|
|
28
|
+
UPLOAD_FOLDER = os.path.abspath("./upload/rag/doc")
|
|
29
|
+
DOWNLOAD_FOLDER = os.path.abspath("./download/attest/report")
|
|
30
|
+
ALLOWED_EXTENSIONS = {".txt", ".pdf", ".pptx", ".docx"}
|
|
31
|
+
|
|
32
|
+
# pcc后端转发配置
|
|
33
|
+
PCC_UID = "jeddak_team"
|
|
34
|
+
PCC_PASSWORD = "some_password"
|
|
35
|
+
PCC_AK = "some_AK"
|
|
36
|
+
PCC_SK = "some_SK"
|
|
37
|
+
RING_ID = "id_1234"
|
|
38
|
+
POLICY_ID = "policy_1234"
|
|
39
|
+
KEY_TYPE = "SYMMETRIC_256"
|
|
40
|
+
|
|
41
|
+
RAG_SERVICE_NAME = "rag-deployment"
|
|
42
|
+
HANDLER_SERVICE_NAME = "ai-handler"
|
|
43
|
+
# RAG_SERVICE = {
|
|
44
|
+
# "ServiceInfo": {
|
|
45
|
+
# "_key": "rag",
|
|
46
|
+
# "_val": "rag-deployment"
|
|
47
|
+
# }
|
|
48
|
+
# }
|
|
49
|
+
# HANDLER_SERVICE = {
|
|
50
|
+
# "ServiceInfo": {
|
|
51
|
+
# "_key": "handler",
|
|
52
|
+
# "_val": "ai-handler"
|
|
53
|
+
# }
|
|
54
|
+
# }
|
|
55
|
+
# SERVICE_INFO_LIST = {"rag": RAG_SERVICE, "handler": HANDLER_SERVICE}
|
|
56
|
+
SERVICE_INFO = {"ServiceInfo": {"rag": RAG_SERVICE_NAME, "handler": HANDLER_SERVICE_NAME}}
|
|
57
|
+
|
|
58
|
+
PCC_URL = "https://jeddakchain.bytedance.com"
|
|
59
|
+
PATH_MAP = {
|
|
60
|
+
"/api_mobile/doc/update": "/api/rag/collection",
|
|
61
|
+
"/api_mobile/rag/query_and_generate": "/api/rag/collection/query_and_generate",
|
|
62
|
+
# security使用同一个接口,如果改成不同,ENV处理逻辑要改
|
|
63
|
+
"/api_mobile/security/status": "/api/ras/v1/attestation_user",
|
|
64
|
+
"/api_mobile/security/status/download": "/api/ras/v1/attestation_user",
|
|
65
|
+
# TODO: 确定接口
|
|
66
|
+
"/api_mobile/key/set": "/api/tks/",
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
# RAG_ADDR = 'http://localhost:10003'
|
|
70
|
+
# RAG_COLLECTION_CREATE_ENDPOINT = '/api/rag/collection/create'
|
|
71
|
+
# RAS_SHIM_ADDR = "http://localhost:10002"
|
|
72
|
+
|
|
73
|
+
DOC_STATUS_MAP = {"creating": "处理中", "ready": "处理完成", "failed": "处理失败"}
|
|
74
|
+
|
|
75
|
+
SALT = "ai-handler-salt"
|
|
76
|
+
|
|
77
|
+
def update_from_env(self):
|
|
78
|
+
for k in self.BACKEND_MAP:
|
|
79
|
+
env_key = f"ENV_{k.split('/')[-2].upper()}_URL"
|
|
80
|
+
# ENV_TKS_URL, ...
|
|
81
|
+
if env_key in os.environ:
|
|
82
|
+
self.BACKEND_MAP[k][0] = os.getenv(env_key)
|
|
83
|
+
for k in self.PATH_MAP:
|
|
84
|
+
env_key = f"HANDLER_{k.split('/')[2].upper()}_URL"
|
|
85
|
+
if env_key in os.environ:
|
|
86
|
+
self.PATH_MAP[k] = os.getenv(env_key)
|
|
87
|
+
self.REDIS_USERNAME = (
|
|
88
|
+
os.getenv("ENV_REDIS_USERNAME") if "ENV_REDIS_USERNAME" in os.environ else None
|
|
89
|
+
)
|
|
90
|
+
self.REDIS_PASSWORD = (
|
|
91
|
+
os.getenv("ENV_REDIS_PASSWORD") if "ENV_REDIS_PASSWORD" in os.environ else None
|
|
92
|
+
)
|
|
93
|
+
if "ENV_REDIS_HOST" in os.environ:
|
|
94
|
+
self.REDIS_HOST = os.getenv("ENV_REDIS_HOST")
|
|
95
|
+
if "ENV_REDIS_PORT" in os.environ:
|
|
96
|
+
self.REDIS_PORT = int(os.getenv("ENV_REDIS_PORT"))
|
|
97
|
+
if "ENV_SESSION_TOKEN_EXPIRE" in os.environ:
|
|
98
|
+
self.SESSION_TOKEN_EXPIRE = int(os.getenv("ENV_SESSION_TOKEN_EXPIRE"))
|
|
99
|
+
if "RAG_SERVICE_NAME" in os.environ:
|
|
100
|
+
self.RAG_SERVICE_NAME = os.getenv("RAG_SERVICE_NAME")
|
|
101
|
+
if "HANDLER_SERVICE_NAME" in os.environ:
|
|
102
|
+
self.HANDLER_SERVICE_NAME = os.getenv("HANDLER_SERVICE_NAME")
|
|
103
|
+
if "RING_ID" in os.environ:
|
|
104
|
+
self.RING_ID = os.getenv("RING_ID")
|
|
105
|
+
if "POLICY_ID" in os.environ:
|
|
106
|
+
self.POLICY_ID = os.getenv("POLICY_ID")
|
|
107
|
+
if "KEY_TYPE" in os.environ:
|
|
108
|
+
self.KEY_TYPE = os.getenv("KEY_TYPE")
|
|
109
|
+
if "PCC_UID" in os.environ:
|
|
110
|
+
self.PCC_UID = os.getenv("PCC_UID")
|
|
111
|
+
if "PCC_PASSWORD" in os.environ:
|
|
112
|
+
self.PCC_PASSWORD = os.getenv("PCC_PASSWORD")
|
|
113
|
+
if "PCC_AK" in os.environ:
|
|
114
|
+
self.PCC_AK = os.getenv("PCC_AK")
|
|
115
|
+
if "PCC_SK" in os.environ:
|
|
116
|
+
self.PCC_SK = os.getenv("PCC_SK")
|
|
117
|
+
os.makedirs(conf.UPLOAD_FOLDER, exist_ok=True)
|
|
118
|
+
os.makedirs(conf.DOWNLOAD_FOLDER, exist_ok=True)
|
|
119
|
+
print(f"final backend_map: {self.BACKEND_MAP}")
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
conf = Conf()
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
"gcm_encrypt",
|
|
3
|
+
"gcm_decrypt",
|
|
4
|
+
]
|
|
5
|
+
|
|
6
|
+
import secrets
|
|
7
|
+
|
|
8
|
+
from cryptography.hazmat.backends import default_backend
|
|
9
|
+
from cryptography.hazmat.primitives import hashes
|
|
10
|
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
|
11
|
+
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
|
12
|
+
|
|
13
|
+
from ..consts import conf
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def gcm_decrypt(key: bytes, ciphertext: bytes) -> bytes:
|
|
17
|
+
iv, ciphertext = ciphertext[:12], ciphertext[12:]
|
|
18
|
+
ciphertext, tag = ciphertext[:-16], ciphertext[-16:]
|
|
19
|
+
decryptor = Cipher(
|
|
20
|
+
algorithms.AES(key),
|
|
21
|
+
modes.GCM(iv, tag),
|
|
22
|
+
).decryptor()
|
|
23
|
+
|
|
24
|
+
return decryptor.update(ciphertext) + decryptor.finalize()
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def gcm_encrypt(key: bytes, plaintext: bytes) -> bytes:
|
|
28
|
+
iv = secrets.token_bytes(12)
|
|
29
|
+
encryptor = Cipher(
|
|
30
|
+
algorithms.AES(key),
|
|
31
|
+
modes.GCM(iv),
|
|
32
|
+
).encryptor()
|
|
33
|
+
|
|
34
|
+
ciphertext = encryptor.update(plaintext) + encryptor.finalize()
|
|
35
|
+
return iv + ciphertext + encryptor.tag
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def generate_key_from_string(seed_string: str) -> bytes:
|
|
39
|
+
backend = default_backend()
|
|
40
|
+
# 将字符串种子转换为字节
|
|
41
|
+
password = seed_string.encode()
|
|
42
|
+
salt = conf.SALT.encode()
|
|
43
|
+
kdf = PBKDF2HMAC(
|
|
44
|
+
algorithm=hashes.SHA256(), length=32, salt=salt, iterations=100000, backend=backend
|
|
45
|
+
)
|
|
46
|
+
key = kdf.derive(password)
|
|
47
|
+
return key
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from typing import Tuple
|
|
2
|
+
|
|
3
|
+
from cryptography.hazmat.primitives import hashes, serialization
|
|
4
|
+
from cryptography.hazmat.primitives.asymmetric import ec
|
|
5
|
+
from cryptography.hazmat.primitives.kdf.hkdf import HKDF
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"diffie_hellman_key_exchange",
|
|
9
|
+
"generate_key_pair",
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def diffie_hellman_key_exchange(local_sk: bytes, peer_pk: bytes) -> bytes:
|
|
14
|
+
local_sk_key = serialization.load_pem_private_key(local_sk, password=None)
|
|
15
|
+
peer_pk_key = serialization.load_pem_public_key(peer_pk)
|
|
16
|
+
assert isinstance(local_sk_key, ec.EllipticCurvePrivateKey)
|
|
17
|
+
assert isinstance(peer_pk_key, ec.EllipticCurvePublicKey)
|
|
18
|
+
shared_key = local_sk_key.exchange(ec.ECDH(), peer_pk_key)
|
|
19
|
+
derived_key = HKDF(algorithm=hashes.SHA256(), length=32, salt=None, info=None).derive(
|
|
20
|
+
shared_key
|
|
21
|
+
)
|
|
22
|
+
return derived_key
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def generate_key_pair() -> Tuple[bytes, bytes]:
|
|
26
|
+
private_key = ec.generate_private_key(ec.SECP384R1())
|
|
27
|
+
public_key = private_key.public_key()
|
|
28
|
+
|
|
29
|
+
private_key_pem = private_key.private_bytes(
|
|
30
|
+
encoding=serialization.Encoding.PEM,
|
|
31
|
+
format=serialization.PrivateFormat.PKCS8,
|
|
32
|
+
encryption_algorithm=serialization.NoEncryption(),
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
public_key_pem = public_key.public_bytes(
|
|
36
|
+
encoding=serialization.Encoding.PEM, format=serialization.PublicFormat.SubjectPublicKeyInfo
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
return private_key_pem, public_key_pem
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from typing import Optional
|
|
2
|
+
|
|
3
|
+
__all__ = [
|
|
4
|
+
"TKSError",
|
|
5
|
+
"RAError",
|
|
6
|
+
]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TKSError(Exception):
|
|
10
|
+
def __init__(self, message: str, http_status: Optional[int] = None):
|
|
11
|
+
super().__init__(message)
|
|
12
|
+
self.http_status = http_status
|
|
13
|
+
|
|
14
|
+
def __str__(self):
|
|
15
|
+
if self.http_status:
|
|
16
|
+
return f"{self.args[0]} (HTTP Status: {self.http_status})"
|
|
17
|
+
return self.args[0]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class RAError(TKSError):
|
|
21
|
+
def __init__(self, message: str, http_status: Optional[int] = None):
|
|
22
|
+
super().__init__(message, http_status)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
"AICCClient",
|
|
3
|
+
"AICCModelType",
|
|
4
|
+
"__version__",
|
|
5
|
+
]
|
|
6
|
+
|
|
7
|
+
__version__ = "0.0.41"
|
|
8
|
+
|
|
9
|
+
from .aicc_client import AICCModelType
|
|
10
|
+
from .aicc_client import AICCClient
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def __getattr__(name: str) -> object:
|
|
14
|
+
raise AttributeError(f"module {__name__} has no attribute {name}")
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
from bytedance.volcengine_aicc_sdk.seedance_inference_client import SeedanceClient
|
|
2
|
+
from bytedance.volcengine_aicc_sdk.seedream_inference_client import SeedreamClient
|
|
3
|
+
from typing import Any, List, Optional, Union
|
|
4
|
+
"""
|
|
5
|
+
AICC访问客户端
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"AICCClient",
|
|
10
|
+
"AICCModelType"
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
from bytedance.volcengine_aicc_sdk.cu_inference_client import CUClient
|
|
14
|
+
from bytedance.volcengine_aicc_sdk.auc_inference_client import AUCClient
|
|
15
|
+
from bytedance.volcengine_aicc_sdk.ark_inference_client import ARKClient
|
|
16
|
+
from bytedance.volcengine_aicc_sdk.deepseek_inference_client import DeepSeekClient
|
|
17
|
+
from bytedance.volcengine_aicc_sdk.asr_websocket_client import AsrWebSocketClient
|
|
18
|
+
from .asr_inference_client import ASRClient
|
|
19
|
+
from volcenginesdkarkruntime import Ark
|
|
20
|
+
from .common.error import AICCException
|
|
21
|
+
from .common.log import LogConfig, logger
|
|
22
|
+
from bytedance.volcengine_aicc_sdk.secure_http_client import AICCConfig, SecureHttpClient
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class AICCModelType:
|
|
26
|
+
DOUBAO = "DOUBAO" # 机密豆包
|
|
27
|
+
AICC_CU = "AICC-CU" # AICC CU版模型
|
|
28
|
+
OSLM = "OSLM" # 开源模型
|
|
29
|
+
ASR = "ASR"
|
|
30
|
+
ASR_WS = "ASR-WS"
|
|
31
|
+
SEEDANCE = "SEEDANCE"
|
|
32
|
+
SEEDREAM = "SEEDREAM"
|
|
33
|
+
AUC = "AUC"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class AICCClient:
|
|
37
|
+
"""
|
|
38
|
+
AICC机密推理访问客户端.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
def __init__(self,
|
|
42
|
+
base_url: str = "",
|
|
43
|
+
api_key: str = "",
|
|
44
|
+
endpoint: str = "",
|
|
45
|
+
aicc_config: AICCConfig = None,
|
|
46
|
+
**kwargs
|
|
47
|
+
):
|
|
48
|
+
self.base_url = base_url
|
|
49
|
+
self.api_key = api_key
|
|
50
|
+
self.aicc_config = aicc_config
|
|
51
|
+
self.endpoint = endpoint
|
|
52
|
+
self.kwargs = kwargs
|
|
53
|
+
|
|
54
|
+
if not self.base_url or not self.api_key:
|
|
55
|
+
raise AICCException(f"New AICC client failed, base_url={self.base_url}, api_key={self.api_key}")
|
|
56
|
+
|
|
57
|
+
if not self.endpoint:
|
|
58
|
+
self.endpoint = self.kwargs.get("model_name")
|
|
59
|
+
|
|
60
|
+
self.secure_http_client = self._init_secure_http_client()
|
|
61
|
+
self.config = dict()
|
|
62
|
+
self._init_config()
|
|
63
|
+
self._init_log()
|
|
64
|
+
|
|
65
|
+
def get_llm_instance(self, model_type):
|
|
66
|
+
model_type = model_type.upper()
|
|
67
|
+
if AICCModelType.SEEDANCE in model_type:
|
|
68
|
+
return SeedanceClient(base_url=self.base_url,
|
|
69
|
+
api_key=self.api_key,
|
|
70
|
+
endpoint=self.endpoint,
|
|
71
|
+
aicc_config=self.aicc_config,
|
|
72
|
+
**self.kwargs)
|
|
73
|
+
elif AICCModelType.AICC_CU in model_type:
|
|
74
|
+
return CUClient(base_url=self.base_url,
|
|
75
|
+
aicc_service_name=self.aicc_config.aicc_llm_service_name,
|
|
76
|
+
policy_id=self.aicc_config.policy_id,
|
|
77
|
+
is_openai=self.kwargs.get("is_openai"),
|
|
78
|
+
is_secure=self.kwargs.get("is_secure"),
|
|
79
|
+
ak=self.aicc_config.ak,
|
|
80
|
+
sk=self.aicc_config.sk,
|
|
81
|
+
region=self.aicc_config.region,
|
|
82
|
+
**self.kwargs)
|
|
83
|
+
elif AICCModelType.ASR == model_type:
|
|
84
|
+
return ASRClient(base_url=self.base_url,
|
|
85
|
+
aicc_service_name=self.aicc_config.aicc_llm_service_name,
|
|
86
|
+
policy_id=self.aicc_config.policy_id,
|
|
87
|
+
is_openai=self.kwargs.get("is_openai"),
|
|
88
|
+
is_secure=self.kwargs.get("is_secure"),
|
|
89
|
+
ak=self.aicc_config.ak,
|
|
90
|
+
sk=self.aicc_config.sk,
|
|
91
|
+
region=self.aicc_config.region,
|
|
92
|
+
**self.kwargs)
|
|
93
|
+
elif AICCModelType.OSLM in model_type:
|
|
94
|
+
return DeepSeekClient(base_url=self.base_url,
|
|
95
|
+
api_key=self.api_key,
|
|
96
|
+
ep=self.endpoint,
|
|
97
|
+
aicc_service_name=self.aicc_config.aicc_llm_service_name,
|
|
98
|
+
policy_id=self.aicc_config.policy_id,
|
|
99
|
+
is_openai=self.kwargs.get("is_openai"),
|
|
100
|
+
is_secure=self.kwargs.get("is_secure"),
|
|
101
|
+
ak=self.aicc_config.ak,
|
|
102
|
+
sk=self.aicc_config.sk,
|
|
103
|
+
region=self.aicc_config.region,
|
|
104
|
+
**self.kwargs)
|
|
105
|
+
elif AICCModelType.DOUBAO in model_type:
|
|
106
|
+
return ARKClient(base_url=self.base_url,
|
|
107
|
+
api_key=self.api_key,
|
|
108
|
+
ep=self.endpoint,
|
|
109
|
+
is_openai=self.kwargs.get("is_openai"),
|
|
110
|
+
is_secure=self.kwargs.get("is_secure"),
|
|
111
|
+
ak=self.aicc_config.ak,
|
|
112
|
+
sk=self.aicc_config.sk,
|
|
113
|
+
region=self.aicc_config.region,
|
|
114
|
+
**self.kwargs)
|
|
115
|
+
elif AICCModelType.ASR_WS == model_type:
|
|
116
|
+
return AsrWebSocketClient(base_url=self.base_url,
|
|
117
|
+
api_key=self.api_key,
|
|
118
|
+
endpoint=self.endpoint,
|
|
119
|
+
aicc_config=self.aicc_config,
|
|
120
|
+
**self.kwargs)
|
|
121
|
+
elif AICCModelType.AUC == model_type:
|
|
122
|
+
return AUCClient(base_url=self.base_url,
|
|
123
|
+
api_key=self.api_key,
|
|
124
|
+
endpoint=self.endpoint,
|
|
125
|
+
aicc_config=self.aicc_config,
|
|
126
|
+
**self.kwargs)
|
|
127
|
+
elif AICCModelType.SEEDREAM == model_type:
|
|
128
|
+
return SeedreamClient(base_url=self.base_url,
|
|
129
|
+
api_key=self.api_key,
|
|
130
|
+
endpoint=self.endpoint,
|
|
131
|
+
aicc_config=self.aicc_config,
|
|
132
|
+
**self.kwargs)
|
|
133
|
+
else:
|
|
134
|
+
raise AICCException(f"New AICC client failed, model_type={model_type}")
|
|
135
|
+
|
|
136
|
+
@staticmethod
|
|
137
|
+
def get_inference_instance(model_type: str,
|
|
138
|
+
base_url: str = None,
|
|
139
|
+
api_key: str = None,
|
|
140
|
+
ep: str = None,
|
|
141
|
+
aicc_service_name: str = None,
|
|
142
|
+
policy_id: str = None,
|
|
143
|
+
is_openai: bool = False,
|
|
144
|
+
is_secure: bool = True,
|
|
145
|
+
ak: str = None,
|
|
146
|
+
sk: str = None,
|
|
147
|
+
region: str = "cn-beijing",
|
|
148
|
+
**kwargs):
|
|
149
|
+
if model_type.upper() == AICCModelType.DOUBAO:
|
|
150
|
+
return ARKClient(base_url=base_url,
|
|
151
|
+
api_key=api_key,
|
|
152
|
+
ep=ep,
|
|
153
|
+
is_openai=is_openai,
|
|
154
|
+
is_secure=is_secure,
|
|
155
|
+
ak=ak,
|
|
156
|
+
sk=sk,
|
|
157
|
+
region=region,
|
|
158
|
+
**kwargs)
|
|
159
|
+
elif model_type.upper() == AICCModelType.AICC_CU:
|
|
160
|
+
return CUClient(base_url=base_url,
|
|
161
|
+
aicc_service_name=aicc_service_name,
|
|
162
|
+
policy_id=policy_id,
|
|
163
|
+
is_openai=is_openai,
|
|
164
|
+
is_secure=is_secure,
|
|
165
|
+
ak=ak,
|
|
166
|
+
sk=sk,
|
|
167
|
+
region=region,
|
|
168
|
+
**kwargs)
|
|
169
|
+
elif model_type.upper() == AICCModelType.OSLM:
|
|
170
|
+
return DeepSeekClient(base_url=base_url,
|
|
171
|
+
api_key=api_key,
|
|
172
|
+
ep=ep,
|
|
173
|
+
aicc_service_name=aicc_service_name,
|
|
174
|
+
policy_id=policy_id,
|
|
175
|
+
is_openai=is_openai,
|
|
176
|
+
is_secure=is_secure,
|
|
177
|
+
ak=ak,
|
|
178
|
+
sk=sk,
|
|
179
|
+
region=region,
|
|
180
|
+
**kwargs)
|
|
181
|
+
elif model_type.upper() == AICCModelType.ASR:
|
|
182
|
+
return ASRClient(base_url=base_url,
|
|
183
|
+
aicc_service_name=aicc_service_name,
|
|
184
|
+
policy_id=policy_id,
|
|
185
|
+
is_openai=is_openai,
|
|
186
|
+
is_secure=is_secure,
|
|
187
|
+
ak=ak,
|
|
188
|
+
sk=sk,
|
|
189
|
+
region=region,
|
|
190
|
+
**kwargs)
|
|
191
|
+
else:
|
|
192
|
+
err_str = f"can not support model type:{model_type}"
|
|
193
|
+
logger.error(err_str)
|
|
194
|
+
raise AICCException(err_str)
|
|
195
|
+
|
|
196
|
+
@staticmethod
|
|
197
|
+
def get_volcengine_ark(base_url: str = None, api_key: str = None, **kwargs):
|
|
198
|
+
return Ark(api_key=api_key, base_url=base_url, **kwargs)
|
|
199
|
+
|
|
200
|
+
def get_secure_http_client(self) -> SecureHttpClient:
|
|
201
|
+
return self.secure_http_client
|
|
202
|
+
|
|
203
|
+
def _init_secure_http_client(self) -> SecureHttpClient:
|
|
204
|
+
if self.aicc_config:
|
|
205
|
+
return SecureHttpClient(aicc_config=self.aicc_config)
|
|
206
|
+
return SecureHttpClient()
|
|
207
|
+
|
|
208
|
+
def get_ark_client(self, secure: bool = True, **kwargs: Any) -> Ark:
|
|
209
|
+
if secure:
|
|
210
|
+
kwargs.setdefault("http_client", self.secure_http_client)
|
|
211
|
+
return Ark(api_key=self.api_key, base_url=self.base_url, **kwargs)
|
|
212
|
+
|
|
213
|
+
def tokenization(
|
|
214
|
+
self,
|
|
215
|
+
text: Union[str, List[str]],
|
|
216
|
+
model: Optional[str] = None,
|
|
217
|
+
user: Optional[str] = None,
|
|
218
|
+
**kwargs: Any):
|
|
219
|
+
if not self.aicc_config:
|
|
220
|
+
raise AICCException("Tokenization failed, aicc_config is required")
|
|
221
|
+
|
|
222
|
+
tokenization_model = model or self.endpoint or self.kwargs.get("model_name")
|
|
223
|
+
if not tokenization_model:
|
|
224
|
+
raise AICCException("Tokenization failed, model is required")
|
|
225
|
+
|
|
226
|
+
request_url = f"{self.base_url.rstrip('/')}/tokenization"
|
|
227
|
+
request_headers = {
|
|
228
|
+
"Authorization": f"Bearer {self.api_key}",
|
|
229
|
+
"Content-Type": "application/json",
|
|
230
|
+
}
|
|
231
|
+
request_body = {
|
|
232
|
+
"model": str(tokenization_model),
|
|
233
|
+
"text": text,
|
|
234
|
+
}
|
|
235
|
+
if user:
|
|
236
|
+
request_body["user"] = user
|
|
237
|
+
|
|
238
|
+
try:
|
|
239
|
+
response = self.secure_http_client.post(
|
|
240
|
+
request_url,
|
|
241
|
+
headers=request_headers,
|
|
242
|
+
json=request_body,
|
|
243
|
+
**kwargs,
|
|
244
|
+
)
|
|
245
|
+
response.raise_for_status()
|
|
246
|
+
return response.json()
|
|
247
|
+
except Exception as e:
|
|
248
|
+
raise
|
|
249
|
+
|
|
250
|
+
@staticmethod
|
|
251
|
+
def hello() -> str:
|
|
252
|
+
info = "hello volcengine_aicc_sdk"
|
|
253
|
+
print(info)
|
|
254
|
+
return info
|
|
255
|
+
|
|
256
|
+
def _init_config(self):
|
|
257
|
+
pass
|
|
258
|
+
|
|
259
|
+
def _init_log(self):
|
|
260
|
+
config = LogConfig()
|
|
261
|
+
|
|
262
|
+
if log_dir := self.config.get("dir", "aicc_log"):
|
|
263
|
+
config.dir = log_dir
|
|
264
|
+
|
|
265
|
+
if filename := self.config.get("filename", "aicc.log"):
|
|
266
|
+
config.filename = filename
|
|
267
|
+
|
|
268
|
+
if rotation := self.config.get("rotation", 100):
|
|
269
|
+
config.rotation = rotation
|
|
270
|
+
|
|
271
|
+
if retention := self.config.get("retention", 30):
|
|
272
|
+
config.retention = retention
|
|
273
|
+
|
|
274
|
+
if level := self.config.get("level"):
|
|
275
|
+
config.level = level
|
|
276
|
+
|
|
277
|
+
logger.init_log_config(config)
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"""
|
|
2
|
+
AICC机密推理客户端BASE
|
|
3
|
+
"""
|
|
4
|
+
__all__ = ["AICCInference"]
|
|
5
|
+
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from typing import IO, Union
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class AICCInference(ABC):
|
|
11
|
+
"""
|
|
12
|
+
AICC机密推理访问客户端.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
def __init__(self,
|
|
16
|
+
base_url: str = None,
|
|
17
|
+
is_openai: bool = True,
|
|
18
|
+
is_secure: bool = True,
|
|
19
|
+
ak: str = None,
|
|
20
|
+
sk: str = None,
|
|
21
|
+
region: str = "cn-beijing"
|
|
22
|
+
):
|
|
23
|
+
self.base_url = base_url
|
|
24
|
+
self.is_openai = is_openai
|
|
25
|
+
self.is_secure = is_secure
|
|
26
|
+
self.ak = ak
|
|
27
|
+
self.sk = sk
|
|
28
|
+
self.region = region
|
|
29
|
+
|
|
30
|
+
@staticmethod
|
|
31
|
+
def hello() -> str:
|
|
32
|
+
info = "hello aicc inference"
|
|
33
|
+
print(info)
|
|
34
|
+
return info
|
|
35
|
+
|
|
36
|
+
@abstractmethod
|
|
37
|
+
def chat_completions(self, messages: list, output_stream: IO = None) -> Union[str, None]:
|
|
38
|
+
"""
|
|
39
|
+
生成聊天补全
|
|
40
|
+
:param messages:
|
|
41
|
+
:param output_stream:是否启用流式响应
|
|
42
|
+
:return:
|
|
43
|
+
"""
|
|
44
|
+
pass
|
|
45
|
+
|
|
46
|
+
@abstractmethod
|
|
47
|
+
def function_call(self, messages: list, tools: list, output_stream: IO = None) -> Union[str, None]:
|
|
48
|
+
"""
|
|
49
|
+
生成聊天补全
|
|
50
|
+
:param messages:
|
|
51
|
+
:param tools:
|
|
52
|
+
:param output_stream: 是否开启流式
|
|
53
|
+
:return:
|
|
54
|
+
"""
|
|
55
|
+
pass
|
|
56
|
+
|
|
57
|
+
@abstractmethod
|
|
58
|
+
def asr_recognize(self, file_name: str) -> Union[str, None]:
|
|
59
|
+
"""
|
|
60
|
+
ASR转文字
|
|
61
|
+
:param file_name:
|
|
62
|
+
:return:
|
|
63
|
+
"""
|
|
64
|
+
pass
|