- 在
configs/model_config.py
中的变量LLM_MODEL
修改为"chatglm3-6b-128k"
,并在变量llm_model_dict
中添加:
"chatglm3-6b-128k": {
"name": "chatglm-6b-int4-qe",
"pretrained_model_name": "THUDM/chatglm3-6b-128k",
# 下面一行替换你自己的路径。后面的步骤还要用到这个路径,所以将其称为<path>
"local_model_path": '<path>',
"provides": "ChatGLM"
},
- 在
<path>
和C:\Users\xxx\.cache\huggingface\modules\transformers_modules\chatglm3-6b-128k
(xxx是你的用户名)中各有一个tokenization_chatglm.py
脚本。找到类ChatGLMTokenizer
,在类中添加类函数:
def convert_history(self, history_int4):
history_128k = []
for interaction in history_int4:
user_content, assistant_content = interaction
# 添加用户或系统角色的内容
if user_content is not None:
history_128k.append({'role': 'user', 'content': user_content})
else:
history_128k.append({'role': 'system', 'content': ''}) # 假设系统消息内容为空
# 添加助手角色的内容
history_128k.append({'role': 'assistant', 'content': assistant_content})
return history_128k
- 在
ChatGLMTokenize
中找到函数build_chat_input
),将其替换为下面的函数(注意两个tokenization_chatglm.py
脚本都要替换:
def build_chat_input(self, query, history=None, role="user"):
if history is None:
history = []
input_ids = []
# List[str,str]格式的history修改为List[Dict]格式,适用于LangChain-ChatGLM项目接口
if history:
history = self.convert_history(history)
for item in history:
content = item["content"]
if item["role"] == "system" and "tools" in item:
content = content + "\n" + json.dumps(item["tools"], indent=4, ensure_ascii=False)
input_ids.extend(self.build_single_message(item["role"], item.get("metadata", ""), content))
input_ids.extend(self.build_single_message(role, "", query))
input_ids.extend([self.get_command("<|assistant|>")])
return self.batch_encode_plus([input_ids], return_tensors="pt", is_split_into_words=True)
Bingo,你的ChatGLM3-6b-128k模型已经可以完美适配langchain-ChatGLM了!
求个赞不过分吧,哈哈。