问答社区
Qwen2.5-Coder[1] 是由阿里云Qwen团队开发的Qwen2.5大型语言模型系列
分类:语言模型
Qwen2.5-Coder[1] 是由阿里云Qwen团队开发的Qwen2.5大型语言模型系列的代码版本。该系列模型致力于推动开源代码语言模型(Open CodeLLMs)的发展。
项目特点
主要特点
-
强大:Qwen2.5-Coder-32B-Instruct是目前开源代码模型中的最佳模型,编码能力与GPT-4o相当。它不仅在编程能力上表现出色,还具备良好的通用和数学技能。 -
多样:在之前开源的1.5B/7B两种大小的基础上,本次发布了包括0.5B/3B/14B/32B在内的四种模型大小。至此,Qwen2.5-Coder已经覆盖了六种主流模型大小,以满足不同开发者的需求。 -
实用:在代码助手和Artifacts两个场景中探索了Qwen2.5-Coder的实用性,并通过一些示例展示了Qwen2.5-Coder在现实世界场景中的潜在应用。
使用场景
Qwen2.5-Coder支持长上下文理解和生成,上下文长度可达128K令牌,支持92种编程语言,包括常见的C、C++、Java、Python等。它在数学和通用能力上保留了基础模型的优势。
项目使用
环境要求
-
python>=3.9
-
transformers>4.37.0
快速开始
与Qwen2.5-Coder-32B-Instruct聊天
使用transformers
库中的几行代码即可与Qwen2.5-Coder-32B-Instruct进行聊天。以下是如何与Qwen2.5-Coder-32B-Instruct聊天的示例代码:
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen2.5-Coder-32B-Instruct"
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype="auto", device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_name)
prompt = "write a quick sort algorithm."
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)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(**model_inputs, max_new_tokens=512)
generated_ids = [output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
使用Qwen2.5-Coder-32B进行编码
-
基础用法
from transformers import AutoModelForCausalLM, AutoTokenizer
# 加载模型和分词器
model_name = "Qwen/Qwen2.5-Coder-32B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
# 编写提示
prompt = "Write a Python function that adds two numbers and returns the result."
input_ids = tokenizer.encode(prompt, return_tensors="pt")
# 生成代码
output = model.generate(input_ids, max_length=50)
generated_code = tokenizer.decode(output[0], skip_special_tokens=True)
# 打印生成的代码
print(generated_code)
-
文件级代码完成
准备一个包含部分代码的文件,希望模型帮助完成剩余的代码。Prompt结构如下所示:
prompt = '<|fim_prefix|>' + prefix_code + '<|fim_suffix|>' + suffix_code + '<|fim_middle|>'
from transformers import AutoTokenizer, AutoModelForCausalLM
# load model
device = "cuda" # the device to load the model onto
TOKENIZER = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-Coder-32B")
MODEL = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-Coder-32B", device_map="auto").eval()
input_text = """<|fim_prefix|>def quicksort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
<|fim_suffix|>
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quicksort(left) + middle + quicksort(right)<|fim_middle|>"""
model_inputs = TOKENIZER([input_text], return_tensors="pt").to(device)
# Use `max_new_tokens` to control the maximum output length.
generated_ids = MODEL.generate(model_inputs.input_ids, max_new_tokens=512, do_sample=False)[0]
# The generated_ids include prompt_ids, we only need to decode the tokens after prompt_ids.
output_text = TOKENIZER.decode(generated_ids[len(model_inputs.input_ids[0]):], skip_special_tokens=True)
print(f"Prompt: {input_text}\n\nGenerated text: {output_text}")
-
仓库级别代码完成方法
Prompt结构如下所示:
input_text = f'''<|repo_name|>{repo_name}
<|file_sep|>{file_path1}
{file_content1}
<|file_sep|>{file_path2}
{file_content2}'''
更多详细内容请查看项目仓库页面。
参考文档
-
Qwen教程[2] -
Qwen2.5-Coder技术报告[3] -
Discord[4] -
微信群[5]。
注:本文内容仅供参考,具体项目特性请参照官方 GitHub 页面的最新说明。
欢迎关注&点赞&在看,感谢你的阅读~
[2]Qwen教程: https://qwen.readthedocs.io/
[3]Qwen2.5-Coder技术报告: https://arxiv.org/abs/2409.12186
[4]Discord: https://discord.gg/z3GAxXZ9Ce
[5]微信群: https://github.com/QwenLM/Qwen/blob/main/assets/wechat.png
model name | type | length | Download |
---|---|---|---|
Qwen2.5-Coder-0.5B | base | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-1.5B | base | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-3B | base | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-7B | base | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-14B | base | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-32B | base | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-0.5B-instruct | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-1.5B-instruct | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-3B-instruct | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-7B-instruct | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-14B-instruct | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-32B-instruct | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-0.5B-Instruct-AWQ | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-0.5B-Instruct-GGUF | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-0.5B-Instruct-GPTQ-Int4 | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-0.5B-Instruct-GPTQ-Int8 | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-1.5B-Instruct-AWQ | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-1.5B-Instruct-GGUF | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-1.5B-Instruct-GPTQ-Int4 | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-1.5B-Instruct-GPTQ-Int8 | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-3B-Instruct-AWQ | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-3B-Instruct-GGUF | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-3B-Instruct-GPTQ-Int4 | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-3B-Instruct-GPTQ-Int8 | instruct | 32k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-7B-Instruct-AWQ | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-7B-Instruct-GGUF | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-7B-Instruct-GPTQ-Int4 | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-7B-Instruct-GPTQ-Int8 | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-14B-Instruct-AWQ | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-14B-Instruct-GGUF | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-14B-Instruct-GPTQ-Int4 | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-14B-Instruct-GPTQ-Int8 | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-32B-Instruct-AWQ | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-32B-Instruct-GGUF | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-32B-Instruct-GPTQ-Int4 | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |
Qwen2.5-Coder-32B-Instruct-GPTQ-Int8 | instruct | 128k | 🤗 Hugging Face • 🤖 ModelScope |