Qwen2.5-Coder 简介
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}nnGenerated 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 页面的最新说明。
欢迎关注&点赞&在看,感谢你的阅读~
Github地址: https://github.com/QwenLM/Qwen2.5-Coder
[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
原文始发于微信公众号(AIGC创想者):1.7K+ Star!Qwen2.5-Coder系列:强大、多样、实用
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/314878.html