LindormAIEmbeddings
這將幫助您開始使用 LangChain 的 Lindorm 嵌入模型。
概觀
整合詳細資訊
供應商 | 套件 |
---|---|
Lindorm | langchain-lindorm-integration |
設定
若要存取 Lindorm 嵌入模型,您需要建立 Lindorm 帳戶、取得 AK 和 SK,並安裝 langchain-lindorm-integration
整合套件。
憑證
您可以在主控台中取得您的憑證
import os
class Config:
AI_LLM_ENDPOINT = os.environ.get("AI_ENDPOINT", "<AI_ENDPOINT>")
AI_USERNAME = os.environ.get("AI_USERNAME", "root")
AI_PWD = os.environ.get("AI_PASSWORD", "<PASSWORD>")
AI_DEFAULT_EMBEDDING_MODEL = "bge_m3_model" # set to your deployed model
安裝
LangChain Lindorm 整合存在於 langchain-lindorm-integration
套件中
%pip install -qU langchain-lindorm-integration
Note: you may need to restart the kernel to use updated packages.
實例化
現在我們可以實例化我們的模型物件並產生聊天完成
from langchain_lindorm_integration import LindormAIEmbeddings
embeddings = LindormAIEmbeddings(
endpoint=Config.AI_LLM_ENDPOINT,
username=Config.AI_USERNAME,
password=Config.AI_PWD,
model_name=Config.AI_DEFAULT_EMBEDDING_MODEL,
)
索引和檢索
嵌入模型通常用於檢索增強生成 (RAG) 流程中,既作為索引資料的一部分,也用於稍後檢索資料。如需更詳細的指示,請參閱我們的 RAG 教學課程。
在下方,請參閱如何使用我們在上方初始化的 embeddings
物件來索引和檢索資料。在此範例中,我們將在 InMemoryVectorStore
中索引和檢索範例文件。
# Create a vector store with a sample text
from langchain_core.vectorstores import InMemoryVectorStore
text = "LangChain is the framework for building context-aware reasoning applications"
vectorstore = InMemoryVectorStore.from_texts(
[text],
embedding=embeddings,
)
# Use the vectorstore as a retriever
retriever = vectorstore.as_retriever()
# Retrieve the most similar text
retrieved_documents = retriever.invoke("What is LangChain?")
# show the retrieved document's content
retrieved_documents[0].page_content
API 參考:InMemoryVectorStore
'LangChain is the framework for building context-aware reasoning applications'
直接使用
在幕後,向量儲存和檢索器實作正在呼叫 embeddings.embed_documents(...)
和 embeddings.embed_query(...)
,以為 from_texts
和檢索 invoke
作業中使用的文字建立嵌入。
您可以直接呼叫這些方法來取得您自己使用案例的嵌入。
嵌入單一文字
您可以使用 embed_query
嵌入單一文字或文件
single_vector = embeddings.embed_query(text)
print(str(single_vector)[:100]) # Show the first 100 characters of the vector
[-0.016254117712378502, -0.01154549140483141, 0.0042558759450912476, -0.011416379362344742, -0.01770
嵌入多個文字
您可以使用 embed_documents
嵌入多個文字
text2 = (
"LangGraph is a library for building stateful, multi-actor applications with LLMs"
)
two_vectors = embeddings.embed_documents([text, text2])
for vector in two_vectors:
print(str(vector)[:100]) # Show the first 100 characters of the vector
[-0.016254086047410965, -0.011545476503670216, 0.0042558712884783745, -0.011416426859796047, -0.0177
[-0.07268096506595612, -3.236892371205613e-05, -0.0019329536007717252, -0.030644644051790237, -0.018
API 參考
如需關於 LindormEmbeddings
功能和組態選項的詳細文件,請參閱 API 參考。