跳到主要內容
Open In ColabOpen on GitHub

Google Vertex AI Embeddings

這將幫助您開始使用 LangChain 的 Google Vertex AI Embeddings 模型。有關 Google Vertex AI Embeddings 功能和配置選項的詳細文件,請參閱 API 參考

概觀

整合詳細資訊

供應商套件
Googlelangchain-google-vertexai

設定

若要存取 Google Vertex AI Embeddings 模型,您需要

  • 建立 Google Cloud 帳戶
  • 安裝 langchain-google-vertexai 整合套件。

憑證

前往 Google Cloud 註冊以建立帳戶。完成後,設定 GOOGLE_APPLICATION_CREDENTIALS 環境變數

如需更多資訊,請參閱

https://cloud.google.com/docs/authentication/application-default-credentials#GAC https://googleapis.dev/python/google-auth/latest/reference/google.auth.html#module-google.auth

選用:驗證您的筆記本環境(僅限 Colab)

如果您在 Google Colab 上執行此筆記本,請執行以下儲存格以驗證您的環境。

import sys

if "google.colab" in sys.modules:
from google.colab import auth

auth.authenticate_user()

設定 Google Cloud 專案資訊並初始化 Vertex AI SDK

若要開始使用 Vertex AI,您必須擁有現有的 Google Cloud 專案並啟用 Vertex AI API

深入瞭解設定專案和開發環境

PROJECT_ID = "[your-project-id]"  # @param {type:"string"}
LOCATION = "us-central1" # @param {type:"string"}

import vertexai

vertexai.init(project=PROJECT_ID, location=LOCATION)

如果您想要取得模型呼叫的自動追蹤,您也可以透過取消註解下方內容來設定您的 LangSmith API 金鑰

# os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")

安裝

LangChain Google Vertex AI Embeddings 整合存在於 langchain-google-vertexai 套件中

%pip install -qU langchain-google-vertexai

例項化

現在我們可以例項化我們的模型物件並產生嵌入

查看 支援模型 清單

from langchain_google_vertexai import VertexAIEmbeddings

# Initialize the a specific Embeddings Model version
embeddings = VertexAIEmbeddings(model_name="text-embedding-004")
API 參考:VertexAIEmbeddings

索引與檢索

嵌入模型通常用於檢索增強生成 (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.02831101417541504, 0.022063178941607475, -0.07454229146242142, 0.006448323838412762, 0.001955120

嵌入多個文字

您可以使用 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.01092718355357647, 0.01213780976831913, -0.05650627985596657, 0.006737854331731796, 0.0085973171
[0.010135706514120102, 0.01234869472682476, -0.07284046709537506, 0.00027134662377648056, 0.01546290

API 參考

有關 Google Vertex AI Embeddings 功能和配置選項的詳細文件,請參閱 API 參考


此頁面是否有幫助?