一、ChatYuan-large-v2 模型
ChatYuan-large-v2
是一個開源的支持中英雙語的功能型對話語言大模型,與其他 LLM
不同的是模型十分輕量化,并且在輕量化的同時效果相對還不錯,僅僅通過0.7B
參數(shù)量就可以實現(xiàn)10B
模型的基礎(chǔ)效果,正是其如此的輕量級,使其可以在普通顯卡、 CPU
、甚至手機(jī)上進(jìn)行推理,而且 INT4
量化后的最低只需 400M
。
v2
版本相對于以前的 v1
版本,是使用了相同的技術(shù)方案,但在指令微調(diào)、人類反饋強(qiáng)化學(xué)習(xí)、思維鏈等方面進(jìn)行了優(yōu)化,主要優(yōu)化點如下所示:
- 增強(qiáng)了基礎(chǔ)能力。原有上下文問答、創(chuàng)意性寫作能力明顯提升。
- 新增了拒答能力。對于一些危險、有害的問題,學(xué)會了拒答處理。
- 新增了代碼生成功能。對于基礎(chǔ)代碼生成進(jìn)行了一定程度優(yōu)化。
- 新增了表格生成功能。使生成的表格內(nèi)容和格式更適配。
- 增強(qiáng)了基礎(chǔ)數(shù)學(xué)運算能力。
- 最大長度
token
數(shù)從1024
擴(kuò)展到4096
。 - 增強(qiáng)了模擬情景能力。
- 新增了中英雙語對話能力。
ChatYuan-large-v2
模型已經(jīng)發(fā)布到了 huggingface
中:
https://huggingface.co/ClueAI/ChatYuan-large-v2
開源項目地址:
https://github.com/clue-ai/ChatYuan
二、AutoModel 調(diào)用示例
由于ChatYuan-large-v2
已經(jīng)發(fā)布到 huggingface
中 ,因此在可以先使用 transformers
中的 AutoTokenizer
和 AutoModel
進(jìn)行調(diào)用體驗。
首先將下面鏈接中的文件下載到本地磁盤中:
https://huggingface.co/ClueAI/ChatYuan-large-v2/tree/main
調(diào)用實例:
# -*- coding: utf-8 -*-
from transformers import AutoTokenizer, AutoModel
import os
# 這里是模型下載的位置
model_dir = 'D:\\AIGC\\model\\ChatYuan-large-v2'
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
model = AutoModel.from_pretrained(model_dir, trust_remote_code=True)
history = []
while True:
query = input("\n用戶:")
if query == "stop":
break
if query == "clear":
history = []
os.system('clear')
continue
response, history = model.chat(tokenizer, query, history=history)
print(f"小元:{response}")
測試:
從上面的演示可以看到一些常見的對話都是OK
的,也可以為我們寫一些代碼,下面將上面的程序轉(zhuǎn)化為 Langchain
中的 LLM
進(jìn)行使用。
三、LangChain 集成
在 LangChain
中為我們提供了一個 HuggingFacePipeline
工具,可以輕松的將 HuggingFace
中的 pipeline
轉(zhuǎn)為 langchain
中的 LLM
,下面是調(diào)用實例:
# -*- coding: utf-8 -*-
from transformers import AutoTokenizer, AutoModel, pipeline
from langchain import HuggingFacePipeline
from langchain import PromptTemplate
import os
model_dir = 'D:\\AIGC\\model\\ChatYuan-large-v2'
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
model = AutoModel.from_pretrained(model_dir, trust_remote_code=True)
pipe = pipeline(
"text2text-generation",
model=model,
tokenizer=tokenizer,
max_length=512,
temperature=0.8,
top_p=1,
repetition_penalty=1.15
)
llm = HuggingFacePipeline(pipeline=pipe)
template = "用戶:{query} \n 小元:"
prompt = PromptTemplate(
input_variables=["query"],
template=template,
)
while True:
query = input("\n用戶:")
if query == "stop":
break
if query == "clear":
os.system('clear')
continue
response = llm(prompt.format(query=query))
print(f"小元:{response}")
測試效果:
四、場景使用探索
4.1 實體識別
提取文本中的 企業(yè)
和 地址
實體:
根據(jù)文本內(nèi)容,提取出"公司"、“地址” 信息, 文本內(nèi)容:阿里巴巴在江蘇南京有分公司嗎?
4.2 情感分析
根據(jù)文本內(nèi)容,判斷情感是正向還是負(fù)向, 文本內(nèi)容:前臺服務(wù)非常好,再接再厲!
根據(jù)文本內(nèi)容,判斷情感是正向還是負(fù)向, 文本內(nèi)容:飯菜口味很難吃!
4.3 文章分類
根據(jù)文本內(nèi)容進(jìn)行文章分類,分類如下: 新聞、體育、美食、健身, 文本內(nèi)容:蘇州的飯菜非常好吃,下次繼續(xù)來吃。
根據(jù)文本內(nèi)容進(jìn)行文章分類,分類如下: 新聞、體育、美食、健身, 文本內(nèi)容:好久沒運動了,我準(zhǔn)備每天跑步。
文章來源:http://www.zghlxwxcb.cn/news/detail-567768.html
4.4 文章生成
寫一個文章,內(nèi)容是關(guān)于美食的。
文章來源地址http://www.zghlxwxcb.cn/news/detail-567768.html
到了這里,關(guān)于LangChain 本地化方案 - 使用 ChatYuan-large-v2 作為 LLM 大語言模型的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!