LangChain 12调用模型HuggingFace中的Llama2和Google Flan t5

LangChain系列文章

  1. LangChain 实现给动物取名字
  2. LangChain 2模块化prompt template并用streamlit生成网站 实现给动物取名字
  3. LangChain 3使用Agent访问Wikipedia和llm-math计算狗的平均年龄
  4. LangChain 4用向量数据库Faiss存储,读取YouTube的视频文本搜索Indexes for information retrieve
  5. LangChain 5易速鲜花内部问答系统
  6. LangChain 6根据图片生成推广文案HuggingFace中的image-caption模型
  7. LangChain 7 文本模型TextLangChain和聊天模型ChatLangChain
  8. LangChain 8 模型Model I/O:输入提示、调用模型、解析输出
  9. LangChain 9 模型Model I/O 聊天提示词ChatPromptTemplate, 少量样本提示词FewShotPrompt
  10. LangChain 10思维链Chain of Thought一步一步的思考 think step by step
  11. LangChain 11实现思维树Implementing the Tree of Thoughts in LangChain’s Chain
    在这里插入图片描述

这里会用到开源的LLM,也就是Predict的Model部分。 Hugging Face 的 transformers 库。

1. Hugging Face 设置

用 HuggingFace 跑开源模型注册并安装 HuggingFace

  1. 第一步,还是要登录 HuggingFace 网站,并拿到专属于你的 Token。
    点击Hugging Face token获取
    在这里插入图片描述
  2. 第二步,用 pip install transformers 安装 HuggingFace Library 详见这里
  3. 第三步,在命令行中运行 huggingface-cli login,设置你的 API Token。
    在这里插入图片描述

Llama-2-7b-chat-hf 需要申请才能用,点击链接申请
申请成功会有提示: Gated model You have been granted access to this model
在这里插入图片描述
token 这是到macOS 的文件中.zshrc, 文件.env 中填写对应的key

OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")

在这里插入图片描述

2. Google Flan t5 翻译

文件代码LLM/hf_google_flan_t5.py

# 导入dotenv库,用于从环境配置文件.env中加载环境变量。
# 这通常用于安全地管理敏感数据,例如API密钥。
from dotenv import load_dotenv

# 执行load_dotenv函数,从.env文件加载环境变量到Python的环境变量中。
load_dotenv()

# 从transformers库导入T5Tokenizer和T5ForConditionalGeneration。
# 这些是用于NLP的预训练模型和对应的分词器。
from transformers import T5Tokenizer, T5ForConditionalGeneration

# 从预训练模型"google/flan-t5-small"加载T5分词器。
# 这个模型专门用于文本生成任务,如翻译、摘要等。
tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-small")

# 加载预训练的T5条件生成模型。
model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-small")

# 定义输入文本,这里是一个英译德的翻译任务。
input_text = "translate English to German: How old are you?"

# 使用分词器处理输入文本,将文本转换为模型可理解的输入ID。
# return_tensors="pt"表示返回的是PyTorch张量。
input_ids = tokenizer(input_text, return_tensors="pt").input_ids

# 使用模型生成响应,即对输入文本进行翻译。
outputs = model.generate(input_ids)

# 解码模型输出,将生成的ID转换回文本格式,并打印出来。
print(tokenizer.decode(outputs[0]))

执行结果

