Skip to content

Commit 163f12c

Browse files
binaryYukibinary-husky
authored andcommitted
# fix com_zhipuglm.py illegal temperature problem (binary-husky#1687)
* Update com_zhipuglm.py # fix 用户在使用 zhipuai 界面时遇到了关于温度参数的非法参数错误
1 parent bdd46c5 commit 163f12c

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

request_llms/com_zhipuglm.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ def __conversation_history(self, history:list, llm_kwargs:dict):
5555
messages.append(what_gpt_answer)
5656
return messages
5757

58+
@staticmethod
59+
def preprocess_param(param, default=0.95, min_val=0.01, max_val=0.99):
60+
"""预处理参数,保证其在允许范围内,并处理精度问题"""
61+
try:
62+
param = float(param)
63+
except ValueError:
64+
return default
65+
66+
if param <= min_val:
67+
return min_val
68+
elif param >= max_val:
69+
return max_val
70+
else:
71+
return round(param, 2) # 可挑选精度,目前是两位小数
72+
5873
def __conversation_message_payload(self, inputs:str, llm_kwargs:dict, history:list, system_prompt:str):
5974
messages = []
6075
if system_prompt:
@@ -64,11 +79,39 @@ def __conversation_message_payload(self, inputs:str, llm_kwargs:dict, history:li
6479
if inputs.strip() == "": # 处理空输入导致报错的问题 https://github.com/binary-husky/gpt_academic/issues/1640 提示 {"error":{"code":"1214","message":"messages[1]:content和tool_calls 字段不能同时为空"}
6580
inputs = "." # 空格、换行、空字符串都会报错,所以用最没有意义的一个点代替
6681
messages.append(self.__conversation_user(inputs, llm_kwargs)) # 处理用户对话
82+
"""
83+
采样温度,控制输出的随机性,必须为正数
84+
取值范围是:(0.0, 1.0),不能等于 0,默认值为 0.95,
85+
值越大,会使输出更随机,更具创造性;
86+
值越小,输出会更加稳定或确定
87+
建议您根据应用场景调整 top_p 或 temperature 参数,但不要同时调整两个参数
88+
"""
89+
temperature = self.preprocess_param(
90+
param=llm_kwargs.get('temperature', 0.95),
91+
default=0.95,
92+
min_val=0.01,
93+
max_val=0.99
94+
)
95+
"""
96+
用温度取样的另一种方法,称为核取样
97+
取值范围是:(0.0, 1.0) 开区间,
98+
不能等于 0 或 1,默认值为 0.7
99+
模型考虑具有 top_p 概率质量 tokens 的结果
100+
例如:0.1 意味着模型解码器只考虑从前 10% 的概率的候选集中取 tokens
101+
建议您根据应用场景调整 top_p 或 temperature 参数,
102+
但不要同时调整两个参数
103+
"""
104+
top_p = self.preprocess_param(
105+
param=llm_kwargs.get('top_p', 0.70),
106+
default=0.70,
107+
min_val=0.01,
108+
max_val=0.99
109+
)
67110
response = self.zhipu_bro.chat.completions.create(
68111
model=self.model, messages=messages, stream=True,
69-
temperature=llm_kwargs.get('temperature', 0.95) * 0.95, # 只能传默认的 temperature 和 top_p
70-
top_p=llm_kwargs.get('top_p', 0.7) * 0.7,
71-
max_tokens=llm_kwargs.get('max_tokens', 1024 * 4), # 最大输出模型的一半
112+
temperature=temperature,
113+
top_p=top_p,
114+
max_tokens=llm_kwargs.get('max_tokens', 1024 * 4),
72115
)
73116
return response
74117

0 commit comments

Comments
 (0)