Hugging Face x LangChain: 全新 LangChain 合作伙伴包

我们很高兴官宣发布 langchain_huggingface,这是一个由 Hugging Face 和 LangChain 共同维护的 LangChain 合作伙伴包。这个新的 Python 包旨在将 Hugging Face 最新功能引入 LangChain 并保持同步。

源自社区,服务社区

目前,LangChain 中所有与 Hugging Face 相关的类都是由社区贡献的。虽然我们以此为基础蓬勃发展,但随着时间的推移,其中一些类在设计时由于缺乏来自 Hugging Face 的内部视角而在后期被废弃。

通过 Langchain 合作伙伴包这个方式,我们的目标是缩短将 Hugging Face 生态系统中的新功能带给 LangChain 用户所需的时间。

langchain-huggingface 与 LangChain 无缝集成,为在 LangChain 生态系统中使用 Hugging Face 模型提供了一种可用且高效的方法。这种伙伴关系不仅仅涉及到技术贡献,还展示了双方对维护和不断改进这一集成的共同承诺。

起步

langchain-huggingface 的起步非常简单。以下是安装该 软件包 的方法:

HuggingFacePipeline

transformers 中的 Pipeline 类是 Hugging Face 工具箱中最通用的工具。LangChain 的设计主要是面向 RAG 和 Agent 应用场景,因此,在 Langchain 中流水线被简化为下面几个以文本为中心的任务: 文本生成 、 文生文 、 摘要 、 翻译 等。

  • Pipeline 文档https://hf.co/docs/transformers/main_classes/pipelines

用户可以使用 from_model_id 方法直接加载模型:

from transformers import AutoModelForCausalLM, AutoTokenizer,pipeline

model_id = "microsoft/Phi-3-mini-4k-instruct"
tokenizer = AutoTokenizer.from_pretrained(model_id)

model = AutoModelForCausalLM.from_pretrained(
    model_id,
    load_in_4bit=True,
    #attn_implementation="flash_attention_2", # if you have an ampere GPU
)
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_new_tokens=100, top_k=50, temperature=0.1)
llm = HuggingFacePipeline(pipeline=pipe)
llm.invoke("Hugging Face is")

使用 HuggingFacePipeline 时,模型是加载至本机并在本机运行的,因此你可能会受到本机可用资源的限制。

from langchain_huggingface import HuggingFaceEndpoint

llm = HuggingFaceEndpoint(
    repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
    task="text-generation",
    max_new_tokens=100,
    do_sample=False,
)
llm.invoke("Hugging Face is")

<span style="margin: 0px 0px -7px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; display: block; background: url(" wx_fmt="svg&from=appmsg")" 10px="" 40px="" no-repeat="" height:="" width:="" border-radius:="">

该类在底层实现时使用了 InferenceClient,因此能够为已部署的 TGI 实例提供面向各种用例的无服务器 API。

  • InferenceClienthttps://hf.co/docs/huggingface_hub/en/package_reference/inference_client

from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint

llm = HuggingFaceEndpoint(
    endpoint_url="",
    task="text-generation",
    max_new_tokens=1024,
    do_sample=False,
)
llm_engine_hf = ChatHuggingFace(llm=llm)
llm_engine_hf.invoke("Hugging Face is")

上述代码等效于:

<span style="margin: 0px 0px -7px; padding: 0px; outline: 0px; max-width: 100%; box-sizing: border-box !important; overflow-wrap: break-word !important; display: block; background: url(" wx_fmt="svg&from=appmsg")" 10px="" 40px="" no-repeat="" height:="" width:="" border-radius:="">

嵌入

Hugging Face 里有很多非常强大的嵌入模型,你可直接把它们用于自己的流水线。

首先,选择你想要的模型。关于如何选择嵌入模型,一个很好的参考是 MTEB 排行榜。

  • MTEB 排行榜https://hf.co/spaces/mteb/leaderboard

from langchain_huggingface.embeddings import HuggingFaceEmbeddings

model_name = "mixedbread-ai/mxbai-embed-large-v1"
hf_embeddings = HuggingFaceEmbeddings(
    model_name=model_name,
)
texts = ["Hello, world!""How are you?"]
hf_embeddings.embed_documents(texts)

from langchain_huggingface.embeddings import HuggingFaceEndpointEmbeddings

hf_embeddings = HuggingFaceEndpointEmbeddings(
    model= "mixedbread-ai/mxbai-embed-large-v1",
    task="feature-extraction",
    huggingfacehub_api_token="",
)
texts = ["Hello, world!""How are you?"]
hf_embeddings.embed_documents(texts)

总结

我们致力于让 langchain-huggingface 变得越来越好。我们将积极监控反馈和问题,并努力尽快解决它们。我们还将不断添加新的特性和功能,以拓展该软件包使其支持更广泛的社区应用。我们强烈推荐你尝试 langchain-huggingface 软件包并提出宝贵意见,有了你的支持,这个软件包的未来道路才会越走越宽。


相关文章