在langchain-ChatGLM项目中引入ChatGLM3-6b-128k

文章介绍了如何在model_config.py中将LLM_MODEL设置为chatglm3-6b-128k,并指导如何在tokenization_chatglm.py中修改类函数以适应新的模型结构,包括处理历史对话记录和构建输入。最终目标是使ChatGLM3-6b-128k模型与LangChain-ChatGLM项目兼容。
摘要由CSDN通过智能技术生成
  1. 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"
},
  1. <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
  1. 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了!
求个赞不过分吧,哈哈。

  • 11
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值