IPEX-LLM:在英特尔 CPU 上运行本地 BGE 嵌入
IPEX-LLM 是一个基于 PyTorch 的库,用于在 Intel CPU 和 GPU(例如带有集成显卡的本地 PC,以及 Arc、Flex 和 Max 等独立显卡)上以极低延迟运行大语言模型(LLM)。
此示例介绍了如何在 Intel CPU 上使用 LangChain 执行嵌入任务,并且不进行 ipex-llm 项优化。这在诸如 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 用户,在安装
--extra-index-url https://download.pytorch.org/whl/cpu时无需安装ipex-llm。
基本用法
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 参考:IpexLLM Bge 嵌入
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]}")