glm4-chat 使用 Xinference 推理踩坑

出现错误

GLM-4 chat 9b:'ChatGLMForConditionalGeneration' object has no attribute 'stream_chat'

解决方案步骤:

  1. 将原模型文件中的 modeling_chatglm.py 替换为THUDM/glm-4-9b-chat-1m 里面的modeling_chatglm.py ,并且对文件进行两处修改 具体修改内容如下:
    ## 源文件
    def _update_model_kwargs_for_generation(
                self,
                outputs: ModelOutput,
                model_kwargs: Dict[str, Any],
                is_encoder_decoder: bool = False,
        ) -> Dict[str, Any]:
    #修改之后
      def _update_model_kwargs_for_generation(
                self,
                outputs: ModelOutput,
                model_kwargs: Dict[str, Any],
                is_encoder_decoder: bool = False,
                standardize_cache_format: bool = False, #添加了这个变量
        ) -> Dict[str, Any]:
    
    ## 源文件
    # update past_key_values
            model_kwargs["past_key_values"] = self._extract_past_from_model_output(
                outputs, standardize_cache_format=standardize_cache_format
            )
    ## 替换
     # update past_key_values
            past_output = self._extract_past_from_model_output(
                outputs, standardize_cache_format=standardize_cache_format
            )
            # adapt transformers update (https://github.com/huggingface/transformers/pull/31116)
            if(type(past_output) is tuple and type(past_output[0]) is str):
                if past_output[0]=="past_key_values":
                    model_kwargs["past_key_values"] = past_output[1]
                else:
                    model_kwargs["past_key_values"] = None
                    print(f"WARN: Get \"{past_output[0]}\" during self._extract_past_from_model_output, not \"past_key_values\"")
            else:
                model_kwargs["past_key_values"] = past_output

  2. 出现 'GenerationConfig' object has no attribute '_eos_token_tensor' 

       降低transformers的版本,使用 transformers== 4.42.0

首先,你需要下载 Chat-GLM-6B-Int4 模型文件和相应的配置文件。然后,你需要在你的 Linux 系统上安装 PyTorch 和 Transformers 库。你可以使用以下命令安装这两个库: ``` pip install torch pip install transformers ``` 接下来,你需要编写一个 Python 脚本来部署 Chat-GLM-6B-Int4 模型。你可以使用以下代码作为脚本的基础: ```python import torch from transformers import GPT2Tokenizer, GPT2LMHeadModel # 加载模型和分词器 tokenizer = GPT2Tokenizer.from_pretrained('chat-glm-6b-int4/config') model = GPT2LMHeadModel.from_pretrained('chat-glm-6b-int4/model') # 指定设备 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) # 对话循环 while True: # 获取用户输入 user_input = input("You: ") # 把用户输入转换成模型的输入格式 input_ids = tokenizer.encode(user_input, return_tensors='pt').to(device) # 在模型上生成回复 output = model.generate(input_ids, max_length=1000, do_sample=True, temperature=0.7) response = tokenizer.decode(output[0], skip_special_tokens=True) # 输出回复 print("Chat-GLM-6B-Int4: " + response) ``` 在这个脚本中,我们首先加载 Chat-GLM-6B-Int4 模型和相应的分词器。然后,我们检查可用的设备并将模型移动到相应的设备上。最后,我们开始一个对话循环,其中用户输入被转换为模型的输入格式,并在模型上生成回复。回复被解码并输出给用户。 注意,这个脚本只是一个基础示例,你需要根据你的具体需求进行修改。例如,你可能需要添加一个 Web 接口来允许用户通过浏览器与 Chat-GLM-6B-Int4 进行交互。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值