|
| 1 | +# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | + |
| 17 | +from veadk.auth.veauth.utils import get_credential_from_vefaas_iam |
| 18 | +from veadk.utils.logger import get_logger |
| 19 | +from veadk.utils.volcengine_sign import ve_request |
| 20 | + |
| 21 | +logger = get_logger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +def _get_api_key_id_by_project_id( |
| 25 | + project_id: str, access_key: str, secret_key: str, session_token: str, region: str |
| 26 | +): |
| 27 | + res = ve_request( |
| 28 | + request_body={"MemoryProjectId": project_id}, |
| 29 | + header={"X-Security-Token": session_token}, |
| 30 | + action="DescribeMemoryProjectDetail", |
| 31 | + ak=access_key, |
| 32 | + sk=secret_key, |
| 33 | + service="mem0", |
| 34 | + version="2025-10-10", |
| 35 | + region=region, |
| 36 | + host="open.volcengineapi.com", |
| 37 | + ) |
| 38 | + try: |
| 39 | + api_key_id = res["Result"]["APIKeyInfos"]["APIKeyId"] |
| 40 | + except KeyError: |
| 41 | + raise ValueError(f"Failed to get mem0 api key id: {res}") |
| 42 | + |
| 43 | + return api_key_id |
| 44 | + |
| 45 | + |
| 46 | +def _get_api_key_by_api_key_id( |
| 47 | + api_key_id: str, access_key: str, secret_key: str, session_token: str, region: str |
| 48 | +) -> str: |
| 49 | + res = ve_request( |
| 50 | + request_body={"APIKeyId": api_key_id}, |
| 51 | + header={"X-Security-Token": session_token}, |
| 52 | + action="DescribeAPIKeyDetail", |
| 53 | + ak=access_key, |
| 54 | + sk=secret_key, |
| 55 | + service="mem0", |
| 56 | + version="2025-10-10", |
| 57 | + region=region, |
| 58 | + host="open.volcengineapi.com", |
| 59 | + ) |
| 60 | + try: |
| 61 | + api_key = res["Result"]["APIKeyValue"] |
| 62 | + except KeyError: |
| 63 | + raise ValueError(f"Failed to get mem0 api key: {res}") |
| 64 | + |
| 65 | + return api_key |
| 66 | + |
| 67 | + |
| 68 | +def get_viking_mem0_token( |
| 69 | + api_key_id: str, memory_project_id: str, region: str = "cn-beijing" |
| 70 | +) -> str: |
| 71 | + logger.info("Fetching Viking mem0 token...") |
| 72 | + |
| 73 | + access_key = os.getenv("VOLCENGINE_ACCESS_KEY") |
| 74 | + secret_key = os.getenv("VOLCENGINE_SECRET_KEY") |
| 75 | + session_token = "" |
| 76 | + |
| 77 | + if not (access_key and secret_key): |
| 78 | + # try to get from vefaas iam |
| 79 | + cred = get_credential_from_vefaas_iam() |
| 80 | + access_key = cred.access_key_id |
| 81 | + secret_key = cred.secret_access_key |
| 82 | + session_token = cred.session_token |
| 83 | + |
| 84 | + if not api_key_id: |
| 85 | + api_key_id = _get_api_key_id_by_project_id( |
| 86 | + memory_project_id, access_key, secret_key, session_token, region |
| 87 | + ) |
| 88 | + |
| 89 | + return _get_api_key_by_api_key_id( |
| 90 | + api_key_id, access_key, secret_key, session_token, region |
| 91 | + ) |
0 commit comments