跳到主要内容
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="https://:8080", model="embedding-model-name"
)
text = "This is a test document."
query_result = embeddings.embed_query(text)
doc_result = embeddings.embed_documents([text])
我们来加载带有第一代模型(例如 text-search-ada-doc-001/text-search-ada-query-001)的 LocalAI 嵌入类。注意:这些模型不推荐使用 - 请参阅 此处
from langchain_community.embeddings import LocalAIEmbeddings
embeddings = LocalAIEmbeddings(
    openai_api_base="https://:8080", model="embedding-model-name"
)
text = "This is a test document."
query_result = embeddings.embed_query(text)
doc_result = embeddings.embed_documents([text])
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"

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.