大模型部署手记(13)LLaMa2+Chinese-LLaMA-Plus-2-7B+Windows+LangChain+摘要问答
【摘要】 大模型部署手记(13)LLaMa2+Chinese-LLaMA-Plus-2-7B+Windows+LangChain+摘要问答
1.简介:
组织机构:Meta(Facebook)
代码仓:https://github.com/facebookresearch/llama
模型:chinese-alpaca-2-7b-hf、text2vec-large-chinese
下载:使用百度网盘和huggingface.co下载
硬件环境:暗影精灵7Plus
Windows版本:Windows 11家庭中文版 Insider Preview 22H2
内存 32G
GPU显卡:Nvidia GTX 3080 Laptop (16G)
LangChain是一个大模型框架,支持大模型加载、Prompt模板,以及将很多Chain串成一个任务。
在windows GPU下longchain 能否正常运行 中文LLaMA&Alpaca大模型?我们来试一下。
2.代码和模型下载:
cd \
![](https://pic1.zhimg.com/80/v2-de1aa5038e5a646edc6b82905b789e1e_720w.png?source=d16d100b)
准备一个合适的embedding model用于匹配过程中的文本/问题向量化
打开 https://huggingface.co/GanymedeNil/text2vec-large-chinese/tree/main
![](https://picx.zhimg.com/80/v2-8d23c77e16da56f10833a644eaed8e23_720w.jpeg?source=d16d100b)
![](https://picx.zhimg.com/80/v2-d9f71766bef39a597bd58b288fb82064_720w.jpeg?source=d16d100b)
![](https://pic1.zhimg.com/80/v2-492e4c3b688734e70c12d893ba830a7e_720w.jpeg?source=d16d100b)
下载 所有文件并存入 D:\Chinese-LLaMA-Alpaca-2\scripts\langchain\text2vec-large-chinese 目录下:
![](https://pic1.zhimg.com/80/v2-b4d2bf68c953a66c883193d2dcc2ea49_720w.png?source=d16d100b)
根据
![](https://picx.zhimg.com/80/v2-2b07d0dd4ff067c32de4e7db8ef4ecf0_720w.png?source=d16d100b)
直接下载完整版模型:
![](https://picx.zhimg.com/80/v2-2f36ddc9ed9d3d96f6eaf174ea5f65c2_720w.png?source=d16d100b)
![](https://pica.zhimg.com/80/v2-e9dffc96bf5b7a9662d347b72b9b4ca2_720w.png?source=d16d100b)
将下载好的Chinese-Alpaca-2-7B的完整版模型 保存到 D:\Chinese-LLaMA-Alpaca-2\scripts\langchain\chinese-alpaca-2-7b-hf 目录下。
![](https://picx.zhimg.com/80/v2-53c79f018bf4699faa7d33d58ae1bea3_720w.png?source=d16d100b)
3.安装依赖
conda deactivate
conda create -n langchain python=3.9
conda activate langchain
![](https://pica.zhimg.com/80/v2-4000af420f9f13b7c5adc702d7643fd1_720w.png?source=d16d100b)
pip install langchain
![](https://picx.zhimg.com/80/v2-f9eb228c121a723ce8c18b75436b60c3_720w.png?source=d16d100b)
![](https://picx.zhimg.com/80/v2-18d9eb384204dd04fe3ecc11262d2156_720w.png?source=d16d100b)
pip install sentence_transformers==2.2.2
![](https://picx.zhimg.com/80/v2-84e220ba231305e6df86ceea5e4adb58_720w.png?source=d16d100b)
![](https://pic1.zhimg.com/80/v2-b7bed3a6d5de06e3b65d85f1842047da_720w.png?source=d16d100b)
pip install pydantic==1.10.8
![](https://picx.zhimg.com/80/v2-40c9ae090a930c07112165e68e0f1452_720w.png?source=d16d100b)
pip install faiss-gpu==1.7.1
![](https://pic1.zhimg.com/80/v2-3c024f84ee9cf892f7f5cc146948c9ab_720w.png?source=d16d100b)
这个好像没有gpu版本?
换成CPU版本试试。。
pip install faiss-cpu==1.7.1
![](https://picx.zhimg.com/80/v2-f6d8f026aedf2e2e77cfe3dbc04408ad_720w.png?source=d16d100b)
4.部署验证
cd d:\Chinese-LLaMA-Alpaca-2
场景1:生成摘要
cd scripts/langchain
python langchain_sum.py --model_path chinese-alpaca-2-7b-hf --file_path doc.txt --chain_type refine
![](https://picx.zhimg.com/80/v2-ab948a0cf0a18545c1c6a8058d05da27_720w.png?source=d16d100b)
这里需要重装一下GPU版的torch
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
![](https://picx.zhimg.com/80/v2-5a5dfbc1ff371007e4c77b2ef3576d17_720w.png?source=d16d100b)
![](https://pica.zhimg.com/80/v2-a3f219ea033849ab9de32f9f599e9b63_720w.png?source=d16d100b)
再来:
python langchain_sum.py --model_path chinese-alpaca-2-7b-hf --file_path doc.txt --chain_type refine
![](https://picx.zhimg.com/80/v2-309b4b05d9cbc35c1b2ebbfec719177e_720w.png?source=d16d100b)
貌似字符集有什么问题。
参考
Python读取文件时出现UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0x80 in position ...blog.csdn.net/qq_31267769/article/details/109128882
将 langchain_sum.py
with open(file_path) as f:
改为
with open(file_path,'r',encoding='utf-8-sig') as f:
![](https://pic1.zhimg.com/80/v2-11a853f2e18c87fe4a483ed9a6341d4e_720w.png?source=d16d100b)
再来:
python langchain_sum.py --model_path chinese-alpaca-2-7b-hf --file_path doc.txt --chain_type refine
![](https://pic1.zhimg.com/80/v2-0f3f9fc1276f318ec7c7d4aa1c863cbf_720w.png?source=d16d100b)
缺protobuf库?
pip install protobuf
![](https://picx.zhimg.com/80/v2-6c137a719e1fd788c2f73eeaae7eed1f_720w.png?source=d16d100b)
再来:
python langchain_sum.py --model_path chinese-alpaca-2-7b-hf --file_path doc.txt --chain_type refine
![](https://pic1.zhimg.com/80/v2-f363189d8490202d59342dd5d2f0cd48_720w.png?source=d16d100b)
pip install accelerate
![](https://pica.zhimg.com/80/v2-eea0ad6ccc30cb93078dff49a57d1a96_720w.png?source=d16d100b)
再来:
python langchain_sum.py --model_path chinese-alpaca-2-7b-hf --file_path doc.txt --chain_type refine
![](https://picx.zhimg.com/80/v2-1fd0ce8f8f42eee15aa39c326bfed934_720w.png?source=d16d100b)
结果如下:
(langchain) PS D:\Chinese-LLaMA-Alpaca-2\scripts\langchain> python langchain_sum.py --model_path chinese-alpaca-2-7b-hf --file_path doc.txt --chain_type refine
L:\Anaconda\envs\langchain\lib\site-packages\langchain\__init__.py:40: UserWarning: Importing HuggingFacePipeline from langchain root module is no longer supported.
warnings.warn(
loading LLM...
Loading checkpoint shards: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:09<00:00, 4.50s/it]
L:\Anaconda\envs\langchain\lib\site-packages\transformers\generation\configuration_utils.py:362: UserWarning: `do_sample` is set to `False`. However, `temperature` is set to `0.9` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `temperature`. This was detected when initializing the generation config instance, which means the corresponding file may hold incorrect parameterization and should be fixed.
warnings.warn(
L:\Anaconda\envs\langchain\lib\site-packages\transformers\generation\configuration_utils.py:367: UserWarning: `do_sample` is set to `False`. However, `top_p` is set to `0.6` -- this flag is only used in sample-based generation modes. You should set `do_sample=True` or unset `top_p`. This was detected when initializing the generation config instance, which means the corresponding file may hold incorrect parameterization and should be fixed.
warnings.warn(
L:\Anaconda\envs\langchain\lib\site-packages\transformers\generation\utils.py:1421: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use and modify the model generation configuration (see https://huggingface.co/docs/transformers/generation_strategies#default-text-generation-configuration )
warnings.warn(
李白,唐朝著名诗人,字太白,号青莲居士,曾在唐玄宗时期受到赏识,但由于性格桀骜不驯被迫离开长安。他在洛阳结识了杜甫和高适,成为了好友。晚年时,他游历各地并参加 了抗击安史之乱的大军。然而,由于疾病的原因,他未能完成使命。最后,他在当涂逝世,享年61岁。尽管他的大部分作品已经失传,但他留下了一些著名的诗歌,如《将进酒》等。
(langchain) PS D:\Chinese-LLaMA-Alpaca-2\scripts\langchain>
李白,唐朝著名诗人,字太白,号青莲居士,曾在唐玄宗时期受到赏识,但由于性格桀骜不驯被迫离开长安。他在洛阳结识了杜甫和高适,成为了好友。晚年时,他游历各地并参加 了抗击安史之乱的大军。然而,由于疾病的原因,他未能完成使命。最后,他在当涂逝世,享年61岁。尽管他的大部分作品已经失传,但他留下了一些著名的诗歌,如《将进酒》等。
场景2:检索式问答
cd scripts/langchain
python langchain_qa.py --embedding_path text2vec-large-chinese --model_path chinese-alpaca-2-7b-hf --file_path doc.txt --chain_type refine
![](https://pica.zhimg.com/80/v2-e58d3360c027111bc8f77fa1e1d418b0_720w.png?source=d16d100b)
同样需要修改open部分,不过这次得修改 conda里面的了!
打开 L:\Anaconda\envs\langchain\lib\site-packages\langchain\document_loaders\text.py
![](https://picx.zhimg.com/80/v2-451fa758feffe982c9a1e745a2d57651_720w.png?source=d16d100b)
再来:
python langchain_qa.py --embedding_path text2vec-large-chinese --model_path chinese-alpaca-2-7b-hf --file_path doc.txt --chain_type refine
![](https://pica.zhimg.com/80/v2-3c35e6ad984f8d0e7e87bc2de0762590_720w.png?source=d16d100b)
pip install faiss-gpu
![](https://picx.zhimg.com/80/v2-67efdccfd64d5cd21e5b11dc23a494ce_720w.png?source=d16d100b)
![](https://pic1.zhimg.com/80/v2-366eb4599148a364f95055b98eec1e40_720w.png?source=d16d100b)
这个 faiss-gpu好像一直装不起来。
pip install --use-pep517 faiss-gpu
![](https://pic1.zhimg.com/80/v2-aeb45abd7595698aac52308029f05999_720w.png?source=d16d100b)
好像缺好多库。应该是requirements.txt没装。把requirements.txt文件里面的torch注释掉,安装一下:
cd D:\Chinese-LLaMA-Alpaca-2\
pip install -r requirements.txt
![](https://picx.zhimg.com/80/v2-b2493cb18e41d71c4abec479a53e9ea2_720w.png?source=d16d100b)
![](https://picx.zhimg.com/80/v2-f62d41a34c225842af1addbdcebcd8e3_720w.png?source=d16d100b)
再来:
cd scripts/langchain
python langchain_qa.py --embedding_path text2vec-large-chinese --model_path chinese-alpaca-2-7b-hf --file_path doc.txt --chain_type refine
![](https://picx.zhimg.com/80/v2-8b91786104d5f071dc8b164fee19ae6d_720w.png?source=d16d100b)
看来还是得搞定faiss-gpu
conda install faiss-gpu -c pytorch
![](https://pic1.zhimg.com/80/v2-53a1225e3a872941d95a18bb35bffa59_720w.png?source=d16d100b)
也不行。
conda也找不到这个包:
conda search faiss
![](https://pica.zhimg.com/80/v2-ad7d40a407b320a241166ab63dd7819c_720w.png?source=d16d100b)
根据提示到官网搜索:https://anaconda.org
![](https://pic1.zhimg.com/80/v2-66832a2afc9abd21944b63dfbf64ff2d_720w.png?source=d16d100b)
conda install faiss-gpu -c conda-forge
![](https://picx.zhimg.com/80/v2-1578e90f12eff31e244dd64ed055419f_720w.png?source=d16d100b)
![](https://pic1.zhimg.com/80/v2-d52d826d29d0cc874035f927aaedf53b_720w.png?source=d16d100b)
![](https://pic1.zhimg.com/80/v2-eab46f391b617f6309aa92c6240053ed_720w.png?source=d16d100b)
它连cuda都装了,动静有点大。。。。
![](https://pica.zhimg.com/80/v2-f502d573e46d4209c84fc7cdfd879db4_720w.png?source=d16d100b)
![](https://pic1.zhimg.com/80/v2-4e494c8fe20edc8a1e468477ebed7e88_720w.png?source=d16d100b)
再来:
python langchain_qa.py --embedding_path text2vec-large-chinese --model_path chinese-alpaca-2-7b-hf --file_path doc.txt --chain_type refine
![](https://picx.zhimg.com/80/v2-58539516403ee0190e46a8f4a02e2ba2_720w.png?source=d16d100b)
可以了!
回答的速度不是很快,但是也还不错了。
![](https://picx.zhimg.com/80/v2-9f55ae7e9489c0bb1dfdbf81f0c69ff9_720w.png?source=d16d100b)
(langchain) PS D:\Chinese-LLaMA-Alpaca-2\scripts\langchain> python langchain_qa.py --embedding_path text2vec-large-chinese --model_path chinese-alpaca-2-7b-hf --file_path doc.txt --chain_type refine
L:\Anaconda\envs\langchain\lib\site-packages\langchain\__init__.py:40: UserWarning: Importing HuggingFacePipeline from langchain root module is no longer supported.
warnings.warn(
Loading the embedding model...
No sentence-transformers model found with name text2vec-large-chinese. Creating a new one with MEAN pooling.
loading LLM...
You are using the legacy behaviour of the <class 'transformers.models.llama.tokenization_llama.LlamaTokenizer'>. This means that tokens that come after special tokens will not be properly handled. We recommend you to read the related pull request available at https://github.com/huggingface/transformers/pull/24565
Loading checkpoint shards: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:09<00:00, 4.84s/it]
Xformers is not installed correctly. If you want to use memory_efficient_attention to accelerate training use the following command to install Xformers
pip install xformers.
请输入问题:李白是谁?
L:\Anaconda\envs\langchain\lib\site-packages\transformers\generation\utils.py:1270: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use a generation configuration file (see https://huggingface.co/docs/transformers/main_classes/text_generation )
warnings.warn(
李白是唐代著名的浪漫主义诗人之一,以豪迈奔放的风格著称。
请输入问题:你是谁?
我是ChatGPT,一个由OpenAI训练的大型语言模型。
请输入问题:什么是敏捷开发?
敏捷开发是一种软件开发方法论,它强调快速响应变化的需求并通过迭代的方式进行开发。敏捷开发的核心理念包括个体和交互、工作的软件、客户合作等。敏捷开发通常采用短周 期的迭代开发模式,每个迭代称为一个"冲刺",每次冲刺持续一到四个星期,团队会完成一些可交付的产品功能并在冲刺结束后向客户展示成果。敏捷开发注重团队协作和沟通,鼓励频繁的反馈和改进,以适应不断变化的需求和环境。
请输入问题:国庆节放假几天?
国庆节放假七天
请输入问题:马上要上班了,不想去上班,怎么办?
如果您不想上班,可以考虑以下几种方法来应对这种情况:
1. 调整心态:尝试从积极的角度看待工作,思考自己为什么喜欢这份工作或者它对您的职业发展有何帮助。这样可以帮助您克服抵触情绪,重新激发工作的热情和动力。
2. 制定计划:将每天的工作任务分解成小目标,逐一完成。同时设定合理的时间限制,避免拖延或浪费时间。
3. 寻找支持:如果您感到无法独自面对工作中的问题,可以寻求同事、家人或朋友的支持和鼓励。他们可能能够提供一些建议或帮助您解决问题。
4. 放松身心:在上班前花些时间进行适当的休息和放松活动,例如运动、冥想或阅读等。这有助于减轻压力,提高专注度和效率。
总之,不要让消极的情绪影响到自己的工作表现。通过调整心态、制定计划、寻找支持以及放松身心等方式,您可以更好地应对不想上班的情况,保持良好的工作状态。
请输入问题:再见!
李白因为诗歌中的一些内容而被宫中所不容,最终被迫离开长安。
请输入问题:李白最有名得诗歌
李白最著名的作品之一是他的诗歌。他被认为是中国古代浪漫主义诗歌的代表人物,他的诗歌在中国文学史上占有重要地位。他的许多著名作品如《将进酒》、《庐山谣》、《夜泊 牛渚怀古》等至今仍被人们广泛传颂和吟唱。
请输入问题:说说阿富汗战争是怎么回事?
很抱歉,根据提供的信息无法回答该问题。因为所提供的背景知识和问题描述之间没有直接关联或相关性。
请输入问题:李白出生在什么地方?
李白出生在碎叶,属于唐安西都护府(今吉尔吉斯斯坦共和国楚河州托克马克市)。
请输入问题:那李白算是外国人还是中国人?
李白是汉族人,属于中国唐朝时期的著名诗人之一。
请输入问题:他不是出生在吉尔吉斯斯坦吗?
L:\Anaconda\envs\langchain\lib\site-packages\transformers\pipelines\base.py:1083: UserWarning: You seem to be using the pipelines sequentially on GPU. In order to maximize efficiency please use a dataset
warnings.warn(
是的,根据提供的信息,李白出生于唐朝的一个名叫碎叶的地方,也就是现在的吉尔吉斯斯坦地区。
请输入问题:那他怎么算是中国人呢?
李白是唐代著名的诗人之一,他的祖先可以追溯到汉代的李广将军,后来家族逐渐衰落并移居到了今天的甘肃省静宁县。虽然他出生地的具体位置并不确定,但据传说他曾经生活在 四川江油市的一个小镇上,也就是现在的青莲镇。因此,我们可以说他是中国古代汉族人民的一部分,并且具有中国文化传统的认同感。
请输入问题:Traceback (most recent call last):
File "D:\Chinese-LLaMA-Alpaca-2\scripts\langchain\langchain_qa.py", line 115, in <module>
query = input("请输入问题:")
KeyboardInterrupt
(langchain) PS D:\Chinese-LLaMA-Alpaca-2\scripts\langchain>
回头再看看第一个功能还能用不?
python langchain_sum.py --model_path chinese-alpaca-2-7b-hf --file_path doc.txt --chain_type refine
(langchain) PS D:\Chinese-LLaMA-Alpaca-2\scripts\langchain> python langchain_sum.py --model_path chinese-alpaca-2-7b-hf --file_path doc.txt --chain_type refine
L:\Anaconda\envs\langchain\lib\site-packages\langchain\__init__.py:40: UserWarning: Importing HuggingFacePipeline from langchain root module is no longer supported.
warnings.warn(
loading LLM...
You are using the legacy behaviour of the <class 'transformers.models.llama.tokenization_llama.LlamaTokenizer'>. This means that tokens that come after special tokens will not be properly handled. We recommend you to read the related pull request available at https://github.com/huggingface/transformers/pull/24565
Loading checkpoint shards: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:09<00:00, 4.67s/it]
Xformers is not installed correctly. If you want to use memory_efficient_attention to accelerate training use the following command to install Xformers
pip install xformers.
L:\Anaconda\envs\langchain\lib\site-packages\transformers\generation\utils.py:1270: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use a generation configuration file (see https://huggingface.co/docs/transformers/main_classes/text_generation )
warnings.warn(
李白,唐朝著名诗人之一,生于武则天大足元年(701年),祖籍陇西成纪(今甘肃省静宁西南),后徙居四川江油。少年时展现出非凡的才华,擅长诗词和剑术,并善于创作奇书和神仙故事。他拜赵蕤为师,对文学造诣产生了重要影响。成年后,李白游历中国各地,留下了许多脍炙人口的诗歌作品。但由于桀骜不驯的性格,他在唐玄宗天宝元年(742年)被逼 离开长安。晚年,李白经历多次流亡,包括安史之乱期间被迫流落夜郎。他晚年在江南一带漂泊,并在当涂(今属安徽省马鞍山)当县令的族叔李阳冰处逝世。李白一生创作大量的诗歌,绝大部分已散轶,现存约900余首。
摘要的结果如下:
李白,唐朝著名诗人之一,生于武则天大足元年(701年),祖籍陇西成纪(今甘肃省静宁西南),后徙居四川江油。少年时展现出非凡的才华,擅长诗词和剑术,并善于创作奇书和神仙故事。他拜赵蕤为师,对文学造诣产生了重要影响。成年后,李白游历中国各地,留下了许多脍炙人口的诗歌作品。但由于桀骜不驯的性格,他在唐玄宗天宝元年(742年)被逼 离开长安。晚年,李白经历多次流亡,包括安史之乱期间被迫流落夜郎。他晚年在江南一带漂泊,并在当涂(今属安徽省马鞍山)当县令的族叔李阳冰处逝世。李白一生创作大量的诗歌,绝大部分已散轶,现存约900余首。
不错,都成功运行了!
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)