prompt = "Give me a short introduction to artificial intelligence."
# 构建包含系统角色和用户角色的消息列表
messages = [
{"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
{"role": "user", "content": prompt}
]
# 将消息列表应用聊天模板进行处理,不进行分词操作且添加生成提示
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
text
复制代码
输出:
'<|im_start|>system\nYou are Qwen, created by Alibaba Cloud. You are a helpful assistant.<|im_end|>\n<|im_start|>user\nGive me a short introduction to artificial intelligence.<|im_end|>\n<|im_start|>assistant\n'
'Certainly! Artificial Intelligence (AI) refers to the simulation of human intelligence in machines that are programmed to think, learn, and perform tasks that typically require human cognition. This includes activities such as visual perception, speech recognition, decision-making, and language translation.\n\nKey components of AI include:\n\n1. **Machine Learning**: A subset of AI where systems can automatically learn and improve from experience without being explicitly programmed.\n2. **Deep Learning**: A more advanced form of machine learning that uses neural networks with many layers to model and solve complex problems.\n3. **Natural Language Processing (NLP)**: The ability of computers to understand, interpret, and generate human language.\n4. **Computer Vision**: Enabling machines to interpret and understand the visual world, similar to how humans would.\n5. **Robotics**: Combining AI with robotics to create machines that can perform tasks autonomously or semi-autonomously.\n\nAI has numerous applications across various fields, including healthcare, finance, transportation, entertainment, and more. It continues to evolve rapidly, transforming industries and societies in profound ways.'
复制代码
完备代码如下
from modelscope import AutoModelForCausalLM, AutoTokenizer