Skip to main content
langchain-localai 是 LocalAI 的第三方集成包,提供了一种在 LangChain 中使用 LocalAI 服务的简便方式。源代码托管于 GitHub
加载 LocalAI 嵌入类。要使用 LocalAI 嵌入类,您需要在某处托管 LocalAI 服务并配置嵌入模型。请参阅 localai.io/basics/getting_started/index.htmllocalai.io/features/embeddings/index.html 中的文档。
pip install -U langchain-localai
from langchain_localai import LocalAIEmbeddings

embeddings = LocalAIEmbeddings(
    openai_api_base="http://localhost:8080", model="embedding-model-name"
)
text = "This is a test document."

query_result = embeddings.embed_query(text)
doc_result = embeddings.embed_documents([text])

旧版 langchain-community LocalAIEmbeddings 文档

为确保兼容性,请使用 0.x 版本的 openai SDK。
使用嵌入模型加载 LocalAI 嵌入类。
pip install -U langchain-community
from langchain_community.embeddings import LocalAIEmbeddings
import os

# if you are behind an explicit proxy, you can use the OPENAI_PROXY environment variable to pass through
os.environ["OPENAI_PROXY"] = "http://proxy.yourcompany.com:8080"

embeddings = LocalAIEmbeddings(
    openai_api_base="http://localhost:8080", model="embedding-model-name"
)

text = "This is a test document."
query_result = embeddings.embed_query(text)
doc_result = embeddings.embed_documents([text])