IPEX-LLM: 在 Intel CPU 上本地运行 BGE Embeddings
IPEX-LLM 是一个 PyTorch 库,用于在 Intel CPU 和 GPU(例如带有 iGPU 的本地 PC、Arc、Flex 和 Max 等独立 GPU)上以非常低的延迟运行 LLM。
本示例将介绍如何使用 LangChain 结合 ipex-llm 优化在 Intel CPU 上执行嵌入任务。这对于 RAG、文档问答等 应用非常有帮助。
设置
%pip install -qU langchain langchain-community
为在 Intel CPU 上进行优化而安装 IPEX-LLM,以及 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 Reference: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]}")
Related
- Embedding model conceptual guide
- Embedding model how-to guides