Skip to content
Snippets Groups Projects
Unverified Commit 446949e3 authored by Leonie's avatar Leonie Committed by GitHub
Browse files

Add Auto-retriever tutorial for Weaviate (#11885)


* Add Auto-retriever tutorial for Weaviate

* Include review comments

* cr

---------

Co-authored-by: default avatarLeonie <leonie@Leonies-MBP-2.fritz.box>
Co-authored-by: default avatarHaotian Zhang <socool.king@gmail.com>
parent ddafdc18
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:0e81b124 tags:
<a href="https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/vector_stores/WeaviateIndex_auto_retriever.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
%% Cell type:markdown id:307804a3-c02b-4a57-ac0d-172c30ddc851 tags:
# Auto-Retrieval from a Weaviate Vector Database
This guide shows how to perform **auto-retrieval** in LlamaIndex with [Weaviate](https://weaviate.io/).
The Weaviate vector database supports a set of [metadata filters](https://weaviate.io/developers/weaviate/search/filters) in addition to a query string for semantic search. Given a natural language query, we first use a Large Language Model (LLM) to infer a set of metadata filters as well as the right query string to pass to the vector database (either can also be blank). This overall query bundle is then executed against the vector database.
This allows for more dynamic, expressive forms of retrieval beyond top-k semantic search. The relevant context for a given query may only require filtering on a metadata tag, or require a joint combination of filtering + semantic search within the filtered set, or just raw semantic search.
%% Cell type:markdown id:f7010b1d-d1bb-4f08-9309-a328bb4ea396 tags:
## Setup
We first define imports and define an empty Weaviate collection.
%% Cell type:markdown id:31faecfb tags:
If you're opening this Notebook on Colab, you will probably need to install LlamaIndex 🦙.
%% Cell type:code id:13223201 tags:
``` python
%pip install llama-index-vector-stores-weaviate
```
%% Cell type:code id:53d3d6e7 tags:
``` python
!pip install llama-index weaviate-client
```
%% Cell type:code id:d48af8e1 tags:
``` python
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
```
%% Cell type:markdown id:198ea1cc tags:
We will be using GPT-4 for its reasoning capabilities to infer the metadata filters. Depending on your use case, `"gpt-3.5-turbo"` can work as well.
%% Cell type:code id:bf49ac18 tags:
``` python
# set up OpenAI
import os
import getpass
import openai
os.environ["OPENAI_API_KEY"] = getpass.getpass("OpenAI API Key:")
openai.api_key = os.environ["OPENAI_API_KEY"]
```
%% Cell type:code id:f2819b6c tags:
``` python
from llama_index.embeddings.openai import OpenAIEmbedding
from llama_index.llms.openai import OpenAI
from llama_index.core.settings import Settings
Settings.llm = OpenAI(model="gpt-4")
Settings.embed_model = OpenAIEmbedding()
```
%% Output
INFO:numexpr.utils:Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
Note: NumExpr detected 10 cores but "NUMEXPR_MAX_THREADS" not set, so enforcing safe limit of 8.
INFO:numexpr.utils:NumExpr defaulting to 8 threads.
NumExpr defaulting to 8 threads.
%% Cell type:markdown id:a9d3d43c tags:
This Notebook uses Weaviate in [Embedded mode](https://weaviate.io/developers/weaviate/installation/embedded), which is supported on Linux and macOS.
If you prefer to try out Weaviate's fully managed service, [Weaviate Cloud Services (WCS)](https://weaviate.io/developers/weaviate/installation/weaviate-cloud-services), you can enable the code in the comments.
%% Cell type:code id:0ce3143d-198c-4dd2-8e5a-c5cdf94f017a tags:
``` python
import weaviate
from weaviate.embedded import EmbeddedOptions
# Connect to Weaviate client in embedded mode
client = weaviate.Client(embedded_options=EmbeddedOptions())
# Enable this code if you want to use Weaviate Cloud Services instead of Embedded mode.
"""
import weaviate
# cloud
resource_owner_config = weaviate.AuthClientPassword(
username="",
password="",
)
client = weaviate.Client(
"https://test.weaviate.network",
auth_client_secret=resource_owner_config,
)
# local
# client = weaviate.Client("http://localhost:8081")
"""
```
%% Output
embedded weaviate is already listening on port 8079
/opt/homebrew/lib/python3.11/site-packages/weaviate/warnings.py:121: DeprecationWarning: Dep005: You are using weaviate-client version 3.26.2. The latest version is 4.5.2.
Please consider upgrading to the latest version. See https://weaviate.io/developers/weaviate/client-libraries/python for details.
warnings.warn(
'\nimport weaviate\n\n# cloud\nresource_owner_config = weaviate.AuthClientPassword(\n username="",\n password="",\n)\nclient = weaviate.Client(\n "https://test.weaviate.network",\n auth_client_secret=resource_owner_config,\n)\n\n# local\n# client = weaviate.Client("http://localhost:8081")\n'
%% Cell type:markdown id:41aa106b-8261-4a01-97c6-1b037dffa1b4 tags:
## Defining Some Sample Data
We insert some sample nodes containing text chunks into the vector database. Note that each `TextNode` not only contains the text, but also metadata e.g. `category` and `country`. These metadata fields will get converted/stored as such in the underlying vector db.
%% Cell type:code id:68cbd239-880e-41a3-98d8-dbb3fab55431 tags:
``` python
from llama_index.core.schema import TextNode
nodes = [
TextNode(
text=(
"Michael Jordan is a retired professional basketball player,"
" widely regarded as one of the greatest basketball players of all"
" time."
),
metadata={
"category": "Sports",
"country": "United States",
},
),
TextNode(
text=(
"Angelina Jolie is an American actress, filmmaker, and"
" humanitarian. She has received numerous awards for her acting"
" and is known for her philanthropic work."
),
metadata={
"category": "Entertainment",
"country": "United States",
},
),
TextNode(
text=(
"Elon Musk is a business magnate, industrial designer, and"
" engineer. He is the founder, CEO, and lead designer of SpaceX,"
" Tesla, Inc., Neuralink, and The Boring Company."
),
metadata={
"category": "Business",
"country": "United States",
},
),
TextNode(
text=(
"Rihanna is a Barbadian singer, actress, and businesswoman. She"
" has achieved significant success in the music industry and is"
" known for her versatile musical style."
),
metadata={
"category": "Music",
"country": "Barbados",
},
),
TextNode(
text=(
"Cristiano Ronaldo is a Portuguese professional footballer who is"
" considered one of the greatest football players of all time. He"
" has won numerous awards and set multiple records during his"
" career."
),
metadata={
"category": "Sports",
"country": "Portugal",
},
),
]
```
%% Cell type:markdown id:e8bd70be-57c7-49e2-990b-ad9a876710fb tags:
## Build Vector Index with Weaviate Vector Store
Here we load the data into the vector store. As mentioned above, both the text and metadata for each node will get converted into corresopnding representations in Weaviate. We can now run semantic queries and also metadata filtering on this data from Weaviate.
%% Cell type:code id:ba1558b3 tags:
``` python
from llama_index.core import VectorStoreIndex, StorageContext
from llama_index.vector_stores.weaviate import WeaviateVectorStore
vector_store = WeaviateVectorStore(
weaviate_client=client, index_name="LlamaIndex_filter"
)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
```
%% Cell type:code id:35369eda tags:
``` python
index = VectorStoreIndex(nodes, storage_context=storage_context)
```
%% Output
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
%% Cell type:markdown id:c793dc45-5087-4dcb-b0d3-85b8e718539f tags:
## Define `VectorIndexAutoRetriever`
We define our core `VectorIndexAutoRetriever` module. The module takes in `VectorStoreInfo`,
which contains a structured description of the vector store collection and the metadata filters it supports.
This information will then be used in the auto-retrieval prompt where the LLM infers metadata filters.
%% Cell type:code id:bedbb693-725f-478f-be26-fa7180ea38b2 tags:
``` python
from llama_index.core.retrievers import VectorIndexAutoRetriever
from llama_index.core.vector_stores.types import MetadataInfo, VectorStoreInfo
vector_store_info = VectorStoreInfo(
content_info="brief biography of celebrities",
metadata_info=[
MetadataInfo(
name="category",
type="str",
description=(
"Category of the celebrity, one of [Sports, Entertainment,"
" Business, Music]"
),
),
MetadataInfo(
name="country",
type="str",
description=(
"Country of the celebrity, one of [United States, Barbados,"
" Portugal]"
),
),
],
)
retriever = VectorIndexAutoRetriever(
index, vector_store_info=vector_store_info
)
```
%% Cell type:markdown id:32808a60-7bab-4e9e-944c-cfe2ed0b0e2e tags:
## Running over some sample data
We try running over some sample data. Note how metadata filters are inferred - this helps with more precise retrieval!
%% Cell type:code id:eeb18e9c tags:
``` python
response = retriever.retrieve("Tell me about celebrities from United States")
```
%% Output
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
INFO:llama_index.core.indices.vector_store.retrievers.auto_retriever.auto_retriever:Using query str: Tell me about celebrities
Using query str: Tell me about celebrities
INFO:llama_index.core.indices.vector_store.retrievers.auto_retriever.auto_retriever:Using filters: [('country', '==', 'United States')]
Using filters: [('country', '==', 'United States')]
INFO:llama_index.core.indices.vector_store.retrievers.auto_retriever.auto_retriever:Using top_k: 2
Using top_k: 2
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
%% Cell type:code id:ee543cf6 tags:
``` python
print(response[0])
```
%% Output
Node ID: a6ba3fa4-3ced-4281-9cf3-df9e73bb92d2
Text: Angelina Jolie is an American actress, filmmaker, and
humanitarian. She has received numerous awards for her acting and is
known for her philanthropic work.
Score: 0.790
%% Cell type:code id:51f00cde tags:
``` python
response = retriever.retrieve(
"Tell me about Sports celebrities from United States"
)
```
%% Output
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
HTTP Request: POST https://api.openai.com/v1/chat/completions "HTTP/1.1 200 OK"
INFO:llama_index.core.indices.vector_store.retrievers.auto_retriever.auto_retriever:Using query str: Sports celebrities
Using query str: Sports celebrities
INFO:llama_index.core.indices.vector_store.retrievers.auto_retriever.auto_retriever:Using filters: [('category', '==', 'Sports'), ('country', '==', 'United States')]
Using filters: [('category', '==', 'Sports'), ('country', '==', 'United States')]
INFO:llama_index.core.indices.vector_store.retrievers.auto_retriever.auto_retriever:Using top_k: 2
Using top_k: 2
INFO:httpx:HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
HTTP Request: POST https://api.openai.com/v1/embeddings "HTTP/1.1 200 OK"
%% Cell type:code id:345d3ca3 tags:
``` python
print(response[0])
```
%% Output
Node ID: 5e689253-2e2c-440d-9f72-6f9513fd2c3a
Text: Michael Jordan is a retired professional basketball player,
widely regarded as one of the greatest basketball players of all time.
Score: 0.797
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment