Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

community[patch]: truncate zhipuai temperature and top_p parameters to [0.01, 0.99] #20261

Merged
merged 8 commits into from
Apr 19, 2024
18 changes: 18 additions & 0 deletions libs/community/langchain_community/chat_models/zhipuai.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@
return default_class(content=content)


def _truncate_params(payload: Dict[str, Any]) -> None:
"""Truncate temperature and top_p parameters between [0.01, 0.99].

ZhipuAI only support temperature / top_p between (0, 1) open interval,
so we tuncate them to [0.01, 0.99].

Check failure on line 155 in libs/community/langchain_community/chat_models/zhipuai.py

View workflow job for this annotation

GitHub Actions / (Check for spelling errors)

tuncate ==> truncate
"""
temperature = payload.get("temperature")
top_p = payload.get("top_p")
if temperature is not None:
payload["temperature"] = max(0.01, min(0.99, temperature))
if top_p is not None:
payload["top_p"] = max(0.01, min(0.99, top_p))


class ChatZhipuAI(BaseChatModel):
"""
`ZhipuAI` large language chat models API.
Expand Down Expand Up @@ -309,6 +323,7 @@
"messages": message_dicts,
"stream": False,
}
_truncate_params(payload)
headers = {
"Authorization": _get_jwt_token(self.zhipuai_api_key),
"Accept": "application/json",
Expand All @@ -334,6 +349,7 @@
raise ValueError("Did not find zhipu_api_base.")
message_dicts, params = self._create_message_dicts(messages, stop)
payload = {**params, **kwargs, "messages": message_dicts, "stream": True}
_truncate_params(payload)
headers = {
"Authorization": _get_jwt_token(self.zhipuai_api_key),
"Accept": "application/json",
Expand Down Expand Up @@ -394,6 +410,7 @@
"messages": message_dicts,
"stream": False,
}
_truncate_params(payload)
headers = {
"Authorization": _get_jwt_token(self.zhipuai_api_key),
"Accept": "application/json",
Expand All @@ -418,6 +435,7 @@
raise ValueError("Did not find zhipu_api_base.")
message_dicts, params = self._create_message_dicts(messages, stop)
payload = {**params, **kwargs, "messages": message_dicts, "stream": True}
_truncate_params(payload)
headers = {
"Authorization": _get_jwt_token(self.zhipuai_api_key),
"Accept": "application/json",
Expand Down
Loading