이 문서는 LangChain을 사용하여 Google Vertex AI Embeddings 모델을 시작하는 데 도움을 드립니다. Google Vertex AI Embeddings의 기능 및 구성 옵션에 대한 자세한 문서는 API reference를 참조하세요.

Overview

Integration details

ProviderPackage
Googlelangchain-google-vertexai

Setup

Google Vertex AI Embeddings 모델에 액세스하려면 다음이 필요합니다:
  • Google Cloud 계정 생성
  • langchain-google-vertexai integration package 설치

Credentials

Google Cloud로 이동하여 계정을 생성하세요. 완료한 후 GOOGLE_APPLICATION_CREDENTIALS 환경 변수를 설정하세요: 자세한 내용은 다음을 참조하세요: cloud.google.com/docs/authentication/application-default-credentials#GAC 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 key를 설정하세요:
os.environ["LANGSMITH_TRACING"] = "true"
os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")

Installation

LangChain Google Vertex AI Embeddings integration은 langchain-google-vertexai package에 있습니다:
pip install -qU langchain-google-vertexai

Instantiation

이제 모델 객체를 인스턴스화하고 embedding을 생성할 수 있습니다:
지원되는 모델 목록을 확인하세요
from langchain_google_vertexai import VertexAIEmbeddings

# Initialize the a specific Embeddings Model version
embeddings = VertexAIEmbeddings(model_name="gemini-embedding-001")

Indexing and Retrieval

Embedding 모델은 데이터 인덱싱과 나중에 검색하는 과정 모두에서 retrieval-augmented generation (RAG) 플로우에 자주 사용됩니다. 자세한 지침은 RAG tutorials를 참조하세요. 아래에서는 위에서 초기화한 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

내부적으로 vectorstore와 retriever 구현은 embeddings.embed_documents(...)embeddings.embed_query(...)를 호출하여 from_texts 및 retrieval invoke 작업에 사용되는 텍스트에 대한 embedding을 생성합니다. 이러한 메서드를 직접 호출하여 자신의 사용 사례에 맞는 embedding을 얻을 수 있습니다.

Embed single texts

embed_query를 사용하여 단일 텍스트 또는 문서를 embedding할 수 있습니다:
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 multiple texts

embed_documents를 사용하여 여러 텍스트를 embedding할 수 있습니다:
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 reference

Google Vertex AI Embeddings의 기능 및 구성 옵션에 대한 자세한 문서는 API reference를 참조하세요.
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I