잉제스트 파이프라인

잉제스트 파이프라인 (Ingestion Pipeline)

IngestionPipeline은 입력 데이터에 적용되는 일련의 변환(Transformations) 개념을 사용해요. 변환을 거친 결과 노드는 반환되거나, 주어진 벡터 데이터베이스에 자동으로 삽입됩니다. 문서를 "잘게 쪼개고 → 메타데이터를 붙이고 → 임베딩으로 바꾸고 → 저장"하는 일련의 과정을 한 곳에서 다루는 도구예요.

출처: 공식문서

설치

npm i llamaindex @llamaindex/openai @llamaindex/qdrant

사용 패턴

가장 간단한 사용법은 잉제스트 파이프라인을 다음과 같이 인스턴스화하는 거예요.

import fs from "node:fs/promises";
import { OpenAI, OpenAIEmbedding } from "@llamaindex/openai";
import {
  Document,
  IngestionPipeline,
  MetadataMode,
  TitleExtractor,
  SentenceSplitter,
} from "llamaindex";


async function main() {
  // Load essay from abramov.txt in Node
  const path = "node_modules/llamaindex/examples/abramov.txt";


  const essay = await fs.readFile(path, "utf-8");


  // Create Document object with essay
  const document = new Document({ text: essay, id_: path });
  const pipeline = new IngestionPipeline({
    transformations: [
      new SentenceSplitter({ chunkSize: 1024, chunkOverlap: 20 }),
      new TitleExtractor(),
      new OpenAIEmbedding(),
    ],
  });


  // run the pipeline
  const nodes = await pipeline.run({ documents: [document] });


  // print out the result of the pipeline run
  for (const node of nodes) {
    console.log(node.getContent(MetadataMode.NONE));
  }
}


main().catch(console.error);

여기서 눈여겨볼 점은 변환 배열이에요. 문장 분할 → 제목 추출 → 임베딩 순서로 데이터가 가공되고, 각 단계가 이전 단계의 출력을 받아요.

벡터 데이터베이스 연결

잉제스트 파이프라인을 실행할 때, 결과 노드를 원격 벡터 스토어에 자동으로 삽입하도록 선택할 수도 있어요. 그러면 나중에 그 벡터 스토어에서 인덱스를 구성할 수 있습니다.

import fs from "node:fs/promises";


import { OpenAIEmbedding } from "@llamaindex/openai";
import { QdrantVectorStore } from "@llamaindex/qdrant";
import {
  Document,
  IngestionPipeline,
  MetadataMode,
  TitleExtractor,
  SentenceSplitter,
  VectorStoreIndex,
} from "llamaindex";


async function main() {
  // Load essay from abramov.txt in Node
  const path = "node_modules/llamaindex/examples/abramov.txt";


  const essay = await fs.readFile(path, "utf-8");


  const vectorStore = new QdrantVectorStore({
    host: "http://localhost:6333",
  });


  // Create Document object with essay
  const document = new Document({ text: essay, id_: path });
  const pipeline = new IngestionPipeline({
    transformations: [
      new SentenceSplitter({ chunkSize: 1024, chunkOverlap: 20 }),
      new TitleExtractor(),
      new OpenAIEmbedding(),
    ],
    vectorStore,
  });


  // run the pipeline
  const nodes = await pipeline.run({ documents: [document] });


  // create an index
  const index = VectorStoreIndex.fromVectorStore(vectorStore);
}


main().catch(console.error);

핵심은 파이프라인에 vectorStore를 넘기면, 파이프라인이 실행되면서 결과 노드를 그 벡터 스토어에 직접 저장하고, 나중에 VectorStoreIndex.fromVectorStore로 그 저장소에서 인덱스를 만들 수 있다는 점이에요.

더 알아보기