Skip to content

Commit 6fd4142

Browse files
authored
Merge pull request #73 from MetaGLM/feature/source_channel
source_channel
2 parents a416cab + da5961e commit 6fd4142

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:
@@ -70,10 +73,11 @@ def __init__(
7073
@override
7174
def auth_headers(self) -> dict[str, str]:
7275
api_key = self.api_key
76+
source_channel = self.source_channel or "python-sdk"
7377
if self._disable_token_cache:
74-
return {"Authorization": f"Bearer {api_key}"}
78+
return {"Authorization": f"Bearer {api_key}","x-source-channel": source_channel}
7579
else:
76-
return {"Authorization": f"Bearer {_jwt_token.generate_token(api_key)}"}
80+
return {"Authorization": f"Bearer {_jwt_token.generate_token(api_key)}","x-source-channel": source_channel}
7781

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

0 commit comments

Comments
 (0)