Hugging Face 上的 Sentence Transformers
Hugging Face sentence-transformers 是一個用於最先進的句子、文字和圖像嵌入的 Python 框架。 您可以使用來自
HuggingFaceEmbeddings
類別的這些嵌入模型。
注意 (caution)
在本地執行 sentence-transformers 可能會受到您的作業系統和其他全域因素的影響。 建議僅限有經驗的使用者使用。
設定 (Setup)
您需要安裝 langchain_huggingface
套件作為依賴項
%pip install -qU langchain-huggingface
使用方式 (Usage)
from langchain_huggingface import HuggingFaceEmbeddings
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
text = "This is a test document."
query_result = embeddings.embed_query(text)
# show only the first 100 characters of the stringified vector
print(str(query_result)[:100] + "...")
API 參考:HuggingFaceEmbeddings
[-0.038338568061590195, 0.12346471101045609, -0.028642969205975533, 0.05365273356437683, 0.008845377...
doc_result = embeddings.embed_documents([text, "This is not a test document."])
print(str(doc_result)[:100] + "...")
[[-0.038338497281074524, 0.12346471846103668, -0.028642890974879265, 0.05365274101495743, 0.00884535...
疑難排解 (Troubleshooting)
如果您在使用 accelerate
套件時遇到找不到或匯入失敗的問題,安裝/升級它可能會有所幫助
%pip install -qU accelerate