大模型部署手记(13)LLaMa2+Chinese-LLaMA-Plus-2-7B+Windows+LangChain+摘要问答

1.简介:

组织机构:Meta(Facebook)

代码仓:GitHub – facebookresearch/llama: Inference code for LLaMA models

模型:chinese-alpaca-2-7b-hf、text2vec-large-chinese

下载:使用百度网盘和huggingface.co下载

硬件环境:暗影精灵7Plus

Windows版本:Windows 11家庭中文版 Insider Preview 22H2

内存 32G

GPU显卡:Nvidia GTX 3080 Laptop (16G)

阅读这篇:https://zhuanlan.zhihu.com/p/644500258

LangChain是一个大模型框架,支持大模型加载、Prompt模板,以及将很多Chain串成一个任务。

官网地址为:https://docs.langchain.com/docs/

在windows GPU下longchain 能否正常运行 中文LLaMA&Alpaca大模型?我们来试一下。

2.代码和模型下载:

参考 Home · ymcui/Chinese-LLaMA-Alpaca-2 Wiki · GitHub

cd \

git clone GitHub – langchain-ai/langchain: ⚡ Building applications with LLMs through composability ⚡

准备一个合适的embedding model用于匹配过程中的文本/问题向量化

打开 https://huggingface.co/GanymedeNil/text2vec-large-chinese/tree/main

下载 所有文件并存入 D:\Chinese-LLaMA-Alpaca-2\scripts\langchain\text2vec-large-chinese 目录下:

根据

直接下载完整版模型:

将下载好的Chinese-Alpaca-2-7B的完整版模型 保存到 D:\Chinese-LLaMA-Alpaca-2\scripts\langchain\chinese-alpaca-2-7b-hf 目录下。

3.安装依赖

conda deactivate

conda create -n langchain python=3.9

conda activate langchain

pip install langchain

pip install sentence_transformers==2.2.2

pip install pydantic==1.10.8

pip install faiss-gpu==1.7.1

这个好像没有gpu版本?

换成CPU版本试试。。

pip install faiss-cpu==1.7.1

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

这里需要重装一下GPU版的torch

pip3 install torch torchvision torchaudio –index-url https://download.pytorch.org/whl/cu117

再来:

python langchain_sum.py –model_path chinese-alpaca-2-7b-hf –file_path doc.txt –chain_type refine

貌似字符集有什么问题。

参考

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:

再来:

python langchain_sum.py –model_path chinese-alpaca-2-7b-hf –file_path doc.txt –chain_type refine

缺protobuf库?

pip install protobuf

再来:

python langchain_sum.py –model_path chinese-alpaca-2-7b-hf –file_path doc.txt –chain_type refine

pip install accelerate

再来:

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...
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

同样需要修改open部分,不过这次得修改 conda里面的了!

打开 L:\Anaconda\envs\langchain\lib\site-packages\langchain\document_loaders\text.py

再来:

python langchain_qa.py –embedding_path text2vec-large-chinese –model_path chinese-alpaca-2-7b-hf –file_path doc.txt –chain_type refine

pip install faiss-gpu

这个 faiss-gpu好像一直装不起来。

pip install –use-pep517 faiss-gpu

好像缺好多库。应该是requirements.txt没装。把requirements.txt文件里面的torch注释掉,安装一下:

cd D:\Chinese-LLaMA-Alpaca-2\

pip install -r requirements.txt

再来:

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

看来还是得搞定faiss-gpu

查看 https://wenku.csdn.net/answer/320ff1adb878d46eb9fe427c92874233

conda install faiss-gpu -c pytorch

也不行。

conda也找不到这个包:

conda search faiss

根据提示到官网搜索:https://anaconda.org

conda install faiss-gpu -c conda-forge

它连cuda都装了,动静有点大。。。。

再来:

python langchain_qa.py –embedding_path text2vec-large-chinese –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_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余首。

不错,都成功运行了!

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2023年12月20日
下一篇 2023年12月20日

相关推荐