Skip to main content

Hugging Face

让我们加载 Hugging Face 嵌入类。

%pip install --upgrade --quiet  langchain sentence_transformers
<!--IMPORTS:[{"imported": "HuggingFaceEmbeddings", "source": "langchain_huggingface.embeddings", "docs": "https://python.langchain.com/api_reference/huggingface/embeddings/langchain_huggingface.embeddings.huggingface.HuggingFaceEmbeddings.html", "title": "Hugging Face"}]-->
from langchain_huggingface.embeddings import HuggingFaceEmbeddings
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2")
text = "This is a test document."
query_result = embeddings.embed_query(text)
query_result[:3]
[-0.04895168915390968, -0.03986193612217903, -0.021562768146395683]
doc_result = embeddings.embed_documents([text])

Hugging Face 推理 API

我们还可以通过 Hugging Face 推理 API 访问嵌入模型,这不需要我们安装 sentence_transformers 并在本地下载模型。

import getpass

inference_api_key = getpass.getpass("Enter your HF Inference API Key:\n\n")
Enter your HF Inference API Key:

········
<!--IMPORTS:[{"imported": "HuggingFaceInferenceAPIEmbeddings", "source": "langchain_community.embeddings", "docs": "https://python.langchain.com/api_reference/community/embeddings/langchain_community.embeddings.huggingface.HuggingFaceInferenceAPIEmbeddings.html", "title": "Hugging Face"}]-->
from langchain_community.embeddings import HuggingFaceInferenceAPIEmbeddings

embeddings = HuggingFaceInferenceAPIEmbeddings(
api_key=inference_api_key, model_name="sentence-transformers/all-MiniLM-l6-v2"
)

query_result = embeddings.embed_query(text)
query_result[:3]
[-0.038338541984558105, 0.1234646737575531, -0.028642963618040085]

Hugging Face Hub

我们还可以通过 Hugging Face Hub 包在本地生成嵌入,这需要我们安装 huggingface_hub

!pip install huggingface_hub
<!--IMPORTS:[{"imported": "HuggingFaceEndpointEmbeddings", "source": "langchain_huggingface.embeddings", "docs": "https://python.langchain.com/api_reference/huggingface/embeddings/langchain_huggingface.embeddings.huggingface_endpoint.HuggingFaceEndpointEmbeddings.html", "title": "Hugging Face"}]-->
from langchain_huggingface.embeddings import HuggingFaceEndpointEmbeddings
embeddings = HuggingFaceEndpointEmbeddings()
text = "This is a test document."
query_result = embeddings.embed_query(text)
query_result[:3]

相关


Was this page helpful?


You can also leave detailed feedback on GitHub.

扫我,入群扫我,找书