Skip to content

Commit 7a510a1

Browse files
author
jinhaiyang
committed
source_channel
1 parent 9ae2fec commit 7a510a1

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

zhipuai/_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ZhipuAI(HttpClient):
1818
chat: api_resource.chat.Chat
1919
api_key: str
2020
_disable_token_cache: bool = True
21+
source_channel: str
2122

2223
def __init__(
2324
self,
@@ -30,12 +31,14 @@ def __init__(
3031
custom_headers: Mapping[str, str] | None = None,
3132
disable_token_cache: bool = True,
3233
_strict_response_validation: bool = False,
34+
source_channel: str | None = None
3335
) -> None:
3436
if api_key is None:
3537
api_key = os.environ.get("ZHIPUAI_API_KEY")
3638
if api_key is None:
3739
raise ZhipuAIError("未提供api_key,请通过参数或环境变量提供")
3840
self.api_key = api_key
41+
self.source_channel = source_channel
3942
self._disable_token_cache = disable_token_cache
4043

4144
if base_url is None:
@@ -69,10 +72,11 @@ def __init__(
6972
@override
7073
def auth_headers(self) -> dict[str, str]:
7174
api_key = self.api_key
75+
source_channel = self.source_channel or "python-sdk"
7276
if self._disable_token_cache:
73-
return {"Authorization": f"Bearer {api_key}"}
77+
return {"Authorization": f"Bearer {api_key}","x-source-channel": source_channel}
7478
else:
75-
return {"Authorization": f"Bearer {_jwt_token.generate_token(api_key)}"}
79+
return {"Authorization": f"Bearer {_jwt_token.generate_token(api_key)}","x-source-channel": source_channel}
7680

7781
def __del__(self) -> None:
7882
if (not hasattr(self, "_has_custom_http_client")

0 commit comments

Comments
 (0)