??一、前言
? ? ?剛開始接觸AI時(shí),您可能會感到困惑,因?yàn)槊鎸Ρ姸嚅_源模型的選擇,不知道應(yīng)該選擇哪個模型,也不知道如何調(diào)用最基本的模型。但是不用擔(dān)心,我將陪伴您一起逐步入門,解決這些問題。
? ? ?在信息時(shí)代,我們可以輕松地通過互聯(lián)網(wǎng)獲取大量的理論知識和概念。然而,僅僅掌握理論知識并不能真正幫助我們成長和進(jìn)步。實(shí)踐是將理論知識轉(zhuǎn)化為實(shí)際技能和經(jīng)驗(yàn)的關(guān)鍵。
? ??我將引導(dǎo)您以最低的成本運(yùn)行ChatGLM3-6b模型,讓您體驗(yàn)到它帶來的美妙特性。
? ? qwen模型教程入口:
開源模型應(yīng)用落地-qwen模型小試-入門篇(一)_qwen文本分類-CSDN博客
? ??baichuan模型教程入口:
開源模型應(yīng)用落地-baichuan模型小試-入門篇(一)-CSDN博客
二、術(shù)語
2.1.?智譜AI
? ? 是由清華大學(xué)計(jì)算機(jī)系技術(shù)成果轉(zhuǎn)化而來的公司,致力于打造新一代認(rèn)知智能通用模型。公司合作研發(fā)了雙語千億級超大規(guī)模預(yù)訓(xùn)練模型GLM-130B,并構(gòu)建了高精度通用知識圖譜,形成數(shù)據(jù)與知識雙輪驅(qū)動的認(rèn)知引擎,基于此模型打造了ChatGLM(chatglm.cn)。此外,智譜AI還推出了認(rèn)知大模型平臺Bigmodel.ai,包括CodeGeeX和CogView等產(chǎn)品,提供智能API服務(wù),鏈接物理世界的億級用戶、賦能元宇宙數(shù)字人、成為具身機(jī)器人的基座,賦予機(jī)器像人一樣“思考”的能力。
2.2. ChatGLM3
? ??是智譜AI和清華大學(xué) KEG 實(shí)驗(yàn)室聯(lián)合發(fā)布的對話預(yù)訓(xùn)練模型。ChatGLM3-6B 是 ChatGLM3 系列中的開源模型,在保留了前兩代模型對話流暢、部署門檻低等眾多優(yōu)秀特性的基礎(chǔ)上,ChatGLM3-6B 引入了如下特性:
- 更強(qiáng)大的基礎(chǔ)模型:?ChatGLM3-6B 的基礎(chǔ)模型 ChatGLM3-6B-Base 采用了更多樣的訓(xùn)練數(shù)據(jù)、更充分的訓(xùn)練步數(shù)和更合理的訓(xùn)練策略。在語義、數(shù)學(xué)、推理、代碼、知識等不同角度的數(shù)據(jù)集上測評顯示,*?ChatGLM3-6B-Base 具有在 10B 以下的基礎(chǔ)模型中最強(qiáng)的性能*。
- 更完整的功能支持:?ChatGLM3-6B 采用了全新設(shè)計(jì)的?Prompt 格式?,除正常的多輪對話外。同時(shí)原生支持工具調(diào)用(Function Call)、代碼執(zhí)行(Code Interpreter)和 Agent 任務(wù)等復(fù)雜場景。
- 更全面的開源序列:?除了對話模型?ChatGLM3-6B?外,還開源了基礎(chǔ)模型?ChatGLM3-6B-Base?、長文本對話模型?ChatGLM3-6B-32K?和進(jìn)一步強(qiáng)化了對于長文本理解能力的?ChatGLM3-6B-128K。以上所有權(quán)重對學(xué)術(shù)研究完全開放?,在填寫?問卷?進(jìn)行登記后亦允許免費(fèi)商業(yè)使用。
三、前置條件
3.1. windows操作系統(tǒng)
3.2.?下載chatglm3-6b模型
從huggingface下載:https://huggingface.co/THUDM/chatglm3-6b/tree/main
從魔搭下載:魔搭社區(qū)匯聚各領(lǐng)域最先進(jìn)的機(jī)器學(xué)習(xí)模型,提供模型探索體驗(yàn)、推理、訓(xùn)練、部署和應(yīng)用的一站式服務(wù)。https://www.modelscope.cn/models/ZhipuAI/chatglm3-6b/fileshttps://www.modelscope.cn/models/ZhipuAI/chatglm3-6b/files
3.3. 創(chuàng)建虛擬環(huán)境&安裝依賴
conda create --name chatglm3 python=3.10
conda activate chatglm3
pip install protobuf transformers==4.30.2 cpm_kernels torch>=2.0 sentencepiece accelerate
四、技術(shù)實(shí)現(xiàn)
4.1. 本地推理
#Tokenizer加載
def loadTokenizer():
tokenizer = AutoTokenizer.from_pretrained(modelPath, use_fast=False, trust_remote_code=True)
return tokenizer
#Model加載
def loadModel():
model = AutoModelForCausalLM.from_pretrained(modelPath, device_map="auto", trust_remote_code=True).eval()
print(model)
return model
#推理執(zhí)行結(jié)果如下:
def chat(model, tokenizer, message):
try:
for response in model.stream_chat(tokenizer, message):
_answer,_history = response
yield _answer
except Exception:
traceback.print_exc()
4.2. 完整代碼
# -*- coding = utf-8 -*-
from transformers import AutoTokenizer, AutoModelForCausalLM
import time
import traceback
modelPath = "E:\\model\\chatglm3-6b"
def chat(model, tokenizer, message):
try:
for response in model.stream_chat(tokenizer, message):
_answer,_history = response
yield _answer
except Exception:
traceback.print_exc()
def loadTokenizer():
tokenizer = AutoTokenizer.from_pretrained(modelPath, use_fast=False, trust_remote_code=True)
return tokenizer
def loadModel():
model = AutoModelForCausalLM.from_pretrained(modelPath, device_map="auto", trust_remote_code=True).eval()
#print(model)
return model
if __name__ == '__main__':
model = loadModel()
tokenizer = loadTokenizer()
message = "你是誰?"
response = chat(model, tokenizer, message)
for answer in response:
print(answer)
運(yùn)行結(jié)果:
五、附帶說明
5.1.ChatGLM3-6B vs Qwen1.5-7B-Chat
1) 推理實(shí)現(xiàn)的代碼差異
ChatGLM3:
? ? for response in?model.stream_chat(tokenizer,message, history)
? ? ? ?......
QWen1.5:
? ? generation_kwargs = dict(inputs=model_inputs.input_ids, streamer=streamer)
? ? thread = Thread(target=model.generate, kwargs=generation_kwargs)
? ? thread.start()
? ? ? ? for response?in streamer:
? ? ? ? ? ? ......
2) 對話格式差異
ChatGLM3:
<|system|>
You are a helpful assistant.
<|user|>
我家在廣州,很好玩哦
<|assistant|>
廣州是一個美麗的城市,有很多有趣的地方可以去。
QWen1.5:文章來源:http://www.zghlxwxcb.cn/news/detail-845980.html
<|im_start|>system
You are a helpful assistant.<|im_end|>
<|im_start|>user
我家在廣州,很好玩哦<|im_end|>
<|im_start|>assistant
廣州是一個美麗的城市,有很多有趣的地方可以去。<|im_end|>
3) 模型參數(shù)文章來源地址http://www.zghlxwxcb.cn/news/detail-845980.html
ChatGLM3-6B | QWen1.5-7B-Chat | |
n_layers | 28 | 32 |
n_heads | 32 | 32 |
vocab size | 65,024 | 151,851 |
sequence length | 8192 | 8192 |
到了這里,關(guān)于開源模型應(yīng)用落地-chatglm3-6b模型小試-入門篇(一)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!