如何做高大上的網(wǎng)站 知乎企業(yè)營(yíng)銷網(wǎng)站建設(shè)系統(tǒng)
使用Transformers來使用模型
如希望使用Qwen-chat進(jìn)行推理,所需要寫的只是如下所示的數(shù)行代碼。請(qǐng)確保你使用的是最新代碼,并指定正確的模型名稱和路徑,如Qwen/Qwen-7B-Chat
和Qwen/Qwen-14B-Chat
這里給出了一段代碼
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation import GenerationConfig# 可選的模型包括: "Qwen/Qwen-7B-Chat", "Qwen/Qwen-14B-Chat"
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True)# 打開bf16精度,A100、H100、RTX3060、RTX3070等顯卡建議啟用以節(jié)省顯存
# model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-7B-Chat", device_map="auto", trust_remote_code=True, bf16=True).eval()
# 打開fp16精度,V100、P100、T4等顯卡建議啟用以節(jié)省顯存
# model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-7B-Chat", device_map="auto", trust_remote_code=True, fp16=True).eval()
# 使用CPU進(jìn)行推理,需要約32GB內(nèi)存
# model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-7B-Chat", device_map="cpu", trust_remote_code=True).eval()
# 默認(rèn)使用自動(dòng)模式,根據(jù)設(shè)備自動(dòng)選擇精度
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-7B-Chat", device_map="auto", trust_remote_code=True).eval()# 可指定不同的生成長(zhǎng)度、top_p等相關(guān)超參
model.generation_config = GenerationConfig.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True)# 第一輪對(duì)話
response, history = model.chat(tokenizer, "你好", history=None)
print(response)
# 你好!很高興為你提供幫助。# 第二輪對(duì)話
response, history = model.chat(tokenizer, "給我講一個(gè)年輕人奮斗創(chuàng)業(yè)最終取得成功的故事。", history=history)
print(response)
# 這是一個(gè)關(guān)于一個(gè)年輕人奮斗創(chuàng)業(yè)最終取得成功的故事。
# 故事的主人公叫李明,他來自一個(gè)普通的家庭,父母都是普通的工人。從小,李明就立下了一個(gè)目標(biāo):要成為一名成功的企業(yè)家。
# 為了實(shí)現(xiàn)這個(gè)目標(biāo),李明勤奮學(xué)習(xí),考上了大學(xué)。在大學(xué)期間