StarCoder(150亿参数)是HuggingFace联合ServiceNow发布的免费大型语言模型,该模型经过训练主要用途是可以生成代码,目的是为了对抗GitHubCopilot和亚马逊CodeWhisperer等基于AI的编程工具。
其训练数据包含80多种不同的编程语言以及从GitHub中提取的文本。
安装首先,我们必须安装requirements.txt中列出的所有库
pipinstall-rrequirements.txt代码生成代码生成pipeline如下
fromtransformersimportAutoModelForCausalLM,AutoTokenizercheckpoint="bigcode/starcoder"device="cuda"#forGPUusageor"cpu"forCPUusagetokenizer=AutoTokenizer.from_pretrained(checkpoint)#tosavememoryconsiderusingfp16orbf16byspecifyingtorch.dtype=torch.float16forexamplemodel=AutoModelForCausalLM.from_pretrained(checkpoint).to(device)inputs=tokenizer.encode("defprint_hello_world():",return_tensors="pt").to(device)outputs=model.generate(inputs)print(tokenizer.decode(outputs[0]))或者
fromtransformersimportAutoModelForCausalLM,AutoTokenizer,pipelinecheckpoint="bigcode/starcoder"model=AutoModelForCausalLM.from_pretrained(checkpoint)tokenizer=AutoTokenizer.from_pretrained(checkpoint)pipe=pipeline("text-generation",model=model,tokenizer=tokenizer,device=0)print(pipe("defhello():"))
评论