OllamaEmbeddings
This will help you get started with Ollama embedding models using LangChain. For detailed documentation on OllamaEmbeddings
features and configuration options, please refer to the API reference. (這將幫助您開始使用 LangChain 的 Ollama 嵌入模型。如需 OllamaEmbeddings
功能和配置選項的詳細文件,請參閱 API 參考。)
Overview (概觀)
Integration details (整合細節)
Provider (供應商) | Package (套件) |
---|---|
Ollama | langchain-ollama |
Setup (設定)
First, follow these instructions to set up and run a local Ollama instance (首先,按照這些說明設定並執行本地 Ollama 實例)
- Download and install Ollama onto the available supported platforms (including Windows Subsystem for Linux) (下載並將 Ollama 安裝到可用的支援平台 (包括 Linux 的 Windows 子系統))
- Fetch available LLM model via
ollama pull <name-of-model>
(透過ollama pull <模型名稱>
取得可用的 LLM 模型)- View a list of available models via the model library (透過模型庫查看可用模型的列表)
- e.g.,
ollama pull llama3
(例如,ollama pull llama3
)
- This will download the default tagged version of the model. Typically, the default points to the latest, smallest sized-parameter model. (這將下載模型的預設標記版本。通常,預設指向最新的、最小尺寸參數的模型。)
On Mac, the models will be download to
~/.ollama/models
(在 Mac 上,模型將下載到~/.ollama/models
)On Linux (or WSL), the models will be stored at
/usr/share/ollama/.ollama/models
(在 Linux (或 WSL) 上,模型將儲存在/usr/share/ollama/.ollama/models
)
- Specify the exact version of the model of interest as such
ollama pull vicuna:13b-v1.5-16k-q4_0
(View the various tags for theVicuna
model in this instance) (指定您感興趣的模型的確切版本,例如ollama pull vicuna:13b-v1.5-16k-q4_0
(在此實例中,查看Vicuna
的各種標籤)) - To view all pulled models, use
ollama list
(要查看所有已提取的模型,請使用ollama list
) - To chat directly with a model from the command line, use
ollama run <name-of-model>
(要從命令列直接與模型聊天,請使用ollama run <模型名稱>
) - View the Ollama documentation for more commands. Run
ollama help
in the terminal to see available commands too. (查看 Ollama 文件 以了解更多命令。在終端機中執行ollama help
也能查看可用的指令。)
Credentials (憑證)
There is no built-in auth mechanism for Ollama. (Ollama 沒有內建的驗證機制。)
If you want to get automated tracing of your model calls you can also set your LangSmith API key by uncommenting below (如果您想自動追蹤您的模型呼叫,您也可以取消註解下面的內容來設定您的 LangSmith API 金鑰)
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
Installation (安裝)
The LangChain Ollama integration lives in the langchain-ollama
package (LangChain Ollama 整合位於 langchain-ollama
套件中)
%pip install -qU langchain-ollama
Note: you may need to restart the kernel to use updated packages.
Instantiation (實例化)
Now we can instantiate our model object and generate embeddings (現在我們可以實例化我們的模型物件並產生嵌入)
from langchain_ollama import OllamaEmbeddings
embeddings = OllamaEmbeddings(
model="llama3",
)
Indexing and Retrieval (索引和檢索)
Embedding models are often used in retrieval-augmented generation (RAG) flows, both as part of indexing data as well as later retrieving it. For more detailed instructions, please see our RAG tutorials. (嵌入模型通常用於檢索增強生成 (RAG) 流程中,既作為索引資料的一部分,也作為稍後檢索資料的一部分。如需更詳細的說明,請參閱我們的 RAG 教學課程。)
Below, see how to index and retrieve data using the embeddings
object we initialized above. In this example, we will index and retrieve a sample document in the InMemoryVectorStore
. (下面,看看如何使用我們上面初始化的 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
'LangChain is the framework for building context-aware reasoning applications'
Direct Usage (直接使用)
Under the hood, the vectorstore and retriever implementations are calling embeddings.embed_documents(...)
and embeddings.embed_query(...)
to create embeddings for the text(s) used in from_texts
and retrieval invoke
operations, respectively. (在底層,vectorstore 和 retriever 實作正在呼叫 embeddings.embed_documents(...)
和 embeddings.embed_query(...)
,以分別為 from_texts
和檢索 invoke
操作中使用的文字建立嵌入。)
You can directly call these methods to get embeddings for your own use cases. (您可以直接呼叫這些方法來取得用於您自己用例的嵌入。)
Embed single texts (嵌入單一文字)
You can embed single texts or documents with embed_query
(您可以使用 embed_query
嵌入單一文字或文件)
single_vector = embeddings.embed_query(text)
print(str(single_vector)[:100]) # Show the first 100 characters of the vector
[-0.001288981, 0.006547121, 0.018376578, 0.025603496, 0.009599175, -0.0042578303, -0.023250086, -0.0
Embed multiple texts (嵌入多個文字)
You can embed multiple texts with embed_documents
(您可以使用 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.0013138362, 0.006438795, 0.018304596, 0.025530428, 0.009717592, -0.004225636, -0.023363983, -0.0
[-0.010317663, 0.01632489, 0.0070348927, 0.017076202, 0.008924255, 0.007399284, -0.023064945, -0.003
API Reference (API 參考)
For detailed documentation on OllamaEmbeddings
features and configuration options, please refer to the API reference. (如需 OllamaEmbeddings
功能和配置選項的詳細文件,請參閱 API 參考。)
Related (相關)
- Embedding model conceptual guide (嵌入模型概念指南)
- Embedding model how-to guides (嵌入模型操作指南)