가이드: 기존 Weaviate 벡터 저장소로 벡터 저장소 인덱스 사용하기
가이드: 기존 Weaviate 벡터 저장소로 벡터 저장소 인덱스 사용하기 (Using Vector Store Index with Existing Weaviate Vector Store)
이번엔 이미 데이터가 들어 있는 Weaviate 컬렉션을 LlamaIndex에서 그대로 활용하는 방법을 배워볼게요. 벡터 저장소 인덱스를 기존 Weaviate 클래스와 연결하고, 'text' 필드로 쓸 프로퍼티를 지정해 주면 곧바로 검색할 수 있답니다. 속성 선택만 정확히 하면 나머지는 간단해요.
출처: 문서
본문
콜랩 노트북에서 열고 있다면 LlamaIndex 설치가 필요할 거예요 🦙.
%pip install llama-index-vector-stores-weaviate
%pip install llama-index-embeddings-openai
!pip install llama-index
import weaviate
client = weaviate.Client("https://test-cluster-bbn8vqsn.weaviate.network")
기존 Weaviate 벡터 저장소 준비하기 (Prepare Sample "Existing" Weaviate Vector Store)
스키마 정의 (Define schema)
4개의 프로퍼티(title(str), author(str), content(str), year(int))를 가진 "Book" 클래스의 스키마를 만듭니다.
try:
client.schema.delete_class("Book")
except:
pass
schema = {
"classes": [
{
"class": "Book",
"properties": [
{"name": "title", "dataType": ["text"]},
{"name": "author", "dataType": ["text"]},
{"name": "content", "dataType": ["text"]},
{"name": "year", "dataType": ["int"]},
],
},
]
}
if not client.schema.contains(schema):
client.schema.create(schema)
샘플 데이터 정의 (Define sample data)
4개의 샘플 책을 만듭니다.
books = [
{
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"content": (
"To Kill a Mockingbird is a novel by Harper Lee published in"
" 1960..."
),
"year": 1960,
},
{
"title": "1984",
"author": "George Orwell",
"content": (
"1984 is a dystopian novel by George Orwell published in 1949..."
),
"year": 1949,
},
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"content": (
"The Great Gatsby is a novel by F. Scott Fitzgerald published in"
" 1925..."
),
"year": 1925,
},
{
"title": "Pride and Prejudice",
"author": "Jane Austen",
"content": (
"Pride and Prejudice is a novel by Jane Austen published in"
" 1813..."
),
"year": 1813,
},
]
데이터 추가 (Add data)
샘플 책을 Weaviate "Book" 클래스에 추가합니다(content 필드를 임베딩해서).
from llama_index.embeddings.openai import OpenAIEmbedding
embed_model = OpenAIEmbedding()
with client.batch as batch:
for book in books:
vector = embed_model.get_text_embedding(book["content"])
batch.add_data_object(
data_object=book, class_name="Book", vector=vector
)
기존 Weaviate 벡터 저장소에 대해 쿼리하기 (Query Against "Existing" Weaviate Vector Store)
from llama_index.vector_stores.weaviate import WeaviateVectorStore
from llama_index.core import VectorStoreIndex
from llama_index.core.response.pprint_utils import pprint_source_node
원하는 Weaviate 클래스와 일치하는 "index_name"을 올바르게 지정하고, 클래스 프로퍼티를 'text' 필드로 선택해야 합니다.
vector_store = WeaviateVectorStore(
weaviate_client=client, index_name="Book", text_key="content"
)
retriever = VectorStoreIndex.from_vector_store(vector_store).as_retriever(
similarity_top_k=1
)
nodes = retriever.retrieve("What is that book about a bird again?")
검색된 노드를 살펴봅시다. 책 데이터가 LlamaIndex Node 객체로 로드되었고, "content" 필드가 주요 텍스트로 사용되었음을 확인할 수 있습니다.
pprint_source_node(nodes[0])
Document ID: cf927ce7-0672-4696-8aae-7e77b33b9659
Similarity: None
Text: author: Harper Lee title: To Kill a Mockingbird year: 1960 To
Kill a Mockingbird is a novel by Harper Lee published in 1960......
나머지 필드는 메타데이터(metadata)로 로드되어야 합니다.
nodes[0].node.metadata
{'author': 'Harper Lee', 'title': 'To Kill a Mockingbird', 'year': 1960}