PineconeStore 통합
PineconeStore 통합
LangChain JavaScript로 PineconeStore와 통합해요.
Pinecone은 세계 최고의 기업들의 AI를 지원하는 벡터 데이터베이스예요.
이 가이드는 Pinecone 벡터 스토어 시작을 위한 빠른 개요를 제공해요. 모든 PineconeStore 기능과 구성에 대한 자세한 문서는 API reference를 참고하세요.
개요 (Overview)
통합 세부 정보 (Integration details)
| 클래스 | 패키지 | PY 지원 | Downloads | Version |
|---|---|---|---|---|
PineconeStore |
@langchain/pinecone |
✅ |
설정 (Setup)
Pinecone 벡터 스토어를 사용하려면 Pinecone 계정을 만들고, 인덱스를 초기화하고, @langchain/pinecone, @langchain/core, 공식 Pinecone SDK(@pinecone-database/pinecone v5.x)를 설치해 PineconeStore용 클라이언트를 초기화하세요.
이 가이드는 OpenAI 임베딩을 예시로 사용해요. 대신 다른 지원 임베딩 모델을 사용할 수도 있어요.
yarn add @langchain/pinecone @langchain/openai @langchain/core @pinecone-database/pinecone@5
pnpm add @langchain/pinecone @langchain/openai @langchain/core @pinecone-database/pinecone@5
자격 증명 (Credentials)
Pinecone 계정에 가입하고 인덱스를 만드세요. 차원(dimensions)이 사용할 임베딩과 일치하는지 확인하세요 (기본값은 OpenAI text-embedding-3-small의 1536). 완료되면 PINECONE_INDEX, PINECONE_API_KEY, 그리고 (선택적으로) PINECONE_ENVIRONMENT 환경 변수를 설정하세요:
process.env.PINECONE_API_KEY = "your-pinecone-api-key";
process.env.PINECONE_INDEX = "your-pinecone-index";
// Optional
process.env.PINECONE_ENVIRONMENT = "your-pinecone-environment";
이 가이드에 OpenAI 임베딩을 사용한다면 OpenAI 키도 설정하세요:
process.env.OPENAI_API_KEY = "YOUR_API_KEY";
모델 호출의 자동 추적을 받으려면 아래 주석을 해제해 LangSmith API 키를 설정할 수도 있어요:
// process.env.LANGSMITH_TRACING="true"
// process.env.LANGSMITH_API_KEY="your-api-key"
인스턴스화 (Instantiation)
import { PineconeStore } from "@langchain/pinecone";
import { OpenAIEmbeddings } from "@langchain/openai";
import { Pinecone as PineconeClient } from "@pinecone-database/pinecone";
const embeddings = new OpenAIEmbeddings({
model: "text-embedding-3-small",
});
const pinecone = new PineconeClient();
// Will automatically read the PINECONE_API_KEY and PINECONE_ENVIRONMENT env vars
const pineconeIndex = pinecone.Index(process.env.PINECONE_INDEX!);
const vectorStore = await PineconeStore.fromExistingIndex(
embeddings,
{
pineconeIndex,
// Maximum number of batch requests to allow at once. Each batch is 1000 vectors.
maxConcurrency: 5,
// You can pass a namespace here too
// namespace: "foo",
}
);
벡터 스토어 관리 (Manage vector store)
벡터 스토어에 항목 추가 (Add items to vector store)
import type { Document } from "@langchain/core/documents";
const document1: Document = {
pageContent: "The powerhouse of the cell is the mitochondria",
metadata: { source: "https://example.com" }
};
const document2: Document = {
pageContent: "Buildings are made out of brick",
metadata: { source: "https://example.com" }
};
const document3: Document = {
pageContent: "Mitochondria are made out of lipids",
metadata: { source: "https://example.com" }
};
const document4: Document = {
pageContent: "The 2024 Olympics are in Paris",
metadata: { source: "https://example.com" }
}
const documents = [document1, document2, document3, document4];
await vectorStore.addDocuments(documents, { ids: ["1", "2", "3", "4"] });
[ '1', '2', '3', '4' ]
참고: 문서를 추가한 후 쿼리 가능해지기까지 약간의 지연이 있어요.
벡터 스토어에서 항목 삭제 (Delete items from vector store)
await vectorStore.delete({ ids: ["4"] });
벡터 스토어 쿼리 (Query vector store)
직접 쿼리 (Query directly)
간단한 유사도 검색은 다음과 같이 수행할 수 있어요:
// Optional filter
const filter = { source: "https://example.com" };
const similaritySearchResults = await vectorStore.similaritySearch("biology", 2, filter);
for (const doc of similaritySearchResults) {
console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);
}
* The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* Mitochondria are made out of lipids [{"source":"https://example.com"}]
유사도 검색을 실행하고 대응하는 점수를 받으려면 다음을 실행할 수 있어요:
const similaritySearchWithScoreResults = await vectorStore.similaritySearchWithScore("biology", 2, filter)
for (const [doc, score] of similaritySearchWithScoreResults) {
console.log(`* [SIM=${score.toFixed(3)}] ${doc.pageContent} [${JSON.stringify(doc.metadata)}]`);
}
* [SIM=0.165] The powerhouse of the cell is the mitochondria [{"source":"https://example.com"}]
* [SIM=0.148] Mitochondria are made out of lipids [{"source":"https://example.com"}]
리트리버로 변환해 쿼리 (Query by turning into retriever)
벡터 스토어를 리트리버로 변환해 체인에서 더 쉽게 사용할 수도 있어요.
const retriever = vectorStore.asRetriever({
// Optional filter
filter: filter,
k: 2,
});
await retriever.invoke("biology");
검색 증강 생성(RAG) 사용법 (Usage for retrieval-augmented generation)
이 벡터 스토어를 검색 증강 생성(RAG)에 사용하는 방법에 대한 가이드는 다음 섹션을 참고하세요:
API reference
모든 PineconeStore 기능과 구성에 대한 자세한 문서는 API reference를 참고하세요.
출처: 문서
본문
PineconeStore는 Pinecone 벡터 데이터베이스용 벡터 스토어 통합이에요. @langchain/pinecone 패키지에서 가져와 PineconeClient·pineconeIndex로 fromExistingIndex를 호출해 인스턴스화하고, addDocuments(id 지원)·delete·similaritySearch·similaritySearchWithScore·asRetriever()를 사용할 수 있어요. PINECONE_API_KEY·PINECONE_INDEX 환경 변수로 자격 증명을 설정해요.