----------------------
LlamaIndex provides multiple integration points with vector databases, which can be used to restrict the LLM to answer based on the data inside the vector database.
- LlamaIndex can use a vector store itself as an index. This index can store documents and be used to answer queries.
- LlamaIndex can load data from vector stores, similar to any other data connector. This data can then be used within LlamaIndex data structures.
For example, you can perform auto-retrieval in LlamaIndex. Given a natural language query, LlamaIndex first uses the LLM to infer a set of metadata filters as well as the right query string to pass to the vector database. 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.
Here is an example of how to set up auto-retrieval with a vector database using Pinecone:
import pinecone
import os
api_key = os.environ["PINECONE_API_KEY"]
pinecone.init(api_key=api_key, environment="us-west1-gcp")
# dimensions are for text-embedding-ada-002
try:
pinecone.create_index(
"quickstart", dimension=1536, metric="euclidean", pod_type="p1"
)
except Exception:
# most likely index already exists
pass
pinecone_index = pinecone.Index("quickstart")
from llama_index import VectorStoreIndex, StorageContext
from llama_index.vector_stores import PineconeVectorStore
from llama_index.schema import TextNode
This code initializes a Pinecone vector database, creates an index, and sets up the necessary imports from LlamaIndex to use this vector database.