(.venv) [zgpeace@zgpeaces-MBP langchain-llm-app (feature/textAndChat ✗)]$ python LLM/hf_google_flan_t5.py
You are using the default legacy behaviour of the <class 'transformers.models.t5.tokenization_t5.T5Tokenizer'>. This is expected, and simply means that the `legacy` (previous) behavior will be used so nothing changes for you. If you want to use the new behaviour, set `legacy=False`. This should only be set if you understand what it means, and thouroughly read the reason why this was added as explained in https://github.com/huggingface/transformers/pull/24565
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/transformers/generation/utils.py:1273: UserWarning: Using the model-agnostic default `max_length` (=20) to control the generation length. We recommend setting `max_new_tokens` to control the maximum length of the generation.
  warnings.warn(
<pad> Wie ich er bitten?</s>

3. 调用Llama2

# 导入dotenv库,用于从环境配置文件.env中加载环境变量。
# 这主要用于安全地管理敏感数据,例如API密钥。
from dotenv import load_dotenv  

# 调用load_dotenv函数来从.env文件加载环境变量。
load_dotenv()  

# 从transformers库导入AutoTokenizer和AutoModelForCausalLM。
# 这些用于自动加载和处理预训练的语言模型。
from transformers import AutoTokenizer, AutoModelForCausalLM

# 加载tokenizer,用于将文本转换为模型能理解的格式。
# 这里使用的是预训练模型"meta-llama/Llama-2-7b-chat-hf"。
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")

# 加载预训练的因果语言模型。
# 指定模型的设备为"auto",以自动选择运行模型的最佳设备(CPU或GPU)。
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
# 将模型分配到适当的设备上
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# 定义一个提示文本,要求生成关于水果的爱情故事。
prompt = "你是一位起水果运营专家,请讲一个动人的关于水果的爱情故事。"

# 使用tokenizer处理输入文本,并将其转移到模型的设备上。
# 这里return_tensors="pt"表示返回的是PyTorch张量。
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

# 使用模型生成回应,设置最大长度、采样参数以控制生成的文本多样性。
outputs = model.generate(inputs["input_ids"], max_length=2000, do_sample=True, top_p=0.95, top_k=60)

# 将生成的输出解码为文本,并跳过特殊标记。
response = tokenizer.decode(outputs[0], skip_special_tokens=True)

# 打印生成的故事。
print(response)

执行结果,老机器了macOS pro, 16G内存,跑步了。。

$ python LLM/hf_llama.py
[1]    2024 killed     /usr/local/bin/python3.10 LLM/hf_llama.py
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/multiprocessing/resource_tracker.py:224: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown
  warnings.warn('resource_tracker: There appear to be %d '

这个错误信息包含了两个部分,一个是进程被终止(killed),另一个是关于资源泄漏的警告(leaked semaphore objects)。

进程被终止([1] 2024 killed)
内存不足:最常见的原因是系统内存不足。当 Python 脚本使用的内存超过系统可用内存时,操作系统会终止进程以防止系统崩溃。这在运行大型机器学习模型(如LLM)时尤其常见。

4. Langchain 调用google flan t5, Llama2回答问题

文件LLM/langchain_hf_google_flan_t5.py代码参考了黄佳老师的课程Demo,如需要知道代码细节请读原文

# 导入dotenv库,用于从.env文件加载环境变量。
# 这对于管理敏感数据,如API密钥,非常有用。
from dotenv import load_dotenv  

# 调用load_dotenv函数来加载.env文件中的环境变量。
load_dotenv()

# 从langchain库导入所需的类。
from langchain.prompts import PromptTemplate
from langchain.llms import HuggingFaceHub
from langchain.chains import LLMChain

# 初始化Hugging Face Hub中的语言模型。
# 这里使用的是"google/flan-t5-small"作为repo_id来指定模型。
# repo_id="meta-llama/Llama-2-7b-chat-hf"是另一个可选的模型。
llm = HuggingFaceHub(
    repo_id="google/flan-t5-small"
    # repo_id="meta-llama/Llama-2-7b-chat-hf"
)

# 创建一个简单的问答模板。
# 模板包含了问题(question)和回答(Answer)的格式。
template = """Question: {question}
              Answer: """

# 创建一个PromptTemplate对象,基于上面定义的模板和输入变量。
prompt = PromptTemplate(template=template, input_variables=["question"])

# 使用LLMChain,它是一种将提示和语言模型结合起来的方法。
# 这里将前面创建的prompt和llm(语言模型)结合起来。
llm_chain = LLMChain(
    prompt=prompt,
    llm=llm
)

# 定义一个问题.
question = "Please tell a lovely story."

# 使用llm_chain运行模型,传入问题,并打印结果。
# 这将根据提供的问题生成回答。
print(llm_chain.run({"question": question}))

运行结果

(.venv) [zgpeace@zgpeaces-MBP langchain-llm-app (feature/textAndChat ✗)]$ python LLM/langchain_hf_google_flan_t5.py 
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/huggingface_hub/utils/_deprecation.py:127: FutureWarning: '__init__' (from 'huggingface_hub.inference_api') is deprecated and will be removed from version '1.0'. `InferenceApi` client is deprecated in favor of the more feature-complete `InferenceClient`. Check out this guide to learn how to convert your script to use it: https://huggingface.co/docs/huggingface_hub/guides/inference#legacy-inferenceapi-client.
  warnings.warn(warning_message, FutureWarning)
The sandstones are a great place to go.

答非所问,以下截图为证
在这里插入图片描述

修改为Llama-2 model

llm = HuggingFaceHub(
    # repo_id="google/flan-t5-small"
    repo_id="meta-llama/Llama-2-7b-chat-hf"
)

Llama2 还行

zgpeace at zgpeaces-MBP in ~/Workspace/LLM/langchain-llm-app (develop●) (.venv) 
$ python LLM/langchain_hf_google_flan_t5.py
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/huggingface_hub/utils/_deprecation.py:127: FutureWarning: '__init__' (from 'huggingface_hub.inference_api') is deprecated and will be removed from version '1.0'. `InferenceApi` client is deprecated in favor of the more feature-complete `InferenceClient`. Check out this guide to learn how to convert your script to use it: https://huggingface.co/docs/huggingface_hub/guides/inference#legacy-inferenceapi-client.
  warnings.warn(warning_message, FutureWarning)
 Of course, I'd be happy to tell you a story.  Here's one that

在这里插入图片描述

代码

  • https://github.com/zgpeace/pets-name-langchain/tree/develop
  • https://github.com/huangjia2019/langchain/tree/main/03_%E6%A8%A1%E5%9E%8BIO
  • 22
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要下载huggingface上的llama2模型,你需要经过官方的授权。你可以在hugging face模型页面申请授权,并等待审核通过。审核通过后,你就可以下载模型了。你可以使用脚本的方式进行下载,这样可以更快速地进行下载,并且可以设置密码和代理。具体的实现方法可以参考以下示例代码: ```python from huggingface_hub import snapshot_download repo_id = "meta-llama/Llama-2-7b-hf" # 模型huggingface上的名称 local_dir = "/home/model_zoo/LLM/llama2/Llama-2-7b-hf/" # 本地模型存储的地址 local_dir_use_symlinks = False # 本地模型使用文件保存,而非blob形式保存 token = "XXX" # 在hugging face上生成的 access token # 如果需要代理的话 proxies = { 'http': 'XXXX', 'https': 'XXXX', } snapshot_download( repo_id=repo_id, local_dir=local_dir, local_dir_use_symlinks=local_dir_use_symlinks, token=token, proxies=proxies ) ``` 请确保将`repo_id`设置为llama2模型huggingface上的名称,`local_dir`设置为你想要存储模型的本地目录,`token`设置为你在hugging face上生成的access token。如果需要代理,请将`proxies`设置为你的代理。 这样,你就可以使用以上代码来下载llama2模型了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [如何优雅地下载huggingface模型,以llama2模型下载为例](https://blog.csdn.net/ljp1919/article/details/131925099)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [LLM-LLaMA:手动模型转换与合并【Step 1: 将原版LLaMA模型转换为HF(HuggingFace)格式;Step 2: 合并LoRA...](https://blog.csdn.net/u013250861/article/details/131387468)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值