Hugging Face x LangChain:LangChain 中的一個新合作伙伴包
我們激動地宣佈推出
langchain_huggingface
,這是 LangChain 中一個由 Hugging Face 和 LangChain 共同維護的合作伙伴包。這個新的 Python 包旨在將 Hugging Face 的最新發展成果帶入 LangChain,並保持其持續更新。
來自社群,為了社群
LangChain 中所有與 Hugging Face 相關的類都是由社群編寫的,雖然我們因此蓬勃發展,但隨著時間的推移,由於缺乏內部人士的視角,其中一些類已經變得過時了。
透過成為合作伙伴包,我們的目標是縮短將 Hugging Face 生態系統中的新功能帶給 LangChain 使用者所需的時間。
langchain-huggingface
與 LangChain 無縫整合,提供了一種在 LangChain 生態系統內高效利用 Hugging Face 模型的有效方法。這種合作關係不僅是技術共享,更體現了維護和持續改進這一整合的共同承諾。
快速入門
開始使用 langchain-huggingface
非常簡單。以下是如何安裝和開始使用該包的方法
pip install langchain-huggingface
既然包已經安裝好了,讓我們來看看裡面有什麼內容吧!
大語言模型(LLM)
HuggingFacePipeline
在 transformers
中,Pipeline 是 Hugging Face 工具箱中最通用的工具。由於 LangChain 主要設計用於解決 RAG 和 Agent 用例,因此這裡的 pipeline 範圍被限制在以下以文字為中心的任務:“text-generation”
(文字生成)、“text2text-generation”
(文字到文字生成)、“summarization”
(摘要)、“translation”
(翻譯)。
模型可以透過 from_model_id
方法直接載入。
from langchain_huggingface import HuggingFacePipeline
llm = HuggingFacePipeline.from_model_id(
model_id="microsoft/Phi-3-mini-4k-instruct",
task="text-generation",
pipeline_kwargs={
"max_new_tokens": 100,
"top_k": 50,
"temperature": 0.1,
},
)
llm.invoke("Hugging Face is")
或者你也可以在將其傳遞給類之前自己定義 pipeline
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")
使用此類時,模型將被載入到快取中並使用你的計算機硬體;因此,你可能會受到計算機可用資源的限制。
HuggingFaceEndpoint
使用這個類也有兩種方式。你可以透過 repo_id
引數指定模型。這些端點使用無伺服器 API,這對使用專業賬戶或企業中心的人尤其有利。不過,普通使用者透過在執行程式碼的環境中使用他們的 HF 令牌登入,也已經可以獲得相當數量的請求。
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")
llm = HuggingFaceEndpoint(
endpoint_url="<endpoint_url>",
task="text-generation",
max_new_tokens=1024,
do_sample=False,
)
llm.invoke("Hugging Face is")
在底層,這個類使用 InferenceClient,以便能夠服務於從無伺服器 API 到已部署的 TGI 例項等多種用例。
ChatHuggingFace
每個模型都有其自己效果最佳的特殊 token。如果不在提示中新增這些 token,你的模型效能將大大降低。
當從訊息列表轉換為補全提示時,大多數 LLM 的分詞器(tokenizer)中存在一個名為 chat_template 的屬性。
要了解不同模型中 chat_template 的更多資訊,請訪問我製作的這個 space!
這個類是其他 LLM 的包裝器。它以訊息列表作為輸入,然後使用 tokenizer.apply_chat_template
方法建立正確的補全提示。
from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
llm = HuggingFaceEndpoint(
endpoint_url="<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")
上面的程式碼等同於
# with mistralai/Mistral-7B-Instruct-v0.2
llm.invoke("<s>[INST] Hugging Face is [/INST]")
# with meta-llama/Meta-Llama-3-8B-Instruct
llm.invoke("""<|begin_of_text|><|start_header_id|>user<|end_header_id|>Hugging Face is<|eot_id|><|start_header_id|>assistant<|end_header_id|>""")
嵌入模型(Embeddings)
Hugging Face 上有許多非常強大的嵌入模型,你可以直接在你的 pipeline 中使用。
首先選擇你的模型。選擇嵌入模型的一個好資源是 MTEB 排行榜。
HuggingFaceEmbeddings
這個類使用 sentence-transformers 嵌入。它在本地計算嵌入,因此會使用你的計算機資源。
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)
HuggingFaceEndpointEmbeddings
HuggingFaceEndpointEmbeddings
與 HuggingFaceEndpoint
為 LLM 所做的事情非常相似,因為它也在底層使用 InferenceClient 來計算嵌入。它可以與模型中心(Hub)上的模型以及本地或線上部署的 TEI 例項一起使用。
from langchain_huggingface.embeddings import HuggingFaceEndpointEmbeddings
hf_embeddings = HuggingFaceEndpointEmbeddings(
model= "mixedbread-ai/mxbai-embed-large-v1",
task="feature-extraction",
huggingfacehub_api_token="<HF_TOKEN>",
)
texts = ["Hello, world!", "How are you?"]
hf_embeddings.embed_documents(texts)
總結
我們致力於讓 langchain-huggingface
一天比一天更好。我們將積極監控反饋和問題,並努力盡快解決它們。我們還將增加新特性和功能,並擴充套件該包以支援更廣泛的社群用例。我們強烈鼓勵你嘗試這個包並給出你的意見,因為它將為該包的未來鋪平道路。