Integrations

LlamaIndex with Upstash Vector

You can use LlamaIndex with Upstash Vector to perform Retrieval-Augmented Generation (RAG). LlamaIndex is a powerful tool that integrates seamlessly with vector databases like Upstash Vector, enabling advanced query and response capabilities.

Install

pip install llama-index upstash-vector llama-index-vector-stores-upstash python-dotenv

Setup

First, create a Vector Index in the Upstash Console. Configure the index with:

  • Dimensions: 1536
  • Distance Metric: Cosine

Once the index is created, copy the UPSTASH_VECTOR_REST_URL and UPSTASH_VECTOR_REST_TOKEN and add them to your .env file along with your OpenAI API key:

UPSTASH_VECTOR_REST_URL=your_upstash_urlUPSTASH_VECTOR_REST_TOKEN=your_upstash_tokenOPENAI_API_KEY=your_openai_api_key

Usage

Here’s how you can integrate LlamaIndex with Upstash Vector:

from llama_index.core import VectorStoreIndex, SimpleDirectoryReaderfrom llama_index.vector_stores.upstash import UpstashVectorStorefrom llama_index.core import StorageContextimport osfrom dotenv import load_dotenv# Load environment variablesload_dotenv()# Set OpenAI API keyopenai.api_key = os.environ["OPENAI_API_KEY"]# Initialize Upstash Vector storeupstash_vector_store = UpstashVectorStore(    url=os.environ["UPSTASH_VECTOR_REST_URL"],    token=os.environ["UPSTASH_VECTOR_REST_TOKEN"],)# Load documents using SimpleDirectoryReaderdocuments = SimpleDirectoryReader("./documents/").load_data()# Create a storage context and initialize the indexstorage_context = StorageContext.from_defaults(vector_store=upstash_vector_store)index = VectorStoreIndex.from_documents(    documents, storage_context=storage_context)

Querying

Once the index is created, you can query it to retrieve and generate responses based on document content.

# Initialize the query enginequery_engine = index.as_query_engine()# Perform queriesresponse_1 = query_engine.query("What is global warming?")print(response_1)response_2 = query_engine.query("How can we reduce our carbon footprint?")print(response_2)

Notes

  • You can specify a namespace when creating the UpstashVectorStore instance:

    vector_store = UpstashVectorStore(    url="your_upstash_url",    token="your_upstash_token",    namespace="your_namespace")
  • Visit the LlamaIndex documentation for more details.

Loading search…