Skip to content

The agent engineering platform.

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

Congyudd5139e30406
diff --git a/libs/community/langchain_community/chat_models/zhipuai.py b/libs/community/langchain_community/chat_models/zhipuai.py
index 6b98ff54e2..5d9dd0eb88 100644
--- a/libs/community/langchain_community/chat_models/zhipuai.py
+++ b/libs/community/langchain_community/chat_models/zhipuai.py
@@ -148,6 +148,20 @@ def _convert_delta_to_message_chunk(
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 truncate them to [0.01, 0.99].
+ """
+ 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.
@@ -213,7 +227,7 @@ class ChatZhipuAI(BaseChatModel):
model_name: Optional[str] = Field(default="glm-4", alias="model")
"""
Model name to use, see 'https://open.bigmodel.cn/dev/api#language'.
- or you can use any finetune model of glm series.
+ Alternatively, you can use any fine-tuned model from the GLM series.
"""
temperature: float = 0.95
@@ -309,6 +323,7 @@ class ChatZhipuAI(BaseChatModel):
"messages": message_dicts,
"stream": False,
}
+ _truncate_params(payload)
headers = {
"Authorization": _get_jwt_token(self.zhipuai_api_key),
"Accept": "application/json",
@@ -334,6 +349,7 @@ class ChatZhipuAI(BaseChatModel):
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",
@@ -394,6 +410,7 @@ class ChatZhipuAI(BaseChatModel):
"messages": message_dicts,
"stream": False,
}
+ _truncate_params(payload)
headers = {
"Authorization": _get_jwt_token(self.zhipuai_api_key),
"Accept": "application/json",
@@ -418,6 +435,7 @@ class ChatZhipuAI(BaseChatModel):
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",