Skip to content

Commit e4393fa

Browse files
author
hexiaochun
committed
feat: support api key
1 parent 2068ee2 commit e4393fa

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

volcenginesdkarkruntime/_client.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def __init__(
3838
base_url: str | URL = BASE_URL,
3939
ak: str | None = None,
4040
sk: str | None = None,
41+
api_key: str | None = None,
4142
timeout: float | Timeout | None = DEFAULT_TIMEOUT,
4243
max_retries: int = DEFAULT_MAX_RETRIES,
4344
http_client: Client | None = None,
@@ -48,6 +49,7 @@ def __init__(
4849
Args:
4950
ak: access key id
5051
sk: secret access key
52+
api_key: api key,this api key will not be refreshed
5153
timeout: timeout of client. default httpx.Timeout(timeout=60.0, connect=60.0)
5254
max_retries: times of retry when request failed. default 1
5355
http_client: specify customized http_client
@@ -59,8 +61,13 @@ def __init__(
5961
ak = os.environ.get("VOLC_ACCESSKEY")
6062
if sk is None:
6163
sk = os.environ.get("VOLC_SECRETKEY")
64+
if api_key is None:
65+
api_key = os.environ.get("ARK_API_KEY")
6266
self.ak = ak
6367
self.sk = sk
68+
self.api_key = api_key
69+
70+
assert (api_key is not None) or (ak is not None and sk is not None), "you need to support api_key or ak&sk"
6471

6572
super().__init__(
6673
base_url=base_url,
@@ -82,6 +89,11 @@ def _get_endpoint_sts_token(self, endpoint_id: str):
8289
self._sts_token_manager = StsTokenManager(self.ak, self.sk)
8390
return self._sts_token_manager.get(endpoint_id)
8491

92+
@property
93+
def auth_headers(self) -> dict[str, str]:
94+
api_key = self.api_key
95+
return {"Authorization": f"Bearer {api_key}"}
96+
8597

8698
class AsyncArk(AsyncAPIClient):
8799
chat: resources.AsyncChat
@@ -91,6 +103,7 @@ def __init__(
91103
*,
92104
ak: str | None = None,
93105
sk: str | None = None,
106+
api_key: str | None = None,
94107
base_url: str | URL = BASE_URL,
95108
timeout: float | Timeout | None = DEFAULT_TIMEOUT,
96109
max_retries: int = DEFAULT_MAX_RETRIES,
@@ -101,6 +114,7 @@ def __init__(
101114
Args:
102115
ak: access key id
103116
sk: secret access key
117+
api_key: api key,this api key will not be refreshed
104118
timeout: timeout of client. default httpx.Timeout(timeout=60.0, connect=60.0)
105119
max_retries: times of retry when request failed. default 1
106120
http_client: specify customized http_client
@@ -112,8 +126,13 @@ def __init__(
112126
ak = os.environ.get("VOLC_ACCESSKEY")
113127
if sk is None:
114128
sk = os.environ.get("VOLC_SECRETKEY")
129+
if api_key is None:
130+
api_key = os.environ.get("ARK_API_KEY")
115131
self.ak = ak
116132
self.sk = sk
133+
self.api_key = api_key
134+
135+
assert (api_key is not None) or (ak is not None and sk is not None), "you need to support api_key or ak&sk"
117136

118137
super().__init__(
119138
base_url=base_url,
@@ -135,6 +154,11 @@ def _get_endpoint_sts_token(self, endpoint_id: str):
135154
self._sts_token_manager = StsTokenManager(self.ak, self.sk)
136155
return self._sts_token_manager.get(endpoint_id)
137156

157+
@property
158+
def auth_headers(self) -> dict[str, str]:
159+
api_key = self.api_key
160+
return {"Authorization": f"Bearer {api_key}"}
161+
138162

139163
class StsTokenManager(object):
140164

volcenginesdkarkruntime/_utils/_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ def _insert_sts_token(args, kwargs):
8080

8181
ark_client = args[0]._client
8282
model = kwargs.get("model", "")
83-
if model and model.startswith("ep-") and ark_client.ak and ark_client.sk:
83+
if ark_client.api_key is None and model and model.startswith("ep-") and ark_client.ak and ark_client.sk:
8484
default_auth_header = {"Authorization": "Bearer " + ark_client._get_endpoint_sts_token(model)}
8585
kwargs["extra_headers"] = {**default_auth_header, **kwargs.get("extra_headers", {})}

volcenginesdkexamples/volcenginesdkarkruntime/example_async_chat_completions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
from volcenginesdkarkruntime import AsyncArk
44

5-
# fetch ak&sk from environmental variables "VOLC_ACCESSKEY", "VOLC_SECRETKEY"
5+
# Support ak&sk or api key
6+
# 1. Fetch ak&sk from environmental variables "VOLC_ACCESSKEY", "VOLC_SECRETKEY"
67
# or specify ak&sk by Ark(ak="${YOUR_AK}", sk="${YOUR_SK}").
78
# you can get ak&sk follow this document(https://www.volcengine.com/docs/6291/65568)
9+
#
10+
# 2. Fetch api key from environmental variables "ARK_API_KEY"
11+
# or specify api key by Ark(api_key="${YOUR_API_KEY}").
12+
# Note: if you support api key,this api key will not be refreshed.
13+
# If you don't want the api to fail after a period of time, to the api key that never expires.
814
client = AsyncArk()
915

1016

volcenginesdkexamples/volcenginesdkarkruntime/example_chat_completions.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
from volcenginesdkarkruntime import Ark
22

3-
# fetch ak&sk from environmental variables "VOLC_ACCESSKEY", "VOLC_SECRETKEY"
3+
# Support ak&sk or api key
4+
# 1. Fetch ak&sk from environmental variables "VOLC_ACCESSKEY", "VOLC_SECRETKEY"
45
# or specify ak&sk by Ark(ak="${YOUR_AK}", sk="${YOUR_SK}").
56
# you can get ak&sk follow this document(https://www.volcengine.com/docs/6291/65568)
7+
#
8+
# 2. Fetch api key from environmental variables "ARK_API_KEY"
9+
# or specify api key by Ark(api_key="${YOUR_API_KEY}").
10+
# Note: if you support api key,this api key will not be refreshed.
11+
# If you don't want the api to fail after a period of time, to the api key that never expires.
612
client = Ark()
713

814
if __name__ == "__main__":
@@ -19,6 +25,26 @@
1925
)
2026
print(completion.choices[0].message.content)
2127

28+
print("----- multiple rounds request -----")
29+
completion = client.chat.completions.create(
30+
model="${YOUR_ENDPOINT_ID}",
31+
messages=[
32+
{
33+
"role": "user",
34+
"content": "Say this is a test",
35+
},
36+
{
37+
"role": "assistant",
38+
"content": "this is a test",
39+
},
40+
{
41+
"role": "user",
42+
"content": "what have you say?",
43+
},
44+
]
45+
)
46+
print(completion.choices[0].message.content)
47+
2248
# Streaming:
2349
print("----- streaming request -----")
2450
stream = client.chat.completions.create(

0 commit comments

Comments
 (0)