6大核心模块(Modules)
LLMS
Huggingface Pipelines

LangChain

抱抱脸(Huggingface)本地管道#

Hugging Face 模型可以通过 HuggingFacePipeline 类在本地运行。

Hugging Face 模型中心 (opens in a new tab) 托管超过 120k 个模型、20k 个数据集和 50k 个演示应用程序(Spaces),全部都是开源且公开可用的,是一个在线平台,人们可以轻松协作和构建机器学习。

这些模型可以通过本地管道包装器或通过 HuggingFaceHub 类调用其托管的推断端点从 LangChain 中调用。有关托管管道的更多信息,请参见 HuggingFaceHub 教程。

要使用,您应该安装 transformers python 包。 (opens in a new tab)

!pip install transformers > /dev/null
 

加载模型#

from langchain import HuggingFacePipeline
 
llm = HuggingFacePipeline.from_model_id(model_id="bigscience/bloom-1b7", task="text-generation", model_kwargs={"temperature":0, "max_length":64})
 
WARNING:root:Failed to default session, using empty session: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /sessions (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x1117f9790>: Failed to establish a new connection: [Errno 61] Connection refused'))
 

将模型集成到LLMChain中#

from langchain import PromptTemplate,  LLMChain
 
template = """Question: {question}
 
Answer: Let's think step by step."""
prompt = PromptTemplate(template=template, input_variables=["question"])
 
llm_chain = LLMChain(prompt=prompt, llm=llm)
 
question = "What is electroencephalography?"
 
print(llm_chain.run(question))
 
/Users/wfh/code/lc/lckg/.venv/lib/python3.11/site-packages/transformers/generation/utils.py:1288: UserWarning: Using `max_length`'s default (64) to control the generation length. This behaviour is deprecated and will be removed from the config in v5 of Transformers -- we recommend using `max_new_tokens` to control the maximum length of the generation.
  warnings.warn(
WARNING:root:Failed to persist run: HTTPConnectionPool(host='localhost', port=8000): Max retries exceeded with url: /chain-runs (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x144d06910>: Failed to establish a new connection: [Errno 61] Connection refused'))
 
 First, we need to understand what is an electroencephalogram. An electroencephalogram is a recording of brain activity. It is a recording of brain activity that is made by placing electrodes on the scalp. The electrodes are placed