开始使用免费开始使用

在 LangChain 中使用 Hugging Face 模型!

Hugging Face 上有成千上万的模型可免费下载和使用。Hugging Face 通过其配套库 langchain-huggingface 与 LangChain 深度集成,您可以直接使用。

在本练习中,您将从 Hugging Face 加载并调用 crumb/nano-mistral 模型。它是一款超轻量级 LLM,适合通过微调来获得更高性能。

本练习是课程的一部分

使用 LangChain 开发 LLM 应用

查看课程

练习说明

  • langchain_huggingface 导入 HuggingFacePipeline,以便使用 Hugging Face 模型。
  • 通过调用 HuggingFacePipeline.from_model_id() 定义一个文本生成 LLM。
  • 设置 model_id 参数来指定要使用的 Hugging Face 模型。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Import the HuggingFacePipeline class for defining Hugging Face pipelines
from langchain_huggingface import ____

# Define the LLM from the Hugging Face model ID
llm = ____.from_model_id(
    ____="crumb/nano-mistral",
    task="text-generation",
    pipeline_kwargs={"max_new_tokens": 20}
)

prompt = "Hugging Face is"

# Invoke the model
response = llm.invoke(prompt)
print(response)
编辑并运行代码