IPEX-LLM:Intel CPU 上的本地 BGE 嵌入
IPEX-LLM 是一個 PyTorch 函式庫,用於在 Intel CPU 和 GPU 上執行 LLM (例如,具有 iGPU 的本地 PC、獨立 GPU,如 Arc、Flex 和 Max),且具有非常低的延遲。
此範例說明如何使用 LangChain 在 Intel CPU 上使用 ipex-llm
最佳化來執行嵌入任務。這對於 RAG、文件 QA 等應用程式非常有用。
設定
%pip install -qU langchain langchain-community
安裝 IPEX-LLM 以在 Intel CPU 上進行最佳化,以及 sentence-transformers
。
%pip install --pre --upgrade ipex-llm[all] --extra-index-url https://download.pytorch.org/whl/cpu
%pip install sentence-transformers
注意
對於 Windows 使用者,安裝
ipex-llm
時不需要--extra-index-url https://download.pytorch.org/whl/cpu
。
基本用法
from langchain_community.embeddings import IpexLLMBgeEmbeddings
embedding_model = IpexLLMBgeEmbeddings(
model_name="BAAI/bge-large-en-v1.5",
model_kwargs={},
encode_kwargs={"normalize_embeddings": True},
)
API 參考:IpexLLMBgeEmbeddings
API 參考
sentence = "IPEX-LLM is a PyTorch library for running LLM on Intel CPU and GPU (e.g., local PC with iGPU, discrete GPU such as Arc, Flex and Max) with very low latency."
query = "What is IPEX-LLM?"
text_embeddings = embedding_model.embed_documents([sentence, query])
print(f"text_embeddings[0][:10]: {text_embeddings[0][:10]}")
print(f"text_embeddings[1][:10]: {text_embeddings[1][:10]}")
query_embedding = embedding_model.embed_query(query)
print(f"query_embedding[:10]: {query_embedding[:10]}")