请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

章节4.10的代码报错

#模型下载
from modelscope import snapshot_download
from transformers import AutoModelForCausalLM,AutoTokenizer

# 推理函数
def generate_txet(prompt):
    model_dir = snapshot_download('Qwen/Qwen3-0.6B', cache_dir = "./")
    tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")
    model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen3-0.6B")
    #1 构造输入
    messages = [
    {"role": "user", "content": prompt}
    ]  
    text = tokenizer.apply_chat_template(
        messages,
        tokenizer=False,
        add_generation_promot= True,
        enable_thinking=True
    )
    # Tokenizer
    model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
    # 生成输出
    generated_ids = model.generate(
         **model_inputs,
         max_new_tokens=32768
    )
    output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() 
    try:
        # rindex finding 151668 (</think>)
        index = len(output_ids) - output_ids[::-1].index(151668)
    except ValueError:
        index = 0
    thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
    content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
    
    '''
    print("thinking content:", thinking_content)
    print("content:", content)
    '''
    return thinking_content + content
	
	
if __name__ == "__main__":
    
    generate_txet(["人工智能对社会的影响是什么"])

报错内容

root@I2515370b0e0040194b:~/chensq/course_code/chapter3# /bin/python3.11 /root/chensq/course_code/chapter3/chensq_test.py
Downloading Model from https://www.modelscope.cn to directory: ./Qwen/Qwen3-0.6B
2025-11-19 18:44:45,366 - modelscope - INFO - Target directory already exists, skipping creation.
Traceback (most recent call last):
File “/root/chensq/course_code/chapter3/chensq_test.py”, line 45, in
generate_txet(“人工智能对社会的影响是什么”)
File “/root/chensq/course_code/chapter3/chensq_test.py”, line 21, in generate_txet
model_inputs = tokenizer([text], return_tensors=“pt”).to(model.device)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/dist-packages/transformers/tokenization_utils_base.py”, line 2938, in call
encodings = self._call_one(text=text, text_pair=text_pair, **all_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.11/dist-packages/transformers/tokenization_utils_base.py”, line 2998, in _call_one
raise ValueError(
ValueError: text input must be of type str (single example), list[str] (batch or single pretokenized example) or list[list[str]]

正在回答 回答被采纳积分+3

2回答

提问者 陈素素 2025-11-20 18:54:08

generate_txet(["人工智能对社会的影响是什么"]) ,老师我是这么调用的,有什么不对吗?


0 回复 有任何疑惑可以回复我~
  • 这里是调用是输入字符串哈
    回复 有任何疑惑可以回复我~ 2025-11-21 15:08:18
Dream哥 2025-11-20 10:34:53
调用的时候要输入字符串列表
0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号