-
Notifications
You must be signed in to change notification settings - Fork 15.9k
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
baskaryan
merged 8 commits into
langchain-ai:master
from
Congyuwang:fix-zhipuai-temperature
Apr 19, 2024
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
45d2606
truncate zhipuai temperature in [0.01, 0.99]
Congyuwang 622a9fd
also truncate top_p parameter
Congyuwang d045cca
Merge branch 'master' into fix-zhipuai-temperature
Congyuwang 35ac5e5
Merge branch 'master' into fix-zhipuai-temperature
baskaryan 64947f9
fix no-untyped-def lint
Congyuwang 4b97a13
Merge branch 'master' into fix-zhipuai-temperature
baskaryan c0503e1
fix typo
Congyuwang e1c7bdb
Merge branch 'master' into fix-zhipuai-temperature
baskaryan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 @@ def _default_params(self) -> Dict[str, Any]: | |
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. | ||
Comment on lines
-216
to
+230
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix some grammar in comment too. |
||
""" | ||
|
||
temperature: float = 0.95 | ||
|
@@ -309,6 +323,7 @@ def _generate( | |
"messages": message_dicts, | ||
"stream": False, | ||
} | ||
_truncate_params(payload) | ||
headers = { | ||
"Authorization": _get_jwt_token(self.zhipuai_api_key), | ||
"Accept": "application/json", | ||
|
@@ -334,6 +349,7 @@ def _stream( | |
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 @@ async def _agenerate( | |
"messages": message_dicts, | ||
"stream": False, | ||
} | ||
_truncate_params(payload) | ||
headers = { | ||
"Authorization": _get_jwt_token(self.zhipuai_api_key), | ||
"Accept": "application/json", | ||
|
@@ -418,6 +435,7 @@ async def _astream( | |
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", | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed typo in comment
truncate