VertexAIEmbeddings 통합
VertexAIEmbeddings 통합
LangChain JavaScript로 VertexAIEmbeddings 임베딩 모델과 통합하는 방법을 안내할게요.
출처: 문서
본문
Gemini Enterprise Agent Platform은 Google Cloud에서 사용 가능한 모든 기반 모델을 노출하는 서비스예요.
이 문서는 LangChain으로 Gemini Enterprise Agent Platform 임베딩 모델을 시작하는 데 도움을 줘요. VertexAIEmbeddings 기능과 구성 옵션에 대한 자세한 문서는 API 레퍼런스를 참고하세요.
개요
통합 세부 정보
| 클래스 | 패키지 | Local | Py 지원 | Downloads | Version |
|---|---|---|---|---|---|
VertexAIEmbeddings |
@langchain/google-vertexai |
❌ | ✅ |
설정
LangChain.js는 Node.js 환경에서 실행하는지 웹 환경에서 실행하는지에 따라 두 가지 다른 인증 방법을 지원해요.
Gemini Enterprise Agent Platform 임베딩 모델에 접근하려면 GCP(Google Cloud Platform) 계정에서 Gemini Enterprise Agent Platform을 설정하고 자격 증명 파일을 저장한 뒤 @langchain/google-vertexai 통합 패키지를 설치해야 해요. Node.js에서 이 패키지는 인증에 @langchain/google-gauth를 사용해요 (별도로 설치할 필요는 없어요).
자격 증명
GCP 계정으로 이동해 자격 증명 파일을 생성하세요. 완료되면 GOOGLE_APPLICATION_CREDENTIALS 환경 변수를 설정하세요:
export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/credentials.json"
로컬 머신에서 gcloud auth application-default login을 실행해 Application Default Credentials를 사용할 수도 있어요.
웹 환경에서 실행한다면 @langchain/google-vertexai-web을 설치하고 GOOGLE_WEB_CREDENTIALS를 서비스 계정 JSON 문자열로 설정하세요 (GOOGLE_VERTEX_AI_WEB_CREDENTIALS는 더 이상 사용되지 않음).
모델 호출의 자동 추적(tracing)을 원한다면 아래 주석을 해제해 LangSmith API 키를 설정할 수도 있어요:
# export LANGSMITH_TRACING="true"
# export LANGSMITH_API_KEY="your-api-key"
설치
LangChain Gemini Enterprise Agent Platform 임베딩 통합은 @langchain/google-vertexai 패키지에 있어요:
yarn add @langchain/google-vertexai @langchain/core
pnpm add @langchain/google-vertexai @langchain/core
인스턴스 생성
이제 모델 객체를 생성하고 텍스트를 임베딩할 수 있어요:
import { VertexAIEmbeddings } from "@langchain/google-vertexai";
// Uncomment the following line if you're running in a web environment:
// import { VertexAIEmbeddings } from "@langchain/google-vertexai-web"
const embeddings = new VertexAIEmbeddings({
model: "gemini-embedding-001",
// ...
});
인덱싱 및 검색
임베딩 모델은 데이터 인덱싱과 나중 검색 모두의 일부로, 검색 증강 생성(RAG) 흐름에서 자주 사용돼요. 더 자세한 지침은 Learn 탭의 RAG 튜토리얼을 참고하세요.
아래에서 위에서 초기화한 embeddings 객체로 데이터를 인덱싱하고 검색하는 방법을 확인하세요. 이 예제에서는 데모 MemoryVectorStore를 사용해 샘플 문서를 인덱싱하고 검색할 거예요.
// Create a vector store with a sample text
import { MemoryVectorStore } from "@langchain/classic/vectorstores/memory";
const text = "LangChain is the framework for building context-aware reasoning applications";
const vectorstore = await MemoryVectorStore.fromDocuments(
[{ pageContent: text, metadata: {} }],
embeddings,
);
// Use the vector store as a retriever that returns a single document
const retriever = vectorstore.asRetriever(1);
// Retrieve the most similar text
const retrievedDocuments = await retriever.invoke("What is LangChain?");
retrievedDocuments[0].pageContent;
LangChain is the framework for building context-aware reasoning applications
직접 사용
내부적으로 vectorstore와 retriever 구현은 fromDocuments에 사용된 텍스트와 retriever의 invoke 연산에 대해 각각 embeddings.embedDocument(...) 와 embeddings.embedQuery(...)를 호출해 임베딩을 만들어요.
이 메서드들을 직접 호출해 자신의 사용 사례에 대한 임베딩을 얻을 수 있어요.
단일 텍스트 임베딩
embedQuery로 검색을 위한 쿼리를 임베딩할 수 있어요. 이는 쿼리에 특화된 벡터 표현을 생성해요:
const singleVector = await embeddings.embedQuery(text);
console.log(singleVector.slice(0, 100));
[
-0.02831101417541504, 0.022063178941607475, -0.07454229146242142,
0.006448323838412762, 0.001955120824277401, -0.017617391422390938,
0.018649872392416, -0.05262855067849159, 0.0006953597767278552,
-0.0018249079585075378, 0.022437218576669693, 0.0036489504855126143,
0.0018086736090481281, 0.016940006986260414, -0.007894322276115417,
-0.04187627509236336, 0.039501357823610306, 0.06918870657682419,
-0.006931832991540432, 0.049655742943286896, 0.021211417391896248,
-0.029322246089577675, -0.04546992480754852, -0.01769082061946392,
0.046703994274139404, 0.03127637133002281, 0.006355373188853264,
0.014901148155331612, -0.006893016863614321, -0.05992589890956879,
-0.009733330458402634, 0.015709295868873596, -0.017982766032218933,
-0.0852997675538063, -0.032453566789627075, 0.0014507169835269451,
0.03345133736729622, 0.048862338066101074, 0.006664620712399483,
-0.06287197023630142, -0.02109423652291298, 0.018176473677158356,
-0.022175665944814682, 0.03340170532464981, -0.008905526250600815,
-0.03492079675197601, -0.03819998353719711, -0.05230168625712395,
-0.05247239023447037, 0.048254698514938354, 0.046494755893945694,
-0.029708227142691612, -0.002180763054639101, 0.051957979798316956,
-0.05483679473400116, 0.00700812041759491, -0.08181990683078766,
-0.02295914851129055, 0.026530204340815544, 0.04028692841529846,
-0.05230272561311722, -0.057705819606781006, -0.015022763051092625,
0.002143724123016, 0.06361843645572662, -0.027828887104988098,
0.006870461627840996, -0.016140831634402275, -0.034440942108631134,
-0.004059414379298687, -0.042537953704595566, -0.00984653178602457,
-0.07701274752616882, 0.09815558046102524, -0.025801729410886765,
-0.008693721145391464, -0.0010926402173936367, -0.027235493063926697,
0.06945550441741943, 0.023456251248717308, -0.02160717360675335,
0.03252667561173439, 0.05874639376997948, -0.001329384627752006,
0.03664775192737579, -0.07353461533784866, -0.028453022241592407,
-0.05666429176926613, -0.012955721467733383, -0.041723109781742096,
0.07209191471338272, 0.0326194241642952, -0.0496046207845211,
-0.025037819519639015, 0.004625750705599785, -0.03622527793049812,
-0.022546149790287018, 0.0053468807600438595, 0.03879072889685631,
0.03238753229379654
]
여러 텍스트 임베딩
embedDocuments로 인덱싱을 위한 여러 텍스트를 임베딩할 수 있어요. 이 메서드에 사용되는 내부는 쿼리 임베딩과 다를 수 있지만(다를 필요는 없음) 있어요:
const text2 = "LangGraph is a library for building stateful, multi-actor applications with LLMs";
const vectors = await embeddings.embedDocuments([text, text2]);
console.log(vectors[0].slice(0, 100));
console.log(vectors[1].slice(0, 100));
[
-0.02831101417541504, 0.022063178941607475, -0.07454229146242142,
0.006448323838412762, 0.001955120824277401, -0.017617391422390938,
0.018649872392416, -0.05262855067849159, 0.0006953597767278552,
-0.0018249079585075378, 0.022437218576669693, 0.0036489504855126143,
0.0018086736090481281, 0.016940006986260414, -0.007894322276115417,
-0.04187627509236336, 0.039501357823610306, 0.06918870657682419,
-0.006931832991540432, 0.049655742943286896, 0.021211417391896248,
-0.029322246089577675, -0.04546992480754852, -0.01769082061946392,
0.046703994274139404, 0.03127637133002281, 0.006355373188853264,
0.014901148155331612, -0.006893016863614321, -0.05992589890956879,
-0.009733330458402634, 0.015709295868873596, -0.017982766032218933,
-0.0852997675538063, -0.032453566789627075, 0.0014507169835269451,
0.03345133736729622, 0.048862338066101074, 0.006664620712399483,
-0.06287197023630142, -0.02109423652291298, 0.018176473677158356,
-0.022175665944814682, 0.03340170532464981, -0.008905526250600815,
-0.03492079675197601, -0.03819998353719711, -0.05230168625712395,
-0.05247239023447037, 0.048254698514938354, 0.046494755893945694,
-0.029708227142691612, -0.002180763054639101, 0.051957979798316956,
-0.05483679473400116, 0.00700812041759491, -0.08181990683078766,
-0.02295914851129055, 0.026530204340815544, 0.04028692841529846,
-0.05230272561311722, -0.057705819606781006, -0.015022763051092625,
0.002143724123016, 0.06361843645572662, -0.027828887104988098,
0.006870461627840996, -0.016140831634402275, -0.034440942108631134,
-0.004059414379298687, -0.042537953704595566, -0.00984653178602457,
-0.07701274752616882, 0.09815558046102524, -0.025801729410886765,
-0.008693721145391464, -0.0010926402173936367, -0.027235493063926697,
0.06945550441741943, 0.023456251248717308, -0.02160717360675335,
0.03252667561173439, 0.05874639376997948, -0.001329384627752006,
0.03664775192737579, -0.07353461533784866, -0.028453022241592407,
-0.05666429176926613, -0.012955721467733383, -0.041723109781742096,
0.07209191471338272, 0.0326194241642952, -0.0496046207845211,
-0.025037819519639015, 0.004625750705599785, -0.03622527793049812,
-0.022546149790287018, 0.0053468807600438595, 0.03879072889685631,
0.03238753229379654
]
[
-0.00007261172140715644, 0.03209814056754112, -0.10099327564239502,
-0.0017932605696842074, -0.0016863049240782857, 0.009428824298083782,
0.023065969347953796, -0.018305035308003426, 0.03765229508280754,
0.03357342258095741, 0.0018431750359013677, 0.03230319544672966,
0.024983661249279976, 0.02752346731722355, -0.027390114963054657,
-0.01945030689239502, -0.05770668387413025, 0.046621184796094894,
-0.03308689966797829, 0.03985097259283066, -0.021250328049063683,
-0.001940526650287211, -0.06034174561500549, -0.05026412755250931,
0.02385033667087555, -0.03279203176498413, 0.02966252714395523,
0.01294293999671936, -0.009747475385665894, -0.07896383106708527,
-0.013269499875605106, -0.011228476651012897, 0.022224457934498787,
-0.018957728520035744, -0.05092151463031769, -0.043285638093948364,
0.016826728358864784, 0.010665969923138618, 0.021219193935394287,
-0.08588971197605133, -0.038367897272109985, 0.012244532816112041,
0.009497410617768764, 0.017629485577344894, 0.0013116559712216258,
-0.016468070447444916, -0.04423798993229866, -0.04043079912662506,
-0.05485917255282402, -0.007577189709991217, 0.028067218139767647,
-0.022974666208028793, 0.0006999042234383523, 0.009812192991375923,
-0.05387532711029053, -0.016531387344002724, -0.015153753571212292,
0.03397523611783981, -0.0018232968868687749, 0.01200891938060522,
-0.013123664073646069, -0.043459296226501465, -0.01856262981891632,
0.018269911408424377, 0.016155652701854706, -0.05597233399748802,
-0.05852395296096802, 0.020076945424079895, -0.033808667212724686,
-0.008225022815167904, -0.014589417725801468, -0.01408824510872364,
-0.06293410807847977, 0.026668129488825798, -0.01397104375064373,
-0.017627086490392685, -0.03409220278263092, -0.018559949472546577,
0.07163946330547333, 0.015611495822668076, -0.034166790544986725,
-0.005098687019199133, 0.04163505882024765, -0.010681619867682457,
0.027817489579319954, -0.031076539307832718, -0.006825212389230728,
-0.06810358166694641, -0.03793689236044884, -0.03981738165020943,
0.09524374455213547, -0.03607913851737976, 0.003638653317466378,
0.02828306518495083, 0.018808560445904732, -0.047244682908058167,
-0.06114668399095535, -0.02395530976355076, 0.036157332360744476,
0.0422002375125885
]
API 레퍼런스
모든 VertexAIEmbeddings 기능과 구성에 대한 자세한 문서는 API 레퍼런스를 참고하세요.
더 알아보기
- 이 문서를 MCP로 연결하면 Claude, VSCode 등에서 실시간 답변을 받을 수 있어요.
- GitHub에서 이 페이지 편집하기 또는 이슈 제출하기.