Weaviate Vector Store
Source: llama_index

<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/docs/examples/vector_stores/WeaviateIndexDemo.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a># Weaviate Vector StoreIf you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙.```python%pip install llama-index-vector-stores-weaviate``````python!pip install llama-index```#### Creating a Weaviate Client```pythonimport osimport openaios.environ["OPENAI_API_KEY"] = ""openai.api_key = os.environ["OPENAI_API_KEY"]``````pythonimport loggingimport syslogging.basicConfig(stream=sys.stdout, level=logging.INFO)logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))``````pythonimport weaviate``````python# cloudcluster_url = ""api_key = ""client = weaviate.connect_to_wcs(    cluster_url=cluster_url,    auth_credentials=weaviate.auth.AuthApiKey(api_key),)# local# client = connect_to_local()```#### Load documents, build the VectorStoreIndex```pythonfrom llama_index.core import VectorStoreIndex, SimpleDirectoryReaderfrom llama_index.vector_stores.weaviate import WeaviateVectorStorefrom IPython.display import Markdown, display```Download Data```python!mkdir -p 'data/paul_graham/'!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'``````python# load documentsdocuments = SimpleDirectoryReader("./data/paul_graham").load_data()``````pythonfrom 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)```#### Query Index```python# set Logging to DEBUG for more detailed outputsquery_engine = index.as_query_engine()response = query_engine.query("What did the author do growing up?")``````pythondisplay(Markdown(f"<b>{response}</b>"))```## Loading the indexHere, we use the same index name as when we created the initial index. This stops it from being auto-generated and allows us to easily connect back to it.```pythoncluster_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()``````pythonvector_store = WeaviateVectorStore(    weaviate_client=client, index_name="LlamaIndex")loaded_index = VectorStoreIndex.from_vector_store(vector_store)``````python# set Logging to DEBUG for more detailed outputsquery_engine = loaded_index.as_query_engine()response = query_engine.query("What happened at interleaf?")display(Markdown(f"<b>{response}</b>"))```## Metadata FilteringLet's insert a dummy document, and try to filter so that only that document is returned.```pythonfrom llama_index.core import Documentdoc = Document.example()print(doc.metadata)print("-----")print(doc.text[:100])``````pythonloaded_index.insert(doc)``````pythonfrom llama_index.core.vector_stores import ExactMatchFilter, MetadataFiltersfilters = 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>"))```# Deleting the index completelyYou can delete the index created by the vector store using the `delete_index` function```pythonvector_store.delete_index()``````pythonvector_store.delete_index()  # calling the function again does nothing```# Connection TerminationYou must ensure your client connections are closed:```pythonclient.close()```