저장

저장 (Storing)

데이터를 로드하고 인덱싱했다면, 재인덱싱하는 시간과 비용을 피하기 위해 그 데이터를 저장하고 싶을 거예요. 기본적으로 인덱스된 데이터는 메모리에만 저장돼요.

디스크에 영구 저장

인덱스된 데이터를 저장하는 가장 간단한 방법은 모든 Index에 내장된 .persist() 메서드를 사용해 지정한 위치에 모든 데이터를 디스크에 쓰는 거예요. 어떤 유형의 인덱스든 동작해요.

index.storage_context.persist(persist_dir="<persist_dir>")

Composable Graph의 예를 들면:

graph.root_index.storage_context.persist(persist_dir="<persist_dir>")

그러면 저장된 인덱스를 다음과 같이 로드해 데이터를 다시 로드하고 재인덱싱하는 일을 피할 수 있어요:

from llama_index.core import StorageContext, load_index_from_storage


# rebuild storage context
storage_context = StorageContext.from_defaults(persist_dir="<persist_dir>")


# load index
index = load_index_from_storage(storage_context)

중요: 인덱스를 커스텀 transformations, embed_model 등으로 초기화했다면, load_index_from_storage에 같은 옵션을 넘기거나 전역 설정으로 설정해야 해요.

벡터 스토어 사용하기

인덱싱에서 논의했듯, 가장 흔한 Index 유형 중 하나가 VectorStoreIndex예요. VectorStoreIndex에서 임베딩을 만드는 API 호출은 시간과 비용 면에서 비쌀 수 있어서, 계속 재인덱싱하지 않도록 저장해 두고 싶을 거예요.

LlamaIndex는 아주 많은 수의 벡터 스토어를 지원하며, 아키텍처·복잡도·비용이 저마다 달라요. 이 예제에서는 오픈소스 벡터 스토어인 Chroma를 사용할게요.

먼저 chroma를 설치해야 해요:

pip install chromadb

Chroma를 사용해 VectorStoreIndex의 임베딩을 저장하려면 다음이 필요해요:

  • Chroma 클라이언트 초기화
  • Chroma에 데이터를 저장할 Collection 생성
  • StorageContext에서 vector_store로 Chroma 지정
  • 해당 StorageContext로 VectorStoreIndex 초기화

실제로 데이터를 쿼리하는 것까지 미리 보면 이렇게 돼요:

import chromadb
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContext


# load some documents
documents = SimpleDirectoryReader("./data").load_data()


# initialize client, setting path to save data
db = chromadb.PersistentClient(path="./chroma_db")


# create collection
chroma_collection = db.get_or_create_collection("quickstart")


# assign chroma as the vector_store to the context
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)


# create your index
index = VectorStoreIndex.from_documents(
    documents, storage_context=storage_context
)


# create a query engine and query
query_engine = index.as_query_engine()
response = query_engine.query("What is the meaning of life?")
print(response)

이미 임베딩을 만들고 저장했다면, 문서를 로드하거나 새 VectorStoreIndex를 만들지 않고 바로 임베딩을 로드하고 싶을 거예요:

import chromadb
from llama_index.core import VectorStoreIndex
from llama_index.vector_stores.chroma import ChromaVectorStore
from llama_index.core import StorageContext


# initialize client
db = chromadb.PersistentClient(path="./chroma_db")


# get collection
chroma_collection = db.get_or_create_collection("quickstart")


# assign chroma as the vector_store to the context
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)


# load your index from stored vectors
index = VectorStoreIndex.from_vector_store(
    vector_store, storage_context=storage_context
)


# create a query engine
query_engine = index.as_query_engine()
response = query_engine.query("What is llama2?")
print(response)

이 스토어를 더 깊이 파고들고 싶다면 Chroma 사용의 더 철저한 예시가 있어요.

이제 쿼리할 준비가 됐어요!

이제 데이터를 로드하고, 인덱싱하고, 그 인덱스를 저장했으니 데이터를 쿼리할 준비가 돼요.

문서나 노드 삽입

이미 인덱스를 만들었다면 insert 메서드로 인덱스에 새 문서를 추가할 수 있어요.

from llama_index.core import VectorStoreIndex


index = VectorStoreIndex([])
for doc in documents:
    index.insert(doc)

문서 관리에 대한 더 자세한 내용과 예제 노트북은 문서 관리 how-to를 참고해요.

더 알아보기