THUDM / CodeGeeX2

CodeGeeX2: A More Powerful Multilingual Code Generation Model
https://codegeex.cn
Apache License 2.0
7.57k stars 533 forks source link

GodeGeex的prompt应该如何设置从而能够使用推理的方式实现代码注释、代码翻译等功能? #172

Open HaixHan opened 6 months ago

HaixHan commented 6 months ago

您好,我使用了你们CodeGeex的vscode 插件,性能表现的真的是amazing! 很感谢你们能够share如此棒的工作!! 但是,我还有一个疑问,当我下载codegeex2-6b 尝试infer时,模型并不能跟从指令来完成一些任务。方便告知使用模型权重来进行推理时,应该如何设置prompt吗?

例子:要求模型实现代码注释的功能

import os
os.environ['CUDA_VISIBLE_DEVICES'] = '1'

from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("/localFile/codegeex2-6b",trust_remote_code=True)
model = AutoModel.from_pretrained("/localFile/odegeex2-6b",device='cuda',trust_remote_code=True)  # 如使用CPU推理,device='cpu'
model = model.eval()

prompt = '''# language: Python\n#Explain the code line by line\n
def bubble_sort(list):
    for i in range(len(list) - 1):
        for j in range(len(list) - 1):
            if list[j] > list[j + 1]:
                list[j], list[j + 1] = list[j + 1], list[j]
    return list
print(bubble_sort([5, 2, 1, 8, 4]))
'''
print("prompt:\n",prompt)
inputs = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(inputs, max_length=256, top_k=1)  
response = tokenizer.decode(outputs[0][inputs.shape[-1]:])
print("response:\n",response)

运行结果:

image
lwinhong commented 4 months ago

我也有同样的疑问

lwinhong commented 4 months ago

我也有同样的疑问