StarCoder代码生成语言模型
StarCoder(150 亿参数)是 Hugging Face 联合 ServiceNow 发布的免费大型语言模型,该模型经过训练主要用途是可以生成代码,目的是为了对抗 GitHub Copilot 和亚马逊 CodeWhisperer 等基于 AI 的编程工具。
其训练数据包含 80 多种不同的编程语言以及从 GitHub 中提取的文本。
安装
首先,我们必须安装 requirements.txt
中列出的所有库
pip install -r requirements.txt
代码生成
代码生成 pipeline 如下
from transformers import AutoModelForCausalLM, AutoTokenizer checkpoint = "bigcode/starcoder" device = "cuda" # for GPU usage or "cpu" for CPU usage tokenizer = AutoTokenizer.from_pretrained(checkpoint) # to save memory consider using fp16 or bf16 by specifying torch.dtype=torch.float16 for example model = AutoModelForCausalLM.from_pretrained(checkpoint).to(device) inputs = tokenizer.encode("def print_hello_world():", return_tensors="pt").to(device) outputs = model.generate(inputs) print(tokenizer.decode(outputs[0]))
或者
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline checkpoint = "bigcode/starcoder" model = AutoModelForCausalLM.from_pretrained(checkpoint) tokenizer = AutoTokenizer.from_pretrained(checkpoint) pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0) print( pipe("def hello():") )
评论