这篇心得主要是总结LangChain实战课里面的第4节课到第8节课,也就是LangChain框架里最核心、最底层的模块, Model I/O
第4课重难点
前面部分的模型I/O相信大家都能跑通,如果没有跑通的建议查看我的上一篇笔记,检查一下MODEL、BASE_URL、KEY是否配置准确。
然后就是在跑模型IO_HuggingFace.py可能会报错,这里的版本问题很费劲
LangChainDeprecationWarning: The class `HuggingFaceHub` was deprecated in LangChain 0.0.21 and will be removed in 1.0. An updated version of the class exists in the :class:`~langchain-huggingface package and should be used instead. To use it run `pip install -U :class:`~langchain-huggingface` and import as `from :class:`~langchain_huggingface import HuggingFaceEndpoint``.
这个报错的解决办法也告诉我们了。LangChain 弃用警告:在 LangChain 0.0.21 版本中,HuggingFaceHub类已被弃用,并将在 1.0 版本中移除。该类的更新版本存在于langchain-huggingface包中,应当改用这个更新版本。要使用它,需运行pip install -U langchain-huggingface命令进行安装,然后通过from langchain_huggingface import HuggingFaceEndpoint语句进行导入。
但是我跑老师的google/flan-t5-large模型会报错,并且解决不了,但我们不要完全卡在这里想着复现google/flan-t5-large模型,因为这里老师只是想让我们get到langchain可以管理HuggingFace上的开源模型,便于我们以后在搭建自己的项目时使用本地大模型,然后这里我换个模型就可以跑通了
from langchain_huggingface import HuggingFaceEndpoint
from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate
template = """You are a flower shop assitiant。\n
For {price} of {flower_name} ,can you write something for me?
"""
prompt = PromptTemplate.from_template(template)
input = prompt.format(flower_name=["玫瑰"], price="50")
repo_id = "mistralai/Mistral-7B-Instruct-v0.2"
llm = HuggingFaceEndpoint(
repo_id=repo_id,
max_length=128,
temperature=0.5,
huggingfacehub_api_token="你的API_TOKEN",
)
print(llm.invoke(input))
此外注意在跑05_模型IO_输出解析这里的代码时,model = ChatOpenAI()不要限制temperature=, max_tokens=,这两个参数,要不然output输出的时候输出不全,格式解析会有问题,reason出不来。
第5、6课重难点
学会使用FewShotPromptTemplate,了解ChatPromptTemplate、PromptTemplate; 微调过程相比于从头训练一个模型要快得多,且需要的数据量也要少得多,这使得作为工程师的我们能够更高效地开发和部署各种NLP解决方案。
第7课重难点
学会使用HuggingFace 跑开源模型 语言模型输出的是文本,这是给人类阅读的。但很多时候,你可能想要获得的是程序能够处理的结构化信息。这就是输出解析器发挥作用的地方。