主题
如果你在 Colab 中打开这个 Notebook,可能需要先安装 LlamaIndex 🦙。
%pip install llama-index-vector-stores-weaviate!pip install llama-index创建 Weaviate 客户端
import os
import openai
os.environ["OPENAI_API_KEY"] = ""
openai.api_key = os.environ["OPENAI_API_KEY"]import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))import weaviate# cloud
cluster_url = ""
api_key = ""
client = weaviate.connect_to_wcs(
cluster_url=cluster_url,
auth_credentials=weaviate.auth.AuthApiKey(api_key),
)
# local
# client = connect_to_local()加载文档,构建 VectorStoreIndex
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.vector_stores.weaviate import WeaviateVectorStore
from IPython.display import Markdown, display下载数据
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'# load documents
documents = SimpleDirectoryReader("./data/paul_graham").load_data()from llama_index.core import StorageContext
# If you want to load the index later, be sure to give it a name!
vector_store = WeaviateVectorStore(
weaviate_client=client, index_name="LlamaIndex"
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
documents, storage_context=storage_context
)
# NOTE: you may also choose to define a index_name manually.
# index_name = "test_prefix"
# vector_store = WeaviateVectorStore(weaviate_client=client, index_name=index_name)使用自定义批处理配置
LlamaIndex 默认使用 Weaviate 的动态批处理,并针对大多数常见场景进行了优化。但在低延迟环境中,这可能会使服务器过载或达到现有 GRPC 消息大小上限。为了获得更多控制和更好的摄取流程,可以考虑使用固定大小的批次来调整批大小。
下面介绍如何微调 WeaviateVectorStore 并定义自定义批次:
from weaviate.classes.config import ConsistencyLevel
custom_batch = client.batch.fixed_size(
batch_size=123,
concurrent_requests=3,
consistency_level=ConsistencyLevel.ALL,
)
vector_store_fixed = WeaviateVectorStore(
weaviate_client=client,
index_name="LlamaIndex",
# we pass our custom batch as a client_kwargs
client_kwargs={"custom_batch": custom_batch},
)查询索引
# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine()
response = query_engine.query("What did the author do growing up?")display(Markdown(f"<b>{response}</b>"))加载索引
这里,我们使用与创建初始索引时相同的索引名称。这样可以避免自动生成索引名,并让我们轻松地重新连接到它。
cluster_url = ""
api_key = ""
client = weaviate.connect_to_wcs(
cluster_url=cluster_url,
auth_credentials=weaviate.auth.AuthApiKey(api_key),
)
# local
# client = weaviate.connect_to_local()vector_store = WeaviateVectorStore(
weaviate_client=client, index_name="LlamaIndex"
)
loaded_index = VectorStoreIndex.from_vector_store(vector_store)# set Logging to DEBUG for more detailed outputs
query_engine = loaded_index.as_query_engine()
response = query_engine.query("What happened at interleaf?")
display(Markdown(f"<b>{response}</b>"))元数据过滤
让我们插入一个虚拟文档,并尝试通过过滤只返回该文档。
from llama_index.core import Document
doc = Document.example()
print(doc.metadata)
print("-----")
print(doc.text[:100])loaded_index.insert(doc)from llama_index.core.vector_stores import ExactMatchFilter, MetadataFilters
filters = MetadataFilters(
filters=[ExactMatchFilter(key="filename", value="README.md")]
)
query_engine = loaded_index.as_query_engine(filters=filters)
response = query_engine.query("What is the name of the file?")
display(Markdown(f"<b>{response}</b>"))完全删除索引
你可以使用 delete_index 函数删除由向量存储创建的索引
vector_store.delete_index()vector_store.delete_index() # calling the function again does nothing连接终止
你必须确保客户端连接已关闭:
client.close()