mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-14 21:59:33 +03:00
Embed documents without locally downloading the HF model --------- Co-authored-by: Bagatur <baskaryan@gmail.com>
3.8 KiB
3.8 KiB
In [ ]:
!pip install langchain sentence_transformersIn [2]:
from langchain.embeddings import HuggingFaceEmbeddingsIn [3]:
embeddings = HuggingFaceEmbeddings()In [3]:
text = "This is a test document."In [5]:
query_result = embeddings.embed_query(text)In [6]:
query_result[:3]Out [6]:
[-0.04895168915390968, -0.03986193612217903, -0.021562768146395683]
In [7]:
doc_result = embeddings.embed_documents([text])In [1]:
import getpass
inference_api_key = getpass.getpass("Enter your HF Inference API Key:\n\n")Enter your HF Inference API Key: ········
In [4]:
from langchain.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]Out [4]:
[-0.038338541984558105, 0.1234646737575531, -0.028642963618040085]