TITLE: Querying Raw Text Data from Upstash Vector DESCRIPTION: This snippet illustrates how to query an Upstash Vector index for similar text data. It shows examples of initializing the index and performing a query with parameters like `data`, `top_k`, and `include_metadata` in Python, JavaScript, Go, PHP, and cURL. SOURCE: https://upstash.com/docs/vector/features/embeddingmodels.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.query( data="What is Upstash?", top_k=1, include_metadata=True, ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.query({ data: "What is Upstash?", topK: 1, includeMetadata: true, }) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex("UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN") index.QueryData(vector.QueryData{ Data: "What is Upstash?", TopK: 1, IncludeMetadata: true, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataQuery; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->queryData(new DataQuery( data: 'What is Upstash?', topK: 1, includeMetadata: true, )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query-data \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"data": "What is Upstash?", "topK": 1, "includeMetadata": "true"}' ``` ---------------------------------------- TITLE: Integrate LangChain with Upstash Vector for Semantic Search DESCRIPTION: This Python code demonstrates how to initialize an `UpstashVectorStore` instance, load sample documents, add them to the Upstash Vector index, and perform a similarity search. It uses `dotenv` for secure credential loading and `langchain_community` for vector store integration. SOURCE: https://upstash.com/docs/vector/integrations/langchain.mdx LANGUAGE: python CODE: ``` from dotenv import load_dotenv from langchain_community.vectorstores.upstash import UpstashVectorStore from langchain.schema import Document # Load environment variables load_dotenv() # Create a vector store instance store = UpstashVectorStore( embedding=True # Embedding option enabled ) # Sample documents to upload documents = [ Document(page_content="Upstash Vector is a scalable vector database."), Document(page_content="LangChain is a framework for building intelligent apps."), Document(page_content="Semantic search enables advanced query matching."), ] # Add documents to the Upstash Vector index store.add_documents(documents) # Perform a similarity search query = "What is LangChain?" results = store.similarity_search(query, k=3) print("Similarity Search Results:") for res in results: print(res.page_content) ``` ---------------------------------------- TITLE: Implement Chat API Route with Vercel AI SDK and Tools DESCRIPTION: Defines the backend API route (`/api/chat`) for the chatbot, handling conversation state, AI model selection (GPT-4o), system instructions, and integrating custom tools for resource creation (`addResource`) and content retrieval (`getInformation`) using Upstash Vector. It uses `streamText` for streaming responses. SOURCE: https://upstash.com/docs/vector/integrations/ai-sdk.mdx LANGUAGE: typescript CODE: ``` import { openai } from '@ai-sdk/openai' import { streamText, tool } from 'ai' import { z } from 'zod' // Tools import { createResource } from '@/lib/actions/resources' import { findRelevantContent } from '@/lib/ai/upstashVector' // Allow streaming responses up to 30 seconds export const maxDuration = 30 export async function POST(req: Request) { const { messages } = await req.json() const result = streamText({ // 1. Choose your AI model model: openai('gpt-4o'), // 2. Pass along the conversation messages from the user messages, // 3. Prompt the model system: `You are a helpful RAG assistant. You have the ability to add and retrieve content from your knowledge base. Only respond to the user with information found in your knowledge base. If no relevant information is found, respond with: "Sorry, I don't know."`, // 4. Provide your "tools": resource creation & retrieving content tools: { addResource: tool({ description: `Add new content to the knowledge base.`, parameters: z.object({ content: z.string().describe('The content to embed and store'), }), execute: async ({ content }) => { const msg = await createResource({ content }) return msg }, }), getInformation: tool({ description: `Retrieve relevant knowledge from your knowledge base to answer user queries.`, parameters: z.object({ question: z.string().describe('The question to search for'), }), execute: async ({ question }) => { const hits = await findRelevantContent(question) // Return array of metadata for each chunk // e.g. [{ id, score, metadata: { resourceId, content }}, ... ] return hits }, }), }, }) // 5. Return the streaming response return result.toDataStreamResponse() } ``` ---------------------------------------- TITLE: Initialize Upstash Vector Index Client from Environment Variables DESCRIPTION: Shows how to initialize the Upstash Vector `Index` client by automatically loading credentials (URL and token) from environment variables. This approach is recommended for secure and flexible credential management, especially in production environments. SOURCE: https://upstash.com/docs/vector/sdks/py/gettingstarted.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index.from_env() ``` ---------------------------------------- TITLE: Perform Semantic Search with Upstash Vector in Python DESCRIPTION: This comprehensive Python script demonstrates the full workflow for semantic search using `upstash-vector`. It initializes the index, defines example documents, clears existing data with `index.reset()`, upserts new documents (triggering automatic embedding generation), and finally queries the index to retrieve semantically similar results. It relies on environment variables for connection details. SOURCE: https://upstash.com/docs/vector/tutorials/semantic_search.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index from dotenv import load_dotenv import time # Load environment variables from a .env file load_dotenv() # Initialize the index from environment variables (URL and token) index = Index.from_env() # Example documents to be indexed documents = [ {"id": "1", "text": "Python is a popular programming language."}, {"id": "2", "text": "Machine learning enables computers to learn from data."}, {"id": "3", "text": "Upstash provides low-latency database solutions."}, {"id": "4", "text": "Semantic search is a technique for understanding the meaning of queries."}, {"id": "5", "text": "Cloud computing allows for scalable and flexible resource management."} ] # Reset the index to remove previous data index.reset() # Upsert documents into Upstash (embeddings are generated automatically) for doc in documents: index.upsert( vectors=[ (doc["id"], doc["text"], {"text": doc["text"]}) ] ) print(f"Document {doc['id']} inserted.") # Wait for the documents to be indexed time.sleep(1) # Search for documents similar to the query query = "What is Python?" results = index.query(data=query, top_k=3, include_metadata=True) # Display search results print("Search Results:") for result in results: print(f"ID: {result.id}") print(f"Score: {result.score:.4f}") print(f"Metadata: {result.metadata}") print("-" * 40) # Separator line between results ``` ---------------------------------------- TITLE: Query Upstash Vector Dense Index DESCRIPTION: Demonstrates how to query a dense vector index using the Upstash Vector SDK's `query()` method. It shows examples for both direct index querying and querying within a specific namespace, specifying parameters like `vector`, `topK`, `includeMetadata`, `includeVectors`, `includeData`, and `filter`. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/query.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; $index = new Index( url: "", token: "", ); $results = $index->query(new VectorQuery( vector: [0.1, 0.2, ...], // "..." represents the dimension size of your vector index. topK: 15, // topK is the limit number of records we want to be returned. includeMetadata: true, // (optional) if true the query results will contain metadata. includeVectors: true, // (optional) if true the query results will contain the indexed vectors. includeData: true, // (optional) if true the query results will contain the string data. filter: '', // (optional) if set, the query results will be filtered by the given filter. )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; $index = new Index( url: "", token: "", ); $results = $index->namespace('my-namespace')->query(new VectorQuery( vector: [0.1, 0.2, ...], // "..." represents the dimension size of your vector index. topK: 15, // topK is the limit number of records we want to be returned. includeMetadata: true, // (optional) if true the query results will contain metadata. includeVectors: true, // (optional) if true the query results will contain the indexed vectors. includeData: true, // (optional) if true the query results will contain the string data. filter: '', // (optional) if set, the query results will be filtered by the given filter. )); ``` ---------------------------------------- TITLE: Initialize Upstash Vector Store with LangChain DESCRIPTION: This Python snippet loads environment variables from the .env file and initializes an UpstashVectorStore instance. By setting 'embedding=True', the store is configured to automatically generate embeddings for documents using Upstash's capabilities. SOURCE: https://upstash.com/docs/vector/tutorials/langchain.mdx LANGUAGE: python CODE: ``` from dotenv import load_dotenv from langchain_community.vectorstores.upstash import UpstashVectorStore load_dotenv() # Create a vector store instance where embeddings are generated by Upstash store = UpstashVectorStore(embedding=True) ``` ---------------------------------------- TITLE: Index and Query Parsed Content with Upstash Vector DESCRIPTION: This Python example shows how to set up an Upstash Vector store, load environment variables, index the parsed documents, and then perform a semantic query against the indexed content. SOURCE: https://upstash.com/docs/vector/integrations/llamaparse.mdx LANGUAGE: python CODE: ``` from llama_index.core import VectorStoreIndex from llama_index.vector_stores.upstash import UpstashVectorStore from llama_index.core import StorageContext from dotenv import load_dotenv import os # Load environment variables load_dotenv() # Set up Upstash Vector Store vector_store = UpstashVectorStore( url=os.getenv("UPSTASH_VECTOR_REST_URL"), token=os.getenv("UPSTASH_VECTOR_REST_TOKEN") ) # Create storage context and index the parsed document storage_context = StorageContext.from_defaults(vector_store=vector_store) index = VectorStoreIndex.from_documents(documents, storage_context=storage_context) # Perform a query query_engine = index.as_query_engine() response = query_engine.query("What is the main topic discussed in the document?") ``` ---------------------------------------- TITLE: Perform Similarity Search on Upstash Vector Index DESCRIPTION: This code snippet demonstrates how to perform a similarity search (query) on an Upstash Vector index. It requires providing a query vector whose dimension must match the index's dimension. Metadata filtering can also be applied. Note that Upstash is eventually consistent, so there might be a delay before newly inserted or updated vectors are available for querying. SOURCE: https://upstash.com/docs/vector/overall/getstarted.mdx LANGUAGE: Python CODE: ``` from upstash_vector import Index index = Index(url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN") index.query( vector=[0.6, 0.8], top_k=3, include_metadata=True, ) ``` LANGUAGE: JavaScript CODE: ``` import { Index } from "@upstash/vector"; const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.query({ vector: [0.6, 0.8], topK: 3, includeMetadata: true }) ``` LANGUAGE: Go CODE: ``` import "github.com/upstash/vector-go" func main() { index := vector.NewIndex("UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN") index.Query(vector.Query{ Vector: []float32{0.6, 0.8}, TopK: 3, IncludeMetadata: true, }) } ``` LANGUAGE: PHP CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; $index = new Index( url: '', token: '', ); $index->query(new VectorQuery( vector: [0.6, 0.8], topK: 3, includeMetadata: true, )); ``` LANGUAGE: Shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"vector": [0.6, 0.8], "topK": 3, "includeMetadata": "true"}' ``` ---------------------------------------- TITLE: Initialize Upstash Vector Index Client with Direct Credentials DESCRIPTION: Demonstrates how to initialize the Upstash Vector `Index` client by directly providing the REST URL and token obtained from the Upstash console. This method is suitable when credentials are explicitly available. SOURCE: https://upstash.com/docs/vector/sdks/py/gettingstarted.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index(url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN") ``` ---------------------------------------- TITLE: Configure Upstash Vector Environment Variables DESCRIPTION: Add the Upstash Vector REST URL and Token to your .env file. These variables are crucial for connecting your application to the Upstash Vector service. Replace the placeholder values with your actual credentials. SOURCE: https://upstash.com/docs/vector/tutorials/langchain.mdx LANGUAGE: bash CODE: ``` UPSTASH_VECTOR_REST_URL=your_upstash_url UPSTASH_VECTOR_REST_TOKEN=your_upstash_token ``` ---------------------------------------- TITLE: Insert Data into Upstash Vector Index DESCRIPTION: This snippet demonstrates how to insert (upsert) a vector into an Upstash Vector index. It shows initialization of the index client with URL and token, and then calls the `upsert` method with a vector ID, numerical vector data, and optional metadata. This operation adds new vectors or updates existing ones. SOURCE: https://upstash.com/docs/vector/overall/getstarted.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index(url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN") index.upsert( vectors=[ ("1", [0.6, 0.8], {"field": "value"}), ] ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector"; const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.upsert({ id: "1", vector: [0.6, 0.8], metadata: {field: "value"} }) ``` LANGUAGE: go CODE: ``` import "github.com/upstash/vector-go" func main() { index := vector.NewIndex("UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN") index.Upsert(vector.Upsert{ Id: "1", Vector: []float32{0.6, 0.8}, Metadata: map[string]any{"field": "value"}, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->upsert(new VectorUpsert( id: '1', vector: [0.6, 0.8], metadata: ['field' => 'value'], )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"id": "1", "vector": [0.6, 0.8], "metadata": {"field": "value"}}' ``` ---------------------------------------- TITLE: Upsert Vector with Metadata into Upstash Vector Index DESCRIPTION: A comprehensive example demonstrating how to initialize the Upstash Vector client from environment variables and then upsert a randomly generated vector along with associated metadata into the index. This snippet illustrates the core functionality of adding data to the vector database, including defining vector dimensions and attaching custom metadata. SOURCE: https://upstash.com/docs/vector/sdks/py/gettingstarted.mdx LANGUAGE: python CODE: ``` import random from upstash_vector import Index # Initialize the index client using environment variables index = Index.from_env() def main(): # Define the dimension based on the index configuration dimension = 128 # Generate a random vector for upsert vector_to_upsert = [random.random() for _ in range(dimension)] # Additional metadata associated with the vector metadata = {"text": "example test for metadata"} # Upsert the vector into the index index.upsert(vectors=[ ("id-for-vector", vector_to_upsert, metadata) ]) ``` ---------------------------------------- TITLE: Upserting and Querying with Upstash Hosted Embeddings DESCRIPTION: This code demonstrates how to use Upstash Vector's hosted embedding models. It configures the Upstash client, defines a `generateChunks` utility, and provides `upsertEmbedding` and `findRelevantContent` functions. Data is upserted using the `data` field, allowing Upstash to handle embedding generation. SOURCE: https://upstash.com/docs/vector/integrations/ai-sdk.mdx LANGUAGE: typescript CODE: ``` import { Index } from '@upstash/vector' // Configure Upstash Vector client // Make sure UPSTASH_VECTOR_REST_URL and UPSTASH_VECTOR_REST_TOKEN are in your .env const index = new Index({ url: process.env.UPSTASH_VECTOR_REST_URL!, token: process.env.UPSTASH_VECTOR_REST_TOKEN!, }) // Chunking logic: split on period function generateChunks(input: string): string[] { return input .trim() .split('.') .filter(i => i !== '') } // Upsert export async function upsertEmbedding(resourceId: string, content: string) { const chunks = generateChunks(content) // Convert each chunk into an Upstash upsert object const toUpsert = chunks.map((chunk, i) => ({ id: `${resourceId}-${i}`, data: chunk, // Using the data field instead of vector because embeddings are generated by Upstash metadata: { resourceId, content: chunk, // Store the chunk as metadata to use during response generation }, })) await index.upsert(toUpsert) } // Query export async function findRelevantContent(query: string, k = 4) { const result = await index.query({ data: query, // Again, using the data field instead of vector field topK: k, includeMetadata: true, // Fetch metadata as well }) return result } ``` ---------------------------------------- TITLE: Initialize Upstash Vector Client using Environment Variables DESCRIPTION: Example of initializing the Upstash Vector client when configuration is provided via environment variables. Imports Index from @upstash/vector. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: typescript CODE: ``` import { Index } from "@upstash/vector"; const index = new Index(); ``` ---------------------------------------- TITLE: Configure Environment Variables for API Keys and Upstash DESCRIPTION: Sets up the necessary environment variables in the .env file. This includes OPENAI_API_KEY for authenticating with OpenAI, and UPSTASH_VECTOR_REST_URL and UPSTASH_VECTOR_REST_TOKEN for connecting to the Upstash Vector database. These credentials are vital for the application's functionality. SOURCE: https://upstash.com/docs/vector/integrations/ai-sdk.mdx LANGUAGE: bash CODE: ``` OPENAI_API_KEY=your_openai_api_key UPSTASH_VECTOR_REST_URL=your_upstash_url UPSTASH_VECTOR_REST_TOKEN=your_upstash_token ``` ---------------------------------------- TITLE: Implement Chat UI with Vercel AI SDK useChat Hook DESCRIPTION: Implements the frontend chat interface using React and the Vercel AI SDK's `useChat` hook. This hook manages message state, input handling, and submission to the `/api/chat` endpoint. The UI renders conversation history, including indicators for tool invocations. SOURCE: https://upstash.com/docs/vector/integrations/ai-sdk.mdx LANGUAGE: typescript CODE: ``` 'use client' import { useChat } from 'ai/react' export default function Home() { // This hook handles message state + streaming from /api/chat const { messages, input, handleInputChange, handleSubmit } = useChat({ // You can enable multi-step calls if you want the model to call multiple tools in one session maxSteps: 3, }) return (

RAG Chatbot with Upstash Vector

{/* Render messages */}
{messages.map(m => (
{m.role}:
{/* If the model calls a tool, show which tool it called */} {m.content.length > 0 ? ( m.content ) : ( calling tool: {m?.toolInvocations?.[0]?.toolName} )}
))}
{/* Text input */}
) } ``` ---------------------------------------- TITLE: Patch Vector Metadata with JSON Merge Patch in Python DESCRIPTION: This Python snippet illustrates how to perform a partial update (patch) on a vector's metadata using the JSON Merge Patch algorithm. It shows how to modify existing fields, delete fields by setting their value to `None`, and add new fields within the metadata dictionary. The `metadata_update_mode` parameter is set to `MetadataUpdateMode.PATCH` to enable this behavior. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/update.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index from upstash_vector.types import MetadataUpdateMode index = Index.from_env() updated = index.update( id="id2", metadata={ "existing-field": "new-value", "existing-field-to-delete": None, "new-field": "new-value", }, metadata_update_mode=MetadataUpdateMode.PATCH, ) print(updated) ``` ---------------------------------------- TITLE: Query Indexed Documents with LlamaIndex DESCRIPTION: Initialize a query engine from the created LlamaIndex and perform two sample queries against the indexed document to retrieve information about global warming and dietary changes. SOURCE: https://upstash.com/docs/vector/tutorials/llamaindex.mdx LANGUAGE: python CODE: ``` # Initialize the query engine query_engine = index.as_query_engine() # Query the document about global warming res1 = query_engine.query("What is global warming?") print(res1) res2 = query_engine.query("How should we modify our diets to reduce our carbon footprint?") print(res2) ``` ---------------------------------------- TITLE: Query LlamaIndex for Retrieval-Augmented Generation DESCRIPTION: This Python snippet shows how to initialize a query engine from the created LlamaIndex and perform example queries to retrieve and generate responses based on the indexed document content. SOURCE: https://upstash.com/docs/vector/integrations/llamaindex.mdx LANGUAGE: python CODE: ``` # Initialize the query engine query_engine = index.as_query_engine() # Perform queries response_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) ``` ---------------------------------------- TITLE: Initialize Hugging Face Embeddings and Upstash Vector Store DESCRIPTION: This Python code loads environment variables, initializes the `HuggingFaceEmbeddings` model with a specified model name, and sets up the `UpstashVectorStore` using the configured embeddings. It prepares the necessary components for interacting with Upstash Vector and performing embedding operations. SOURCE: https://upstash.com/docs/vector/tutorials/huggingface-embeddings.mdx LANGUAGE: python CODE: ``` from dotenv import load_dotenv import os load_dotenv() from langchain_huggingface.embeddings import HuggingFaceEmbeddings from langchain_community.vectorstores.upstash import UpstashVectorStore embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") vector_store = UpstashVectorStore(embedding=embeddings) ``` ---------------------------------------- TITLE: Upstash Vector Query Method API Reference DESCRIPTION: Detailed API documentation for the Upstash Vector `query` method, outlining its payload arguments and options for retrieving similar vectors from an index. The query vector's dimension must match the index's. Returned scores are normalized between 0 (lowest similarity) and 1 (highest similarity). SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/query.mdx LANGUAGE: APIDOC CODE: ``` Query Method Arguments: Payload (QueryCommandPayload, required): vector | sparseVector | data (number[] | SparseVector | string, required) Description: The query data/vector to search for. Pass `vector` or `sparseVector` directly, or `data` for Upstash Embedding. topK (number, required) Description: Number of vectors to return, sorted by distance. includeMetadata (boolean, optional) Description: Include vector metadata in response. includeVectors (boolean, optional) Description: Include vectors themselves in response. includeData (boolean, optional) Description: Include the data field in response. filter (string, optional) Description: Metadata filtering for query results. weightingStrategy (WeightingStrategy, optional) Description: Weighting strategy for sparse vectors. (Default: no weighting) fusionAlgorithm (FusionAlgorithm, optional) Description: Fusion algorithm for hybrid index scores. (Default: RRF) queryMode (QueryMode, optional) Description: Query mode for hybrid indexes. (Default: HYBRID) Options (QueryCommandOptions, optional): Namespace (string, optional) Description: Namespace to query. (Default: default namespace) ``` ---------------------------------------- TITLE: Add Documents to Upstash Vector Store DESCRIPTION: This code adds the pre-processed document chunks (`docs`) into the Upstash Vector index. The `add_documents` method handles the embedding and storage of the text data, making it searchable. SOURCE: https://upstash.com/docs/vector/tutorials/langchain.mdx LANGUAGE: python CODE: ``` inserted_vectors = store.add_documents(docs) ``` ---------------------------------------- TITLE: Complete Resumable Query Workflow Example (Python) DESCRIPTION: A comprehensive end-to-end example demonstrating the full lifecycle of a resumable query, including index creation, vector upsertion, query initialization, fetching initial results, processing, and proper query termination. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/resumable-query.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index # Create an index instance index = Index() # Upsert vectors into the index index.upsert( vectors=[ ("id1", [0.1, 0.2], {"field": "value1"}), ("id2", [0.3, 0.4], {"field": "value2"}), ("id3", [0.5, 0.6], {"field": "value3"}) ], namespace="example-namespace" ) # Start a resumable query query = index.resumable_query( vector=[0.1, 0.2], top_k=2, include_metadata=True, include_vectors=True, namespace="example-namespace" ) # Fetch initial results results = query.start() # Access result data for result in results: print(f"ID: {result.id}, Metadata: {result.metadata}") # Stop the query when done query.stop() ``` ---------------------------------------- TITLE: Perform Resumable Query with Vector Input DESCRIPTION: Demonstrates how to initiate a resumable query using a vector as input. It shows how to retrieve initial results, fetch additional batches of results using `fetchNext`, and properly stop the query to release resources. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/resumable-query.mdx LANGUAGE: typescript CODE: ``` const { result, fetchNext, stop } = await index.resumableQuery({ maxIdle: 3600, topK: 50, vector: [0, 1, 2, ..., 383], // 384-dimensional vector includeMetadata: true, includeVectors: true, }); console.log(result); /* [ { id: '6345', score: 1.00000012, vector: [0, 1, 2, ..., 383], metadata: { sentence: "Upstash is great." } }, // ... more results ] */ const nextBatch = await fetchNext(5); // Fetch next 5 results console.log(nextBatch); await stop(); // Stop the resumable query ``` ---------------------------------------- TITLE: Query Upstash Vector Index Using Data String DESCRIPTION: Illustrates querying an Upstash Vector index by providing a data string, allowing the index to generate the embedding internally. This example includes options to retrieve vectors and metadata, set a `topK` limit, and apply a filter expression to narrow down results. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/query.mdx LANGUAGE: typescript CODE: ``` const results = await index.query({ data: "Movie about an adventure of a hobbit in a fantasy world.", includeVectors: true, includeMetadata: true, topK: 1, filter: "genre = 'fantasy' and title = 'Lord of the Rings'", }); /* [ { id: "1234", vector: [0.1, 0.2, 0.3, 0.4, 0.5], score: 0.9999999, metadata: { title: "Lord of The Rings", genre: "fantasy", category: "classic", }, } ] */ ``` ---------------------------------------- TITLE: Define Index-Level Metadata Types for Upstash Vector DESCRIPTION: Illustrates how to define a metadata type at the `Index` level using TypeScript generics. This approach provides strong type safety for metadata across various operations such as `query`, `upsert`, `fetch`, and `range`, ensuring consistency and reducing errors. SOURCE: https://upstash.com/docs/vector/sdks/ts/getting-started.mdx LANGUAGE: typescript CODE: ``` import { Index } from "@upstash/vector"; type Metadata = { genre: string, year: number }; const index = new Index(); ``` ---------------------------------------- TITLE: Upsert Vectors with Data and Metadata DESCRIPTION: Demonstrates how to upsert vectors, including both structured metadata and unstructured data fields. This allows associating contextual information directly with vector embeddings. The example shows how to set both fields or just the 'data' field during the upsert operation. SOURCE: https://upstash.com/docs/vector/features/metadata.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.upsert( [ { "id": "id-0", "vector": [0.9215, 0.3897], "metadata": {"url": "https://imgur.com/z9AVZLb"}, "data": "data-0", }, { "id": "id-1", "vector": [0.3897, 0.9215], "data": "data-1", }, ], ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.upsert([ { id: "id-0", vector: [0.9215, 0.3897], metadata: {"url": "https://imgur.com/z9AVZLb"}, data: "data-0", }, { id: "id-1", vector: [0.3897, 0.9215], data: "data-1", }, ]) ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->upsertMany([ new VectorUpsert( id: 'id-0', vector: [0.9215, 0.3897], data: 'data-0', ), new VectorUpsert( id: 'id-1', vector: [0.3897, 0.9215], data: 'data-1', ), ]); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '[\n {\n "id": "id-0",\n "vector": [0.9215, 0.3897],\n "metadata": {"url": "https://imgur.com/z9AVZLb"},\n "data": "data-0"\n },\n {\n "id": "id-1",\n "vector": [0.3897, 0.9215],\n "data": "data-1"\n }\n ]' ``` ---------------------------------------- TITLE: Perform Resumable Query with Custom Metadata Type and Filtering DESCRIPTION: Shows how to define and use a custom metadata type with a resumable query. It demonstrates filtering results based on specific metadata fields and accessing strongly-typed metadata from the query results without additional typecasting. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/resumable-query.mdx LANGUAGE: typescript CODE: ``` type Metadata = { title: string, genre: 'sci-fi' | 'fantasy' | 'horror' | 'action' } const { result, fetchNext, stop } = await index.resumableQuery({ vector: [ ... // query embedding ], includeMetadata: true, topK: 1, filter: "genre = 'fantasy' and title = 'Lord of the Rings'", maxIdle: 3600, }) if (result[0].metadata) { // Since we passed the Metadata type parameter above, // we can interact with metadata fields without having to // do any typecasting. const { title, genre } = result[0].metadata; console.log(`The best match in fantasy was ${title}`) } await stop(); ``` ---------------------------------------- TITLE: Add Sample Documents to Upstash Vector DESCRIPTION: This Python snippet defines sample text documents, converts them into LangChain `Document` objects, and then adds them to the `UpstashVectorStore`. Documents are embedded using the initialized Hugging Face model and stored in batches, with parameters controlling embedding and vector batch sizes for efficient processing. SOURCE: https://upstash.com/docs/vector/tutorials/huggingface-embeddings.mdx LANGUAGE: python CODE: ``` from langchain.schema import Document documents = [ Document(page_content="Global warming is causing sea levels to rise."), Document(page_content="Artificial intelligence is transforming many industries."), Document(page_content="Renewable energy is vital for sustainable development.") ] vector_store.add_documents( documents=documents, batch_size=100, embedding_chunk_size=200 ) print("Documents with embeddings have been stored in Upstash Vector.") ``` ---------------------------------------- TITLE: Upsert Single Data String for Embedding in TypeScript DESCRIPTION: Shows how to upsert a single string of data, which will be embedded by Upstash, along with an ID and optional metadata. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/upsert.mdx LANGUAGE: typescript CODE: ``` await index.upsert({ id: "1234", data: "'The Lord of the Rings' follows Frodo Baggins and his allies on a quest to destroy a powerful ring and save Middle-earth from the dark lord Sauron.", metadata: { title: "Lord of The Rings", genre: "drama", category: "classic", }, }); ``` ---------------------------------------- TITLE: Install Required Python Libraries for Upstash Vector DESCRIPTION: This command installs the `upstash-vector` library, which provides the client for interacting with the Upstash Vector database, and `python-dotenv`, used for loading environment variables from a `.env` file. Both are crucial dependencies for running the semantic search application. SOURCE: https://upstash.com/docs/vector/tutorials/semantic_search.mdx LANGUAGE: bash CODE: ``` pip install upstash-vector python-dotenv ``` ---------------------------------------- TITLE: Initialize Upstash Vector Client from Environment Variables (PHP) DESCRIPTION: This PHP code snippet demonstrates how to initialize the Upstash Vector client by automatically picking up configuration from environment variables. It uses the `Index::fromEnv()` static method, simplifying setup when credentials are globally configured. SOURCE: https://upstash.com/docs/vector/sdks/php/getting-started.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; $index = Index::fromEnv(); ``` ---------------------------------------- TITLE: Install Python Dependencies for RAG Application DESCRIPTION: This command installs all necessary Python libraries for building the RAG application. It includes `gradio` for the user interface, `langchain` for orchestration, `sentence_transformers` and `transformers` for Hugging Face models, `upstash-vector` for the vector database, and `python-dotenv` for environment variable management. SOURCE: https://upstash.com/docs/vector/tutorials/gradio-application.mdx LANGUAGE: bash CODE: ``` pip install gradio langchain sentence_transformers upstash-vector python-dotenv transformers langchain-community langchain-huggingface ``` ---------------------------------------- TITLE: Install Upstash Vector PHP SDK with Composer DESCRIPTION: This command installs the `upstash/vector` PHP SDK using Composer, the dependency manager for PHP. It's the recommended way to add the SDK to your project. SOURCE: https://upstash.com/docs/vector/sdks/php/getting-started.mdx LANGUAGE: shell CODE: ``` composer require upstash/vector ``` ---------------------------------------- TITLE: Query Vectors and Retrieve Metadata in Upstash Vector DESCRIPTION: This snippet shows how to query the Upstash Vector index for similar vectors and explicitly request the inclusion of associated metadata in the query results. The `include_metadata` (or equivalent) parameter ensures that the stored context is returned alongside the vector ID and score. SOURCE: https://upstash.com/docs/vector/features/metadata.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.query( [0.9215, 0.3897], top_k=5, include_metadata=True, ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.query({ vector: [0.9215, 0.3897], topK: 5, includeMetadata: true, }) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex("UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN") index.Query(vector.Query{ Vector: []float32{0.9215, 0.3897}, TopK: 5, IncludeMetadata: true, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->query(new VectorQuery( vector: [0.9215, 0.3897], topK: 5, includeMetadata: true, )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "vector":[0.9215,0.3897], "topK" : 5, "includeMetadata": true }' ``` ---------------------------------------- TITLE: Specify Namespace for Upstash Vector Store DESCRIPTION: This example demonstrates how to configure a specific namespace when initializing the Upstash Vector store, allowing for data isolation and organization within the database. SOURCE: https://upstash.com/docs/vector/integrations/llamaindex.mdx LANGUAGE: python CODE: ``` vector_store = UpstashVectorStore( url="your_upstash_url", token="your_upstash_token", namespace="your_namespace" ) ``` ---------------------------------------- TITLE: APIDOC: `update` Method for Upstash Vector Index DESCRIPTION: Comprehensive documentation for the `update` method of the `Index` class in Upstash Vector. This method allows for modifying an existing vector's data, metadata, or the vector embedding itself, identified by its ID. It supports various parameters for granular control over the update process, including specific metadata update modes and namespace targeting. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/update.mdx LANGUAGE: APIDOC CODE: ``` UpstashVector.Index.update(id: str, vector: Optional[list[float]] = None, metadata: Optional[dict] = None, data: Optional[str] = None, metadata_update_mode: Optional[MetadataUpdateMode] = None, namespace: Optional[str] = None) -> bool Description: Updates the vector, metadata, or data of an existing vector by its ID. Parameters: id: The ID of the vector to update. vector: The new vector data (list of floats). metadata: The new metadata dictionary. Can be used with `metadata_update_mode` for patching. data: The new data string. metadata_update_mode: Specifies how metadata updates are applied (e.g., MetadataUpdateMode.PATCH for JSON Merge Patch). namespace: The namespace in which the vector resides. If not provided, the default namespace is used. Returns: bool: True if the update was successful. ``` ---------------------------------------- TITLE: Upstash Vector Index Range Method API Reference DESCRIPTION: Documents the `range` method of the Upstash Vector Index, detailing its input parameters (`cursor`, `prefix`, `limit`, `include_vectors`, `include_metadata`, `include_data`) and the structure of its response object, including `next_cursor` and a list of `vectors` with their `id`, `vector`, `metadata`, and `data`. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/range.mdx LANGUAGE: APIDOC CODE: ``` Method: range Description: Retrieves vectors from the index within a specified range. Parameters: cursor: string - A cursor to start the range query. prefix: string - A string prefix to match vector IDs. limit: number - The maximum number of vectors to retrieve in a single query. include_vectors: boolean - Whether to include vectors in the range results. include_metadata: boolean - Whether to include metadata in the range results. include_data: boolean - Whether to include data in the range results. Returns: object next_cursor: string - A cursor indicating the position to start the next range query. Empty if no more results. vectors: array - List containing information for each vector. id: string vector: array (float) metadata: object data: object ``` ---------------------------------------- TITLE: Query Vectors and Retrieve Associated Data DESCRIPTION: Shows how to perform a query using a data string as the query input and retrieve the associated 'data' field for matching vectors. By setting `include_data` to true, the original contextual information stored with the vector is returned in the query results. SOURCE: https://upstash.com/docs/vector/features/metadata.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) result = index.query( data="What is Upstash?", include_data=True, ) for res in result: print(f"{res.id}: {res.data}") ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) const result = await index.query({ data: "What is Upstash?", includeData: true, topK: 3 }) for (const vector of result) { console.log(`${vector.id}: ${vector.data}`) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataQuery; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $results = $index->queryData(new DataQuery( data: 'Upstash is a serverless data platform.', topK: 3 includeData: true, )); foreach ($results as $result) { print_r($result->toArray()); } ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query-data \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{\n "data": "What is Upstash?",\n "includeData": true\n }' ``` ---------------------------------------- TITLE: Upsert Multiple Data Strings for Embedding in TypeScript DESCRIPTION: Illustrates upserting an array of data strings, each to be embedded by Upstash, with their respective IDs and optional metadata. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/upsert.mdx LANGUAGE: typescript CODE: ``` await index.upsert([ { id: "6789", data: "'Harry Potter' follows the journey of a young wizard, Harry Potter, as he attends Hogwarts School of Witchcraft and Wizardry, forms deep friendships, and confronts the dark wizard Voldemort, who seeks immortality and domination over the magical world.", }, { id: "1234", data: "'The Lord of the Rings' follows Frodo Baggins and his allies on a quest to destroy a powerful ring and save Middle-earth from the dark lord Sauron.", metadata: { title: "Lord of The Rings", genre: "drama", category: "classic", }, }, ]); ``` ---------------------------------------- TITLE: Upserting and Querying with Custom Embedding Models (OpenAI) DESCRIPTION: This section illustrates how to use a custom embedding model, specifically OpenAI's `text-embedding-ada-002`, with Upstash Vector. It includes functions to generate single and multiple embeddings using `@ai-sdk/openai` and then upserts these pre-generated embeddings using the `vector` field. SOURCE: https://upstash.com/docs/vector/integrations/ai-sdk.mdx LANGUAGE: typescript CODE: ``` import { Index } from '@upstash/vector' import { embed, embedMany } from 'ai' import { openai } from '@ai-sdk/openai' // Configure Upstash Vector client const index = new Index({ url: process.env.UPSTASH_VECTOR_REST_URL!, token: process.env.UPSTASH_VECTOR_REST_TOKEN!, }) // Chunking logic: split on period function generateChunks(input: string): string[] { return input .trim() .split('.') .filter(i => i !== '') } // Define the embedding model const embeddingModel = openai.embedding('text-embedding-ada-002') // Function to generate a single embedding async function generateEmbedding(value: string): Promise { const input = value.replaceAll('\n', ' ') const { embedding } = await embed({ model: embeddingModel, value: input, }) return embedding } // Function to generate embeddings for multiple chunks async function generateEmbeddings( value: string, ): Promise> { const chunks = generateChunks(value) const { embeddings } = await embedMany({ model: embeddingModel, values: chunks, }) return embeddings.map((vector, i) => ({ content: chunks[i], embedding: vector, })) } // Upsert export async function upsertEmbeddings(resourceId: string, content: string) { // Generate embeddings for each chunk const chunkEmbeddings = await generateEmbeddings(content) // Convert each chunk into an Upstash upsert object const toUpsert = chunkEmbeddings.map((chunk, i) => ({ id: `${resourceId}-${i}`, // e.g. "abc123-0" vector: chunk.embedding, metadata: { resourceId, content: chunk.content, }, })) await index.upsert(toUpsert) } // Query export async function findRelevantContent(query: string, k = 4) { const userEmbedding = await generateEmbedding(query) const result = await index.query({ vector: userEmbedding, topK: k, includeMetadata: true, }) return result } ``` ---------------------------------------- TITLE: Apply IDF Weighting Strategy in Upstash Vector Queries DESCRIPTION: This snippet demonstrates how to apply Inverse Document Frequency (IDF) as a weighting strategy when performing queries against an Upstash Vector index. It includes the mathematical formula for IDF and practical examples in Python, JavaScript, Go, PHP, and cURL, showing how to initialize the index and pass the IDF weighting strategy to the query method. This helps in making rare terms more significant in search results. SOURCE: https://upstash.com/docs/vector/features/sparseindexes.mdx LANGUAGE: Formula CODE: ``` IDF(qᵢ) = log((N - n(qᵢ) + 0.5) / (n(qᵢ) + 0.5)) ``` LANGUAGE: python CODE: ``` from upstash_vector import Index from upstash_vector.types import WeightingStrategy index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.query( data="Upstash Vector", top_k=5, weighting_strategy=WeightingStrategy.IDF, ) ``` LANGUAGE: javascript CODE: ``` import { Index, WeightingStrategy } from "@upstash/vector"; const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }); await index.query({ sparseVector: { indices: [2, 3], values: [0.13, 0.87], }, weightingStrategy: WeightingStrategy.IDF, topK: 3, }); ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) scores, err := index.QueryData(vector.QueryData{ Data: "Upstash Vector", TopK: 5, WeightingStrategy: vector.WeightingStrategyIDF, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataQuery; use Upstash\Vector\Enums\WeightingStrategy; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->queryData(new DataQuery( data: 'Upstash Vector', topK: 5, weightingStrategy: WeightingStrategy::INVERSE_DOCUMENT_FREQUENCY, )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query-data \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"data": "Upstash Vector", "topK": 5, "weightingStrategy": "IDF"}' ``` ---------------------------------------- TITLE: Implement QA Pipeline and Launch Gradio Interface for RAG App DESCRIPTION: This Python code initializes a Hugging Face question-answering pipeline. It defines `answer_question`, a core function that retrieves relevant documents from Upstash Vector based on a query, uses them as context for the QA model, and returns an answer. Finally, it sets up and launches a Gradio web interface, allowing users to interact with the RAG application. SOURCE: https://upstash.com/docs/vector/tutorials/gradio-application.mdx LANGUAGE: python CODE: ``` # Set up a Hugging Face Question Answering model qa_pipeline = pipeline("question-answering", model="distilbert-base-cased-distilled-squad") # Gradio interface function def answer_question(query): # Retrieve relevant documents from Upstash Vector results = vector_store.similarity_search(query, k=3) # Use the most relevant document for QA if results: context = results[0].page_content qa_input = {"question": query, "context": context} answer = qa_pipeline(qa_input)["answer"] return f"Answer: {answer}\n\nContext: {context}" else: return "No relevant context found." # Set up Gradio interface iface = gr.Interface( fn=answer_question, inputs="text", outputs="text", title="RAG Application", description="Ask a question, and the app will retrieve relevant information and provide an answer." ) # Launch the Gradio app iface.launch() ``` ---------------------------------------- TITLE: Langflow Upstash Vector Integration Component Reference DESCRIPTION: This section details the Langflow components used for integrating with Upstash Vector, outlining their purpose and how they connect within a workflow for document processing, storage, and semantic search. SOURCE: https://upstash.com/docs/vector/integrations/langflow.mdx LANGUAGE: APIDOC CODE: ``` Langflow Components: - File Component: Description: Used for loading documents into the workflow. - Split Component: Description: Used for splitting loaded documents into manageable chunks. - Upstash Component: Description: The core component for interacting with the Upstash Vector index. Capabilities: - Document Processing and Storage: Input: Processed document chunks (from Split component). Output: Stores document embeddings in the Upstash Vector index. Workflow Connection: Connects from 'Split' component. - Vector Search: Input: User query. Output: Relevant context retrieved from the Upstash Vector index. Workflow Connection: Connects to query input. ``` ---------------------------------------- TITLE: Query Parsed Document with LLM and Upstash Vector DESCRIPTION: Loads environment variables, sets up the OpenAI API key, and initializes the `UpstashVectorStore`. It then creates a `StorageContext`, indexes the previously parsed documents, and finally queries the index using an LLM to extract information. SOURCE: https://upstash.com/docs/vector/tutorials/llamaparse.mdx LANGUAGE: python CODE: ``` from llama_index.core import VectorStoreIndex from llama_index.vector_stores.upstash import UpstashVectorStore from llama_index.core import StorageContext import openai # Load environment variables for API keys and Upstash configuration from dotenv import load_dotenv import os load_dotenv() # Set up OpenAI API key openai.api_key = os.getenv("OPENAI_API_KEY") # Set up Upstash Vector Store upstash_vector_store = UpstashVectorStore( url=os.getenv("UPSTASH_VECTOR_REST_URL"), token=os.getenv("UPSTASH_VECTOR_REST_TOKEN"), ) # Create a storage context for Upstash Vector and index the parsed document storage_context = StorageContext.from_defaults(vector_store=upstash_vector_store) index = VectorStoreIndex.from_documents(documents, storage_context=storage_context) # Create a query engine for the index and perform a query query_engine = index.as_query_engine() query = "What are the main points discussed in the document?" response = query_engine.query(query) print(response) ``` ---------------------------------------- TITLE: Upstash Vector Index Query Method API Documentation DESCRIPTION: Detailed API documentation for the `query` method, outlining all supported parameters for vector retrieval and the structure of the returned response object, including fields like ID, metadata, score, and the vector itself. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/query.mdx LANGUAGE: APIDOC CODE: ``` Method: query Parameters: vector: The reference vector for similarity comparison. sparse_vector: The sparse vector value to query. data: A string for text-based queries (mutually exclusive with vector). include_metadata: A boolean flag indicating whether to include metadata in the query results. include_vector: A boolean flag indicating whether to include vectors in the query results. include_data: A boolean flag indicating whether to include data in the query results. top_k: The number of top matching vectors to retrieve. filter: Metadata filtering of the vector is used to query your data based on the filters and narrow down the query results. namespace: The namespace to use. When not specified, the default namespace is used. weighting_strategy: Weighting strategy to be used for sparse vectors. fusion_algorithm: Fusion algorithm to use while fusing scores from hybrid vectors. query_mode: Query mode for hybrid indexes with Upstash-hosted embedding models. Response Fields: id: The identifier associated with the matching vector. metadata: Additional information or attributes linked to the matching vector. score: A measure of similarity indicating how closely the vector matches the query vector. The score is normalized to the range [0, 1], where 1 indicates a perfect match. vector: The vector itself (included only if `include_vector` is set to `True`). sparse_vector: The sparse vector itself (included only if `include_vector` is set to `True`). data: Additional unstructured information linked to the matching vector. ``` ---------------------------------------- TITLE: Filter Vector Query Results by Metadata in Upstash Vector DESCRIPTION: This snippet demonstrates how to refine vector query results by applying a metadata filter. The `filter` parameter allows specifying conditions (e.g., using `GLOB` for pattern matching) on the stored metadata, ensuring only relevant vectors are returned. SOURCE: https://upstash.com/docs/vector/features/metadata.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.query( [0.9215, 0.3897], top_k=5, include_metadata=True, filter="url GLOB '*imgur.com*'", ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.query({ vector: [0.9215, 0.3897], topK: 5, includeMetadata: true, filter: "url GLOB '*imgur.com*'", }) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex("UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN") index.Query(vector.Query{ Vector: []float32{0.9215, 0.3897}, TopK: 5, IncludeMetadata: true, Filter: "url GLOB '*imgur.com*'", }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->query(new VectorQuery( ``` ---------------------------------------- TITLE: Initialize Hugging Face Embeddings and Upstash Vector Store in Python DESCRIPTION: This Python code imports required libraries and loads environment variables. It then initializes a `HuggingFaceEmbeddings` model for generating text embeddings and sets up an `UpstashVectorStore` instance, which will be used to store and retrieve document embeddings. SOURCE: https://upstash.com/docs/vector/tutorials/gradio-application.mdx LANGUAGE: python CODE: ``` # Import libraries import gradio as gr from dotenv import load_dotenv from langchain_huggingface.embeddings import HuggingFaceEmbeddings from langchain_community.vectorstores.upstash import UpstashVectorStore from transformers import pipeline from langchain.schema import Document # Load environment variables load_dotenv() # Set up embeddings and Upstash Vector store embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") vector_store = UpstashVectorStore(embedding=embeddings) ``` ---------------------------------------- TITLE: Upsert Multiple or Single Records into Upstash Vector Index DESCRIPTION: Demonstrates how to upsert both multiple records (as an array) and a single record into an Upstash Vector index. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: typescript CODE: ``` // Upsert multiple records await index.upsert([...]); // Upsert a single record await index.upsert({...}); ``` ---------------------------------------- TITLE: Upsert Single Vector in TypeScript DESCRIPTION: Demonstrates how to add a single vector with an ID, vector data, and optional metadata to the index using the `upsert` method. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/upsert.mdx LANGUAGE: typescript CODE: ``` await index.upsert({ id: "1234", vector: [0.1, 0.2, 0.3, 0.4, 0.5], metadata: { title: "Lord of The Rings", genre: "drama", category: "classic", }, }); ``` ---------------------------------------- TITLE: Perform Semantic Similarity Search DESCRIPTION: This snippet executes a semantic similarity search against the Upstash Vector Store. It queries the index with a natural language phrase and retrieves the top `k` most semantically similar documents, demonstrating the retrieval capabilities of the vector database. SOURCE: https://upstash.com/docs/vector/tutorials/langchain.mdx LANGUAGE: python CODE: ``` result = store.similarity_search("Technology's role in global warming.", k=5) print(result) ``` ---------------------------------------- TITLE: Querying Upstash Vector with Metadata Filters DESCRIPTION: Demonstrates how to perform a vector similarity search with a metadata filter using the Upstash Vector client libraries and curl. The filter uses SQL-like syntax to specify conditions on metadata fields like 'population' and 'geography.continent'. This example retrieves top 5 vectors matching the filter and includes metadata in the response. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.query( vector=[0.9215, 0.3897], filter="population >= 1000000 AND geography.continent = 'Asia'", top_k=5, include_metadata=True ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.query({ vector: [0.9215, 0.3897], filter: "population >= 1000000 AND geography.continent = 'Asia'", topK: 5, includeMetadata: true, }); ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex("UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN") index.Query(vector.Query{ Vector: []float32{0.9215, 0.3897}, Filter: `population >= 1000000 AND geography.continent = 'Asia'`, TopK: 5, IncludeMetadata: true, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->query(new VectorQuery( vector: [0.9215, 0.3897], topK: 5, includeMetadata: true, filter: "population >= 1000000 AND geography.continent = 'Asia'", )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "vector":[0.9215,0.3897], "topK" : 5, "filter": "population >= 1000000 AND geography.continent = \"Asia\"", "includeMetadata": true }' ``` ---------------------------------------- TITLE: Split Documents into Chunks using CharacterTextSplitter DESCRIPTION: This snippet demonstrates how to split a collection of documents into smaller, manageable chunks using LangChain's `CharacterTextSplitter`. It configures the chunk size and overlap to prepare documents for embedding and storage in a vector database. SOURCE: https://upstash.com/docs/vector/tutorials/langchain.mdx LANGUAGE: python CODE: ``` # Split the document into chunks text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0) docs = text_splitter.split_documents(documents) ``` ---------------------------------------- TITLE: Upsert Multiple Vectors in TypeScript DESCRIPTION: Shows how to add an array of vectors, each with an ID, vector data, and optional metadata, to the index in a single `upsert` call. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/upsert.mdx LANGUAGE: typescript CODE: ``` await index.upsert([ { id: "6789", vector: [0.6, 0.7, 0.8, 0.9, 0.9], }, { id: "1234", vector: [0.1, 0.2, 0.3, 0.4, 0.5], metadata: { title: "Lord of The Rings", genre: "drama", category: "classic", }, }, ]); ``` ---------------------------------------- TITLE: Upsert Text Data into Upstash Vector Index DESCRIPTION: This snippet demonstrates how to upsert text data into an Upstash Vector hybrid index. It uses the configured index to automatically embed the text data using Upstash-hosted models. The `id` and `data` fields are required for each vector. SOURCE: https://upstash.com/docs/vector/features/hybridindexes.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index, Vector index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.upsert( vectors=[ Vector(id="id-0", data="Upstash Vector provides dense and sparse embedding models."), Vector(id="id-1", data="You can upsert text data with these embedding models."), ] ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.upsert([ { id: 'id-0', data: "Upstash Vector provides dense and sparse embedding models.", } ]) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) err := index.UpsertData(vector.UpsertData{ Id: "id-0", Data: "Upstash Vector provides dense and sparse embedding models.", }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataUpsert; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->upsertData(new DataUpsert( id: 'id-0', data: 'Upstash Vector provides dense and sparse embedding models.', )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert-data \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '[ {"id": "id-0", "data": "Upstash Vector provides dense and sparse embedding models."}, {"id": "id-1", "data": "You can upsert text data with these embedding models."} ]' ``` ---------------------------------------- TITLE: Upsert Multiple Vectors (cURL) DESCRIPTION: Example cURL command to upsert multiple vectors into the default namespace using a JSON array in the request body. SOURCE: https://upstash.com/docs/vector/api/endpoints/upsert.mdx LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '[ \ { "id": "id-0", "vector": [0.1, 0.2], "metadata": { "link": "upstash.com" } }, \ { "id": "id-1", "vector": [0.2, 0.3] } \ ]' ``` ---------------------------------------- TITLE: Combine Filters with SQL Boolean Operators (AND, OR) DESCRIPTION: Shows how to combine multiple filter conditions using `AND` and `OR` boolean operators to form compound filters. Parentheses can be used to explicitly control operator precedence. When no parentheses are provided in ambiguous filters, `AND` has higher precedence than `OR`. Examples demonstrate basic combination, explicit grouping, and implicit precedence. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` country = 'Turkey' AND population > 10000000 ``` LANGUAGE: SQL CODE: ``` country = 'Turkey' AND (population > 10000000 OR is_capital = false) ``` LANGUAGE: SQL CODE: ``` country = 'Turkey' AND population > 10000000 OR is_capital = false ``` LANGUAGE: SQL CODE: ``` (country = 'Turkey' AND population > 10000000) OR is_capital = false ``` ---------------------------------------- TITLE: Perform Separate Dense and Sparse Queries for Custom Reranking DESCRIPTION: Illustrates how to query dense and sparse components of a hybrid index separately using the Upstash Vector client. This approach allows developers to implement custom reranking logic on the retrieved results, providing flexibility beyond built-in fusion algorithms. SOURCE: https://upstash.com/docs/vector/features/hybridindexes.mdx LANGUAGE: Python CODE: ``` from upstash_vector import Index from upstash_vector.types import SparseVector index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) dense_results = index.query( vector=[0.5, 0.4], ) sparse_results = index.query( sparse_vector=SparseVector([3, 5], [0.3, 0.5]), ) ``` ---------------------------------------- TITLE: Initialize Upstash Vector Store and Index Documents DESCRIPTION: This Python code loads environment variables, initializes the Upstash vector store with provided URL and token, loads documents from a specified directory, and creates a LlamaIndex vector store index using the Upstash store. SOURCE: https://upstash.com/docs/vector/tutorials/llamaindex.mdx LANGUAGE: python CODE: ``` from llama_index.core import VectorStoreIndex, SimpleDirectoryReader from llama_index.vector_stores.upstash import UpstashVectorStore from llama_index.core import StorageContext import openai import os from dotenv import load_dotenv # Load environment variables load_dotenv() openai.api_key = os.environ["OPENAI_API_KEY"] # Setup the Upstash vector store upstash_vector_store = UpstashVectorStore( url=os.environ["UPSTASH_VECTOR_REST_URL"], token=os.environ["UPSTASH_VECTOR_REST_TOKEN"], ) # Read the document about global warming from the documents directory documents = SimpleDirectoryReader("./documents/").load_data() # Initialize the storage context with the Upstash vector store storage_context = StorageContext.from_defaults(vector_store=upstash_vector_store) # Create the index from the loaded document with 1536 dimensions and cosine distance index = VectorStoreIndex.from_documents( documents, storage_context=storage_context ) ``` ---------------------------------------- TITLE: Upsert Raw Text Data for Automatic Embedding DESCRIPTION: Illustrates how to upsert raw text data directly. When raw text is provided in the 'data' field without an explicit vector, the system automatically embeds the text and stores it. This simplifies the process of storing contextual text information. SOURCE: https://upstash.com/docs/vector/features/metadata.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.upsert( [ { "id": "id-2", "data": "Upstash is a serverless data platform.", }, ], ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.upsert([ { id: "id-2", data: "Upstash is a serverless data platform.", } ]) ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataUpsert; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->upsertData(new DataUpsert( id: 'id-0', data: 'Upstash is a serverless data platform.', )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert-data \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{\n "id": "id-0",\n "data": "Upstash is a serverless data platform."\n }' ``` ---------------------------------------- TITLE: Upsert Vector API Reference DESCRIPTION: Detailed API documentation for upserting vectors, including request body parameters, path parameters, and response structures. Supports single or multiple vector upserts. SOURCE: https://upstash.com/docs/vector/api/endpoints/upsert.mdx LANGUAGE: APIDOC CODE: ``` API: POST https://{endpoint}/upsert/{namespace} Authentication: Bearer Token Request Body (JSON Array or Object): - id (string, required): The id of the vector. - vector (number[]): The dense vector value for dense and hybrid indexes. Note: Must have same dimensions as index. - sparseVector (Object[]): The sparse vector value for sparse and hybrid indexes. - indices (number[]): Indices of the non-zero valued dimensions. - values (number[]): Values of the non-zero valued dimensions. - metadata (Object): Metadata for the vector, useful for filtering. - data (string): Unstructured raw text data associated with the vector. Request Body Notes: - For dense indexes: Only 'vector' should be provided. - For sparse indexes: Only 'sparseVector' should be provided. - For hybrid indexes: Both 'vector' and 'sparseVector' must be present. Path Parameters: - namespace (string, optional, default: ""): The namespace to use. Default namespace if not specified. Response (200 OK): - result (string): "Success" string. Response (422 Unprocessable Entity): - error (string): Description of the error (e.g., "Invalid vector dimension: 2, expected: 256"). - status (number): HTTP status code (e.g., 422). ``` ---------------------------------------- TITLE: Perform Upsert and Query Operations within a Specific Namespace DESCRIPTION: This snippet demonstrates how to implicitly create a namespace by performing an upsert operation, and then how to query vectors specifically within that namespace. It initializes an Upstash Vector Index and uses the namespace method or parameter to target operations. SOURCE: https://upstash.com/docs/vector/features/namespaces.mdx LANGUAGE: Python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.upsert( [('id-0', [0.9215, 0.3897])], namespace="ns", ) index.query( [0.9215, 0.3897], top_k=5, namespace="ns", ) ``` LANGUAGE: JavaScript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) const namespace = index.namespace("ns") await namespace.upsert({ id: "id-0", vector: [0.9215, 0.3897], }) await namespace.query({ vector: [0.9215, 0.3897], topK: 5, }) ``` LANGUAGE: Go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex("UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN") namespace := index.Namespace("ns") namespace.Upsert(vector.Upsert{ Id: "id-0", Vector: []float32{0.9215, 0.3897}, }) namespace.Query(vector.Query{ Vector: []float32{0.9215, 0.3897}, TopK: 5, }) } ``` LANGUAGE: PHP CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; use Upstash\Vector\VectorQuery; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $namespace = $index->namespace('ns'); $namespace->upsert(new VectorUpsert( id: 'id-0', vector: [0.9215, 0.3897], )); $namespace->query(new VectorQuery( vector: [0.9215, 0.3897], topK: 5, )); ``` LANGUAGE: curl CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert/ns \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"id":"id-0", "vector":[0.9215,0.3897]}' curl $UPSTASH_VECTOR_REST_URL/query/ns \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"vector":[0.9215,0.3897], "topK" : 5}' ``` ---------------------------------------- TITLE: Query Upstash Vector Index with String Data (PHP) DESCRIPTION: Demonstrates how to query an Upstash Vector index using a simple string. The string is automatically converted into vector embeddings, provided the index is configured with an embedding model. The example shows both direct index interaction and querying within a specific namespace. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/query.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataQuery; $index = new Index( url: "", token: "", ); $results = $index->queryData(new DataQuery( data: 'What is the capital of France?', topK: 1, // to only return 1 result. includeData: true, )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataQuery; $index = new Index( url: "", token: "", ); $results = $index->namespace('my-namespace')->queryData(new DataQuery( data: 'What is the capital of France?', topK: 1, // to only return 1 result. includeData: true, )); ``` ---------------------------------------- TITLE: Upserting Raw Text Data with Upstash Vector DESCRIPTION: This snippet demonstrates how to initialize an Upstash Vector index using a URL and token, then upsert raw text data with an associated ID and optional metadata. It shows examples in Python, JavaScript, Go, PHP, and cURL. SOURCE: https://upstash.com/docs/vector/features/embeddingmodels.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.upsert( [("id-0", "Upstash is a serverless data platform.", {"field": "value"})], ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.upsert({ id: "id-0", data: "Upstash is a serverless data platform.", metadata: { field: "value", }, }) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex("UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN") index.UpsertData(vector.UpsertData{ Id: "id-0", Data: "Upstash is a serverless data platform.", Metadata: map[string]any{"field": "value"}, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataUpsert; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->upsertData(new DataUpsert( id: 'id-0', data: 'Upstash is a serverless data platform.', metadata: [ 'field' => 'value', ], )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert-data \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"id": "1", "data": "Upstash is a serverless data platform.", "metadata": {"field": "value"}}' ``` ---------------------------------------- TITLE: Configure Environment Variables for Upstash Vector and OpenAI DESCRIPTION: This snippet shows the required environment variables for connecting to Upstash Vector and authenticating with OpenAI, which should be placed in a .env file for secure access. SOURCE: https://upstash.com/docs/vector/integrations/llamaindex.mdx LANGUAGE: plaintext CODE: ``` UPSTASH_VECTOR_REST_URL=your_upstash_url UPSTASH_VECTOR_REST_TOKEN=your_upstash_token OPENAI_API_KEY=your_openai_api_key ``` ---------------------------------------- TITLE: Configure Environment Variables for Upstash and OpenAI DESCRIPTION: Add the necessary API keys and URLs to your .env file for Upstash Vector and OpenAI API access. These variables are crucial for connecting to the respective services. SOURCE: https://upstash.com/docs/vector/tutorials/llamaindex.mdx LANGUAGE: plaintext CODE: ``` UPSTASH_VECTOR_REST_URL=your_upstash_url UPSTASH_VECTOR_REST_TOKEN=your_upstash_token OPENAI_API_KEY=your_openai_api_key ``` ---------------------------------------- TITLE: Configure Environment Variables for API Keys and Upstash DESCRIPTION: Defines environment variables for Upstash Vector URL, Upstash Vector Token, OpenAI API Key, and Llama Cloud API Key. These keys are crucial for authenticating with the respective services. SOURCE: https://upstash.com/docs/vector/tutorials/llamaparse.mdx LANGUAGE: plaintext CODE: ``` UPSTASH_VECTOR_REST_URL=your_upstash_url UPSTASH_VECTOR_REST_TOKEN=your_upstash_token OPENAI_API_KEY=your_openai_api_key LLAMA_CLOUD_API_KEY=your_llama_cloud_api_key ``` ---------------------------------------- TITLE: Upsert Text Data for Embedding with Upstash Vector DESCRIPTION: This snippet illustrates how to upsert raw text data into an Upstash Vector index. If the index was created with an Upstash-hosted sparse embedding model, the service will automatically embed the text data behind the scenes before storing it. This simplifies the process by removing the need for client-side embedding. SOURCE: https://upstash.com/docs/vector/features/sparseindexes.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index, Vector index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.upsert( vectors=[ Vector(id="id-0", data="Upstash Vector provides sparse embedding models."), Vector(id="id-1", data="You can upsert text data with these embedding models."), ] ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.upsert([ { id: 'id-0', data: "Upstash Vector provides dense and sparse embedding models.", } ]) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) err := index.UpsertDataMany([]vector.UpsertData{ { Id: "id-0", Data: "Upstash Vector provides sparse embedding models.", }, { Id: "id-1", Data: "You can upsert text data with these embedding models.", }, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataUpsert; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->upsertDataMany([ new DataUpsert( id: 'id-0', data: 'Upstash Vector provides sparse embedding models.', ), new DataUpsert( id: 'id-1', data: 'You can upsert text data with these embedding models.', ), ]); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert-data \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '[ {"id": "id-0", "data": "Upstash Vector provides sparse embedding models."}, {"id": "id-1", "data": "You can upsert text data with these embedding models."} ]' ``` ---------------------------------------- TITLE: Query Upstash Vector with Text Data DESCRIPTION: Demonstrates how to query an Upstash Vector index using raw text data. Upstash handles the embedding process internally before performing the query. This requires an initialized Index object with your Upstash Vector REST URL and Token. SOURCE: https://upstash.com/docs/vector/features/hybridindexes.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.query( data="Upstash Vector", top_k=5, ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.query( { data: "Upstash Vector", topK: 1, }, ) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) scores, err := index.QueryData(vector.QueryData{ Data: "Upstash Vector", TopK: 5, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataQuery; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->queryData(new DataQuery( data: 'Upstash Vector', topK: 5, includeMetadata: true, )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query-data \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"data": "Upstash Vector", "topK": 5}' ``` ---------------------------------------- TITLE: Upsert single data point with embedding model (PHP) DESCRIPTION: Demonstrates how to upsert a single data entry into an Upstash Vector index using the `upsertData()` method. It shows both a simple usage and an example with a specified namespace. The SDK automatically generates vector embeddings for the provided data. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/upsert-data.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataUpsert; $index = new Index( url: "", token: "", ); $index->upsertData(new DataUpsert( id: '1', data: 'The capital of Japan is Tokyo', )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataUpsert; $index = new Index( url: "", token: "", ); $index->namespace('my-namespace')->upsertData(new DataUpsert( id: '1', data: 'The capital of Japan is Tokyo', )); ``` ---------------------------------------- TITLE: Python Example: Fetch Multiple Vectors by ID DESCRIPTION: Demonstrates how to use the `index.fetch` method in Python to retrieve multiple vectors from an Upstash Vector index by their specific IDs, including their vector data, metadata, and general data. It iterates and prints the details of each fetched vector. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/fetch.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index.from_env() # Specify the identifiers of vectors to be fetched ids_to_fetch = ["id-1", "id-2", "id-3"] # Fetch the specified vectors with vectors and metadata included fetch_result = index.fetch( ids=ids_to_fetch, include_vectors=True, include_metadata=True, include_data=True, ) # Display the fetched vectors for vector_info in fetch_result: print("ID:", vector_info.id) print("Vector:", vector_info.vector) print("Metadata:", vector_info.metadata) print("Data:", vector_info.data) ``` ---------------------------------------- TITLE: Execute Basic Range Query in Python DESCRIPTION: Demonstrates how to perform a basic `range` query using the `upstash_vector` Python client. This example fetches a limited number of vectors, excluding the vector embeddings themselves but including metadata and data, and then prints the retrieved information. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/range.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index.from_env() # Execute the range query range_result = index.range( cursor="", limit=10, include_vectors=False, include_metadata=True, include_data=True, ) # Print the range result print("Next Cursor:", range_result.next_cursor) for vector_info in range_result.vectors: print("ID:", vector_info.id) print("Vector:", vector_info.vector) print("Metadata:", vector_info.metadata) print("Data:", vector_info.data) ``` ---------------------------------------- TITLE: Upsert Multiple Vectors in Upstash Vector (PHP) DESCRIPTION: Illustrates how to efficiently upsert multiple vectors into an Upstash Vector index using the `upsertMany()` method. Examples cover both simple index operations and operations within a specified namespace. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/upsert-vectors.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; use function Upstash\Vector\createRandomVector; $index = new Index( url: "", token: "" ); $index->upsertMany([ new VectorUpsert( id: '1', vector: createRandomVector(dimensions: 1536) ), new VectorUpsert( id: '2', vector: createRandomVector(dimensions: 1536) ) ]); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; use function Upstash\Vector\createRandomVector; $index = new Index( url: "", token: "" ); $index->namespace('my-namespace')->upsertMany([ new VectorUpsert( id: '1', vector: createRandomVector(dimensions: 1536) ), new VectorUpsert( id: '2', vector: createRandomVector(dimensions: 1536) ) ]); ``` ---------------------------------------- TITLE: Upsert Vector with Metadata in Upstash Vector DESCRIPTION: This snippet demonstrates how to insert or update vectors along with associated metadata using the Upstash Vector client. Metadata can be used to store additional context, such as a URL, which can be retrieved or filtered later. It requires an initialized Index client with a URL and token. SOURCE: https://upstash.com/docs/vector/features/metadata.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.upsert( [("id-0", [0.9215, 0.3897]), {"url": "https://imgur.com/z9AVZLb"}], ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.upsert({ "id": "id-0", vector: [0.9215, 0.3897], metadata: { url: "https://imgur.com/z9AVZLb", }, }) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex("UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN") index.Upsert(vector.Upsert{ Id: "id-0", Vector: []float32{0.9215, 0.3897}, Metadata: map[string]any{"url": "https://imgur.com/z9AVZLb"}, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; use function Upstash\Vector\createRandomVector; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->upsert(new VectorUpsert( id: 'id-0', vector: createRandomVector(384), metadata: [ 'url' => "https://imgur.com/z9AVZLb", ], )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "id":"id-0", "vector":[0.9215,0.3897], "metadata":{ "url":"https://imgur.com/z9AVZLb" } }' ``` ---------------------------------------- TITLE: Upsert Vectors with Vector Objects (Python) DESCRIPTION: Demonstrates how to insert or update vectors in an Upstash Vector index by creating a list of `upstash_vector.Vector` objects. Each object encapsulates the vector's ID, numerical data, optional metadata, and unstructured data, which are then passed to the `index.upsert` method. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/upsert.mdx LANGUAGE: python CODE: ``` import random from upstash_vector import Index, Vector index = Index.from_env() dimension = 128 # Adjust based on your index's dimension upsert_amount = 100 vectors = [ Vector( id=f"generated-id-{i}", vector=[random.random() for _ in range(dimension)], metadata={"some_field": f"some_value-{i}"}, data=f"some-unstructured-data-{i}", ) for i in range(upsert_amount) ] index.upsert(vectors=vectors) ``` ---------------------------------------- TITLE: Filter Upstash Vector Query Results by Metadata (PHP) DESCRIPTION: Illustrates how to filter vector query results based on their associated metadata values. This allows for precise retrieval of vectors that match specific criteria, such as country or continent. Examples include both direct index interaction and filtering within a specific namespace. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/query.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; $index = new Index( url: "", token: "", ); $results = $index->query(new VectorQuery( vector: [0.1, 0.2, ...], topK: 15, filter: "country = 'PT' AND continent = 'EU'" )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; $index = new Index( url: "", token: "", ); $results = $index->namespace('my-namespace')->query(new VectorQuery( vector: [0.1, 0.2, ...], topK: 15, filter: "country = 'PT' AND continent = 'EU'" )); ``` ---------------------------------------- TITLE: Upsert Method API Reference DESCRIPTION: Defines the arguments and response structure for the `upsert` method, allowing for direct vector input or raw data input for embedding. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/upsert.mdx LANGUAGE: APIDOC CODE: ``` Arguments: VectorPayload (type: Vector | Vector[], required): id (type: string | number, required): The ID of the vector. vector (type: number[], required): The vectors to add to the store. metadata (type: Record): Metadata of the vector. Namespace (type: { namespace?: string }): Namespace to upsert to. If not set, default namespace is used. OR DataPayload (type: Data | Data[], required): id (type: string | number, required): The ID of the vector. data (type: string, required): The data to be embedded and added to the store. metadata (type: Record): Metadata of the vector. Namespace (type: { namespace?: string }): Namespace to upsert to. If not set, default namespace is used. Response: type: str value: 'Success' on successful operation. ``` ---------------------------------------- TITLE: Upsert Data API Reference DESCRIPTION: Detailed API documentation for the Upsert Data endpoint, including HTTP method, URL, authentication, request body parameters, path parameters, and response structure. SOURCE: https://upstash.com/docs/vector/api/endpoints/upsert-data.mdx LANGUAGE: APIDOC CODE: ``` Endpoint: POST https://{endpoint}/upsert-data/{namespace} Authentication: Bearer Token Request: Body Parameters: - id (string, required): The id of the vector. - data (string, required): The raw text data to embed and upsert. - metadata (Object, optional): The metadata of the vector. This makes identifying vectors on retrieval easier and can be used to with filters on queries. Note: Data field of the vector will be automatically set to the raw text data. Path Parameters: - namespace (string, optional, default: ""): The namespace to use. When no namespace is specified, the default namespace will be used. Response: - result (string): "Success" string. ``` ---------------------------------------- TITLE: Update Existing Vector Data and Metadata in TypeScript DESCRIPTION: Demonstrates updating an existing vector's data and metadata by calling `upsert` with the same ID. The example also includes an `index.update` call. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/upsert.mdx LANGUAGE: typescript CODE: ``` await index.upsert({ id: "1234", vector: [0.1, 0.2, 0.3, 0.4, 0.5] metadata: { title: "Redis" } }) await index.update({ id: "1234", metadata: { title: "QStash" } }) ``` ---------------------------------------- TITLE: Python Example: Batch Querying Upstash Vector Index DESCRIPTION: This Python example demonstrates how to perform multiple queries in a single network request using the `index.query_many` method. It shows how to construct a list of query configurations, each with its own parameters and filters, and then process the results for each individual query. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/query.mdx LANGUAGE: python CODE: ``` import random from upstash_vector import Index index = Index.from_env() # Generate a random vector for similarity comparison dimension = 128 # Adjust based on your index's dimension query_vectors = [[random.random() for _ in range(dimension)] for _ in range(2)] # Execute the query query_results = index.query_many( queries=[ { "vector": query_vectors[0], "include_metadata": True, "include_data": True, "include_vectors": False, "top_k": 5, "filter": "genre = 'fantasy' and title = 'Lord of the Rings'", }, { "vector": query_vectors[1], "include_metadata": False, "include_data": False, "include_vectors": True, "top_k": 3, "filter": "genre = 'drama'", }, ] ) for i, query_result in enumerate(query_results): print(f"Query-{i} result:") # Print the query result for result in query_result: print("Score:", result.score) print("ID:", result.id) print("Vector:", result.vector) print("Metadata:", result.metadata) print("Data:", result.data) ``` ---------------------------------------- TITLE: API Reference: Query Vectors Endpoint DESCRIPTION: Detailed documentation for the Upstash Vector 'Query Vectors' API endpoint, outlining its HTTP method, authentication requirements, and all supported request and path parameters for querying approximate nearest neighbors. SOURCE: https://upstash.com/docs/vector/api/endpoints/query.mdx LANGUAGE: APIDOC CODE: ``` Endpoint: POST https://{endpoint}/query/{namespace} Authentication: Bearer Token Request Body Parameters: - vector: number[] (required) Description: The query vector. Must have the same dimensions as your index. - topK: number (default: 10) Description: The maximum number of vectors to return, sorted by distance metric score. - includeMetadata: boolean (default: false) Description: Whether to include vector metadata in the response. Recommended for identification. - includeVectors: boolean (default: false) Description: Whether to include vector values in the response. Recommended to set to `false` due to size. - includeData: boolean (default: false) Description: Whether to include the data of the vectors in the response. - filter: string (default: "") Description: Metadata filter to apply. Refer to /vector/features/filtering. - weightingStrategy: string (optional) Description: Weighting strategy for sparse vectors in sparse/hybrid indexes. Possible value: `IDF` (Inverse Document Frequency). If not provided, no weighting is used. - fusionAlgorithm: string (optional) Description: Fusion algorithm for dense and sparse components of a hybrid index. Defaults to `RRF` (Reciprocal Rank Fusion). Other possible value: `DBSF` (Distribution-Based Score Fusion). Path Parameters: - namespace: string (default: "") Description: The target namespace for the query. Uses the default namespace if not specified. ``` ---------------------------------------- TITLE: Perform Range Query on Upstash Vector Index DESCRIPTION: Illustrates how to perform a range query on the Upstash Vector index. This operation retrieves vectors starting from a specified cursor, limited by a given count, and optionally includes associated metadata. Examples are provided for Python, JavaScript, Go, PHP, and cURL. SOURCE: https://upstash.com/docs/vector/features/metadata.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.range( cursor="0", limit=3, include_metadata=True, ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.range({ cursor: "0", limit: 3, includeMetadata: true, }) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex("UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN") index.Range(vector.Range{ Cursor: "0", Limit: 3, IncludeMetadata: true, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorRange; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->range(new VectorRange( limit: 3, includeMetadata: true, )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/range \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "cursor" : "0", "limit" : 3, "includeMetadata": true}' ``` ---------------------------------------- TITLE: Inject Upstash Vector IndexInterface into Controller DESCRIPTION: Illustrates how to use dependency injection to obtain an `IndexInterface` instance in a Laravel controller, allowing direct interaction with the Upstash Vector index, such as listing namespaces. SOURCE: https://upstash.com/docs/vector/sdks/php/laravel.mdx LANGUAGE: php CODE: ``` namespace App\Http\Controllers; use Upstash\Vector\Contracts\IndexInterface; class Controller { public function index(IndexInterface $index) { $namespaces = $index->listNamespaces(); return response()->json(['namespaces' => $namespaces]); } } ``` ---------------------------------------- TITLE: Upstash Vector Query API Response Structure DESCRIPTION: Defines the structure of the response object(s) returned by the Upstash Vector Query API, including fields for ID, score, vector data, metadata, and unstructured data. Scores are normalized for dense indexes (0-1) and arbitrary for sparse/hybrid indexes. SOURCE: https://upstash.com/docs/vector/api/endpoints/query.mdx LANGUAGE: APIDOC CODE: ``` Scores (Object[]) id (string, required): The id of the vector. score (number, required): The similarity score of the vector, calculated based on the distance metric of your index. vector (number[]): The dense vector value for dense and hybrid indexes. sparseVector (Object[]) indices (number[]): Indices of the non-zero valued dimensions. values (number[]): Values of the non-zero valued dimensions. metadata (Object): The metadata of the vector, if any. data (string): The unstructured data of the vector, if any. ``` ---------------------------------------- TITLE: Initialize UpstashVectorStore with Namespace DESCRIPTION: This code demonstrates how to initialize the `UpstashVectorStore` and specify a `namespace`. Namespaces allow for logical separation of different types of documents within the same vector index, enabling more organized and targeted searches. SOURCE: https://upstash.com/docs/vector/tutorials/langchain.mdx LANGUAGE: python CODE: ``` store = UpstashVectorStore(embedding=True, namespace="my_namespace") ``` ---------------------------------------- TITLE: Upsert Vectors with Namespace (Python) DESCRIPTION: Explains how to perform an upsert operation on a specific namespace within an Upstash Vector index. This snippet demonstrates passing the `namespace` argument to the `index.upsert` method to target a particular data partition. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/upsert.mdx LANGUAGE: python CODE: ``` index.upsert(..., namespace="ns") ``` ---------------------------------------- TITLE: Example: Perform Resumable Query with cURL DESCRIPTION: A practical example demonstrating how to invoke the resumable query API using the cURL command-line tool. This snippet shows how to send text data ('Hello world') and specify parameters like `topK`, `includeMetadata`, and `maxIdle` in the request body. SOURCE: https://upstash.com/docs/vector/api/endpoints/resumable-query/start-with-data.mdx LANGUAGE: curl CODE: ``` curl $UPSTASH_VECTOR_REST_URL/resumable-query-data \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "data": "Hello world", "topK": 2, "includeMetadata": true, "maxIdle": 3600 }' ``` ---------------------------------------- TITLE: Configure Upstash Vector Store with Namespace DESCRIPTION: Demonstrates how to specify a namespace when initializing the UpstashVectorStore instance to logically separate different types of documents within the same index. SOURCE: https://upstash.com/docs/vector/tutorials/llamaindex.mdx LANGUAGE: python CODE: ``` vector_store = UpstashVectorStore( url=your_upstash_url, token=your_upstash_token, namespace=your_namespace, ) ``` ---------------------------------------- TITLE: Scan Entire Upstash Vector Index in Python DESCRIPTION: Illustrates how to iterate through an entire Upstash Vector index using the `range` method with cursor-based pagination. The loop continuously fetches batches of vectors until all results are retrieved, demonstrating a full index scan. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/range.mdx LANGUAGE: python CODE: ``` res = index.range(cursor="", limit=5) print(res.vectors) while res.next_cursor != "": res = index.range(cursor=res.next_cursor, limit=10) print(res.vectors) ``` ---------------------------------------- TITLE: Python Example: Querying Upstash Vector Index DESCRIPTION: This Python example demonstrates how to perform a single query against an Upstash Vector index. It shows how to generate a random query vector, specify parameters like `include_metadata`, `include_data`, `top_k`, and apply a metadata filter, then iterate and print the results. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/query.mdx LANGUAGE: python CODE: ``` import random from upstash_vector import Index index = Index.from_env() # Generate a random vector for similarity comparison dimension = 128 # Adjust based on your index's dimension query_vector = [random.random() for _ in range(dimension)] # Execute the query query_result = index.query( vector=query_vector, include_metadata=True, include_data=True, include_vectors=False, top_k=5, filter="genre = 'fantasy' and title = 'Lord of the Rings'", ) # Print the query result for result in query_result: print("Score:", result.score) print("ID:", result.id) print("Vector:", result.vector) print("Metadata:", result.metadata) print("Data:", result.data) ``` ---------------------------------------- TITLE: Perform Semantic Search by Vector Embedding DESCRIPTION: This Python code illustrates an alternative method for semantic search, directly using a pre-computed embedding vector. It performs a similarity search based on the provided vector to find the most relevant documents in the Upstash Vector store. SOURCE: https://upstash.com/docs/vector/tutorials/huggingface-embeddings.mdx LANGUAGE: python CODE: ``` result_vector_query = vector_store.similarity_search_by_vector( embedding=query_embedding, k=5 ) print("Results for vector-based similarity search:") for res in result_vector_query: print(res.page_content) ``` ---------------------------------------- TITLE: Query Upstash Vector with Metadata Filtering via cURL DESCRIPTION: Demonstrates how to query the Upstash Vector index using a REST API call via cURL. The query includes a vector embedding, specifies the number of top results (topK), requests metadata inclusion, and applies a filter based on a URL pattern using GLOB. SOURCE: https://upstash.com/docs/vector/features/metadata.mdx LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "vector":[0.9215,0.3897], "topK" : 5, "includeMetadata": true, "filter": "url GLOB \"*imgur.com*\"" }' ``` ---------------------------------------- TITLE: Upsert Vectors into a Specific Namespace in TypeScript DESCRIPTION: Illustrates how to upsert vectors into a designated namespace, overriding the default namespace if set. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/upsert.mdx LANGUAGE: typescript CODE: ``` await index.upsert([ { id: "6789", vector: [0.6, 0.7, 0.8, 0.9, 0.9], }, ], { namespace: "my-namespace" }); ``` ---------------------------------------- TITLE: Upstash Vector Index Batch Query Method API Documentation DESCRIPTION: Detailed API documentation for the `query_many` method, which allows performing multiple queries in a single API call. It describes the structure of the `queries` list, where each item is a query object with parameters similar to the single `query` method, and the expected list of results. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/query.mdx LANGUAGE: APIDOC CODE: ``` Method: query_many Parameters: queries: A list of query objects. Each object can contain: vector: The reference vector for similarity comparison. sparse_vector: The sparse vector value to query. data: A string for text-based queries (mutually exclusive with vector). include_metadata: A boolean flag indicating whether to include metadata in the query results. include_vector: A boolean flag indicating whether to include vectors in the query results. include_data: A boolean flag indicating whether to include data in the query results. top_k: The number of top matching vectors to retrieve. filter: Metadata filtering of the vector is used to query your data based on the filters and narrow down the query results. namespace: The namespace to use. When not specified, the default namespace is used. weighting_strategy: Weighting strategy to be used for sparse vectors. fusion_algorithm: Fusion algorithm to use while fusing scores from hybrid vectors. query_mode: Query mode for hybrid indexes with Upstash-hosted embedding models. Response: A list of query results, where each item corresponds to a query in the input list. Each result object has the following fields: id: The identifier associated with the matching vector. metadata: Additional information or attributes linked to the matching vector. score: A measure of similarity indicating how closely the vector matches the query vector. The score is normalized to the range [0, 1], where 1 indicates a perfect match. vector: The vector itself (included only if `include_vector` is set to `True`). sparse_vector: The sparse vector itself (included only if `include_vector` is set to `True`). data: Additional unstructured information linked to the matching vector. ``` ---------------------------------------- TITLE: Query Upstash Vector with Explicit Dense and Sparse Vectors DESCRIPTION: Demonstrates how to query the Upstash Vector index using explicit dense vectors (float arrays) and sparse vectors (indices and values). This allows for separate retrieval of dense and sparse results for custom reranking based on your application's needs. SOURCE: https://upstash.com/docs/vector/features/hybridindexes.mdx LANGUAGE: JavaScript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) const denseResults = await index.query( { vector: [0.5, 0.4], topK: 3, }, ) const sparseResults = await index.query( { sparseVector: { indices: [2, 3], values: [0.13, 0.87], }, topK: 3, }, ) // Rerank dense and sparse results as you like here ``` LANGUAGE: Go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) denseScores, err := index.Query(vector.Query{ Vector: []float32{0.5, 0.4}, }) sparseScores, err := index.Query(vector.Query{ SparseVector: &vector.SparseVector{ Indices: []int32{3, 5}, Values: []float32{0.3, 05}, }, }) // Rerank dense and sparse results as you like here } ``` LANGUAGE: PHP CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; use Upstash\Vector\SparseVector; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $denseResults = $index->query(new VectorQuery( vector: [0.5, 0.4], topK: 3, )); $sparseResults = $index->query(new VectorQuery( sparseVector: new SparseVector( indices: [3, 5], values: [0.3, 0.5], ), topK: 3, )); // Rerank dense and sparse results as you like here ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"vector": [0.5, 0.4]}' curl $UPSTASH_VECTOR_REST_URL/query \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"sparseVector": {"indices": [3, 5], "values": [0.3, 0.5]}}' ``` ---------------------------------------- TITLE: Configure Upstash Vector Client via Environment Variables DESCRIPTION: Demonstrates how to set `UPSTASH_VECTOR_REST_URL` and `UPSTASH_VECTOR_REST_TOKEN` as environment variables. When these variables are present, the `Index` client can be initialized without explicit arguments, simplifying the setup process. SOURCE: https://upstash.com/docs/vector/sdks/ts/getting-started.mdx LANGUAGE: bash CODE: ``` UPSTASH_VECTOR_REST_URL="your_rest_url" UPSTASH_VECTOR_REST_TOKEN="your_rest_token" ``` LANGUAGE: typescript CODE: ``` import { Index } from "@upstash/vector"; const index = new Index(); ``` ---------------------------------------- TITLE: Configure Upstash Vector Client with Environment Variables DESCRIPTION: Defines the environment variables required for the Upstash Vector client: UPSTASH_VECTOR_REST_URL and UPSTASH_VECTOR_REST_TOKEN. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: bash CODE: ``` UPSTASH_VECTOR_REST_URL="your_rest_url" UPSTASH_VECTOR_REST_TOKEN="your_rest_token" ``` ---------------------------------------- TITLE: Filter Values Using SQL IN Operator DESCRIPTION: Explains the `IN` operator for filtering keys whose value matches any of the provided literals. This operator is applicable to string, number, and boolean values. The first example filters countries that are 'Germany', 'Turkey', or 'France'. The second code block shows its semantic equivalence using multiple equals operators combined with `OR`. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` country IN ('Germany', 'Turkey', 'France') ``` LANGUAGE: SQL CODE: ``` country = 'Germany' OR country = 'Turkey' OR country = 'France' ``` ---------------------------------------- TITLE: Query Upstash Vector Index Using Vector Embedding DESCRIPTION: Demonstrates how to query an Upstash Vector index by providing a vector embedding. This example shows how to include metadata and vectors in the response and provides an example of the returned array of `QueryResult` objects, including an optional namespace. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/query.mdx LANGUAGE: typescript CODE: ``` await index.query({ topK: 2, vector: [ ... ], includeMetadata: true, includeVectors: true }, { namespace: "my-namespace" }) /* [ { id: '6345', score: 0.85, vector: [], metadata: { sentence: "Upstash is great." } }, { id: '1233', score: 0.75, vector: [], metadata: undefined }, ] */ ``` ---------------------------------------- TITLE: Query Upstash Vector Index by Vector Values DESCRIPTION: Example of querying an Upstash Vector index using a vector, demonstrating the query method with topK and vector parameters, and showing the structure of the returned matches. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: typescript CODE: ``` > await index.query({ topK: 3, vector: [ ... ]}) { matches: [ { id: '6345', score: 1.00000012, vector: [], metadata: undefined }, { id: '1233', score: 1.00000012, vector: [], metadata: undefined }, { id: '4142', score: 1.00000012, vector: [], metadata: undefined } ], namespace: '' } ``` ---------------------------------------- TITLE: Update Vector Data and Metadata in Python DESCRIPTION: This Python example demonstrates how to update an existing vector's metadata and associated data using the `index.update` method. It initializes an `Index` object from environment variables and then performs an update operation on a vector identified by 'id1', setting new metadata and data values. The result of the update operation is printed to the console. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/update.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index.from_env() updated = index.update( id="id1", metadata={"new": "metadata"}, data="new-data", ) print(updated) ``` ---------------------------------------- TITLE: Query Upstash Vector Index with Metadata Typing DESCRIPTION: Example of querying an Upstash Vector index, demonstrating how to pass a Metadata type parameter to query() for proper TypeScript type-checking on results, and accessing metadata fields. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: typescript CODE: ``` const results = await index.query({ vector: [ ... // query embedding ], includeVectors: true, topK: 1 }) if (results[0].metadata) { // Since we passed the Metadata type parameter above, // we can interact with metadata fields without having to // do any typecasting. const { title, genre, category } = results[0].metadata; console.log(`The best match in fantasy was ${title}`) } ``` ---------------------------------------- TITLE: Upsert multiple data points with embedding model (PHP) DESCRIPTION: Illustrates how to efficiently upsert multiple data entries into an Upstash Vector index using the `upsertDataMany()` method. It includes examples for both simple usage and with a specified namespace, highlighting batch processing for improved performance. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/upsert-data.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataUpsert; $index = new Index( url: "", token: "", ); $index->upsertDataMany([ new DataUpsert(id: '1', data: 'The capital of Japan is Tokyo'), new DataUpsert(id: '2', data: 'The capital of France is Paris'), new DataUpsert(id: '3', data: 'The capital of Germany is Berlin'), ]); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataUpsert; $index = new Index( url: "", token: "", ); $index->namespace('my-namespace')->upsertDataMany([ new DataUpsert(id: '1', data: 'The capital of Japan is Tokyo'), new DataUpsert(id: '2', data: 'The capital of France is Paris'), new DataUpsert(id: '3', data: 'The capital of Germany is Berlin'), ]); ``` ---------------------------------------- TITLE: Batch Query Upstash Vector Index (cURL) DESCRIPTION: Example cURL command to perform a batch query on the Upstash Vector index, sending multiple vectors in a single request. This optimizes performance for multiple queries. SOURCE: https://upstash.com/docs/vector/api/endpoints/query.mdx LANGUAGE: sh curl CODE: ``` curl "$UPSTASH_VECTOR_REST_URL/query" \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '[ { "vector": [0.1, 0.2], "topK": 2, "includeMetadata": true }, { "vector": [0.2, 0.3], "topK": 3 } ]' ``` ---------------------------------------- TITLE: Delete Vectors by Metadata Filter (TypeScript) DESCRIPTION: Demonstrates how to delete vectors based on a metadata filter string, noting that this is an O(N) operation and can be slow for large indexes. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/delete.mdx LANGUAGE: typescript CODE: ``` const response = await index.delete({ filter: "age > 30", }); // { deleted: 3 } ``` ---------------------------------------- TITLE: Delete Vectors by ID Prefix in PHP DESCRIPTION: This snippet illustrates how to delete multiple vectors that share a common ID prefix. It utilizes the `VectorDeleteByPrefix` class for efficient bulk deletion, providing examples for both direct index and namespaced operations. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/delete-vectors.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorDeleteByPrefix; $index = new Index( url: "", token: "", ); $index->delete(new VectorDeleteByPrefix( prefix: 'users:', )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorDeleteByPrefix; $index = new Index( url: "", token: "", ); $index->namespace('my-namespace')->delete(new VectorDeleteByPrefix( prefix: 'users:', )); ``` ---------------------------------------- TITLE: Query Upstash Vector Index (cURL) DESCRIPTION: Example cURL command to query the Upstash Vector index with a single vector, requesting metadata. This demonstrates a basic query operation. SOURCE: https://upstash.com/docs/vector/api/endpoints/query.mdx LANGUAGE: sh curl CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "vector": [0.1, 0.2], "topK": 2, "includeMetadata": true }' ``` ---------------------------------------- TITLE: Initialize Upstash Vector Client with Configuration Object DESCRIPTION: Shows how to initialize the Upstash Vector client by directly passing a configuration object containing the `url` and `token`. This method is particularly useful for applications that need to interact with multiple projects, each requiring different configuration values. SOURCE: https://upstash.com/docs/vector/sdks/ts/getting-started.mdx LANGUAGE: typescript CODE: ``` import { Index } from "@upstash/vector"; const index = new Index({ url: "", token: "" }); ``` ---------------------------------------- TITLE: Delete Specific Vectors by ID in PHP DESCRIPTION: This snippet demonstrates how to delete specific vectors from an Upstash Vector index using their unique IDs. It shows examples for both direct index deletion and deletion within a specified namespace. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/delete-vectors.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; $index = new Index( url: "", token: "", ); $index->delete(['1', '2', '3']); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; $index = new Index( url: "", token: "", ); $index->namespace('my-namespace')->delete(['1', '2', '3']); ``` ---------------------------------------- TITLE: Update Existing Data String and Metadata in TypeScript DESCRIPTION: Demonstrates updating an existing data entry's content and metadata by calling `upsert` with the same ID. The example shows two consecutive `upsert` calls to modify different fields. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/upsert.mdx LANGUAGE: typescript CODE: ``` await index.upsert({ id: "1234", data: "Upstash product" metadata: { title: "Redis" } }) await index.upsert({ id: "1234", metadata: { title: "QStash" } }) ``` ---------------------------------------- TITLE: Python Example: Querying with a Specific Namespace DESCRIPTION: This snippet illustrates how to specify a particular namespace when executing a query against the Upstash Vector index. If no namespace is provided, the default namespace is used. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/query.mdx LANGUAGE: python CODE: ``` index.query(..., namespace="ns") ``` ---------------------------------------- TITLE: Specify Namespace for Vector Update in Python DESCRIPTION: This example demonstrates how to explicitly specify a namespace when performing an update operation on a vector. By providing the `namespace` argument, the update will be applied to the vector within the designated namespace. If this argument is omitted, the operation defaults to the primary or default namespace. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/update.mdx LANGUAGE: python CODE: ``` index.update(..., namespace="ns") ``` ---------------------------------------- TITLE: Query Upstash Vector Index with Improved Typechecking DESCRIPTION: Shows how to leverage TypeScript's type parameters to define the expected metadata structure when querying an Upstash Vector index. This allows for type-safe access to metadata fields in the query results without requiring explicit type casting, improving code readability and maintainability. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/query.mdx LANGUAGE: typescript CODE: ``` type Metadata = { title: string, genre: 'sci-fi' | 'fantasy' | 'horror' | 'action' } const results = await index.query({ vector: [ ... // query embedding ], includeVectors: true, topK: 1, filter: "genre = 'fantasy' and title = 'Lord of the Rings'" }) if (results[0].metadata) { // Since we passed the Metadata type parameter above, // we can interact with metadata fields without having to // do any typecasting. const { title, genre } = results[0].metadata; console.log(`The best match in fantasy was ${title}`) } ``` ---------------------------------------- TITLE: Delete a Single Vector by ID in Python DESCRIPTION: Shows a simplified way to delete a single vector from the index by directly passing its identifier to the `delete` method. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/delete.mdx LANGUAGE: python CODE: ``` index.delete("id-4") ``` ---------------------------------------- TITLE: Basic Range Query Example (TypeScript) DESCRIPTION: Demonstrates a fundamental usage of the `index.range` method in TypeScript, specifying a cursor, limit, and requesting the inclusion of metadata. The example also shows the expected JSON response structure. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/range.mdx LANGUAGE: typescript CODE: ``` const responseRange = await index.range( { cursor: 0, limit: 2, includeMetadata: true, }, { namespace: "my-namespace" } ); /* { nextCursor: '2', vectors: [ { id: '0', metadata: { keyword: "Vector" } }, { id: '19', metadata: { keyword: "Redis" } } ] } */ ``` ---------------------------------------- TITLE: Upsert Vectors with Tuples (Python) DESCRIPTION: Illustrates inserting or updating vectors in an Upstash Vector index using a list of tuples. Each tuple represents a vector, containing its ID, numerical data, optional metadata, and unstructured data, which is then provided to the `index.upsert` method. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/upsert.mdx LANGUAGE: python CODE: ``` import random from upstash_vector import Index index = Index.from_env() dimension = 128 # Adjust based on your index's dimension upsert_amount = 100 vectors = [ ( f"generated-id-{i}", [random.random() for _ in range(dimension)], {"some_field": f"some_value-{i}"}, f"some-unstructured-data-{i}", ) for i in range(upsert_amount) ] index.upsert(vectors=vectors) ``` ---------------------------------------- TITLE: Delete Records from Upstash Vector Index DESCRIPTION: Demonstrates how to delete multiple records (using an array of IDs) or a single record (using a single ID) from an Upstash Vector index. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: typescript CODE: ``` // Delete multiple records await index.delete([...]); // Delete a single record await index.delete("id-to-delete"); ``` ---------------------------------------- TITLE: Upstash Vector Data Query cURL Examples DESCRIPTION: Illustrates various cURL commands for querying data from Upstash Vector. Examples include a basic data query, a namespaced query, and a batch query with multiple data points, demonstrating how to send data and specify parameters like topK and includeMetadata. SOURCE: https://upstash.com/docs/vector/api/endpoints/query-data.mdx LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query-data \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "data": "What is Upstash?", "topK": 2, "includeMetadata": true }' ``` LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query-data/ns \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "data": "What is Upstash?", "topK": 2, "includeMetadata": true }' ``` LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query-data \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '[ { "data": "What is Upstash?", "topK": 2, "includeMetadata": true }, { "data": "What is Upstash Vector?", "topK": 3 } ]' ``` ---------------------------------------- TITLE: Upsert Data cURL Request Examples DESCRIPTION: Examples demonstrating how to use cURL to upsert data to the Upstash Vector API, including both batch upsert and single upsert with a specified namespace. Requires `UPSTASH_VECTOR_REST_URL` and `UPSTASH_VECTOR_REST_TOKEN` environment variables. SOURCE: https://upstash.com/docs/vector/api/endpoints/upsert-data.mdx LANGUAGE: curl CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert-data \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '[ \ { "id": "id-0", "data": "Upstash is a serverless data platform.", "metadata": { "link": "upstash.com" } }, \ { "id": "id-1", "data": "Upstash Vector is a serverless vector database." }\ ]' ``` LANGUAGE: curl CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert-data/ns \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "id": "id-2", "data": "Upstash is a serverless data platform.", "metadata": { "link": "upstash.com" } }' ``` ---------------------------------------- TITLE: Update a Record in Upstash Vector Index DESCRIPTION: Demonstrates how to update an existing record in an Upstash Vector index by specifying its ID and the attributes (e.g., metadata) to be updated. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: typescript CODE: ``` await index.upsert({ id: "18593", metadata: { genre: "romance" } }); ``` ---------------------------------------- TITLE: Query Upstash Vector Index with Namespace (cURL) DESCRIPTION: Example cURL command to query a specific namespace within the Upstash Vector index with a single vector. This is useful for multi-tenant or categorized data. SOURCE: https://upstash.com/docs/vector/api/endpoints/query.mdx LANGUAGE: sh curl CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query/ns \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "vector": [0.1, 0.2], "topK": 2, "includeMetadata": true }' ``` ---------------------------------------- TITLE: Load Document with LangChain TextLoader DESCRIPTION: This Python snippet demonstrates how to load a text document, specifically 'global_warming.txt', using LangChain's TextLoader. The loaded content is stored in the 'documents' variable, ready for subsequent processing like text splitting and embedding. SOURCE: https://upstash.com/docs/vector/tutorials/langchain.mdx LANGUAGE: python CODE: ``` from langchain_community.document_loaders import TextLoader from langchain_text_splitters import CharacterTextSplitter # Load the document loader = TextLoader("documents/global_warming.txt") documents = loader.load() ``` ---------------------------------------- TITLE: Delete Multiple Vectors by IDs in Python DESCRIPTION: Demonstrates how to delete multiple vectors from an Upstash Vector index using a list of specific identifiers. It initializes the index from environment variables and prints the count of successfully deleted vectors. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/delete.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index.from_env() # Specify the identifiers of vectors to be deleted ids_to_delete = ["id1", "id2", "id3"] # Delete the specified vectors delete_result = index.delete(ids=ids_to_delete) # Display the number of vectors deleted print("Number of Vectors Deleted:", delete_result.deleted) ``` ---------------------------------------- TITLE: Upstash Vector QueryResult Response Structure DESCRIPTION: Defines the structure of the `QueryResult` object returned by the Upstash Vector query operation, detailing each field's type, requirement, and purpose. This object represents a single matched vector from the index. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/query.mdx LANGUAGE: APIDOC CODE: ``` QueryResult: id: string | number (required) - The ID of the resulting vector. score: number (required) - The score of the vector data, calculated based on the distance metric of your index. vector: number[] (optional) - The resulting vector (if `includeVectors` is set to true) sparseVector: SparseVector (optional) - The resulting sparseVector (if `includeVectors` is set to true) metadata: Record (optional) - The metadata of the vector (if `includeMetadata` is set to true) data: string (optional) - The data of the vector (if `includeData` is set to true) ``` ---------------------------------------- TITLE: Range Vectors API Request Examples with cURL DESCRIPTION: Illustrative cURL commands demonstrating how to make requests to the Upstash Vector Range API. The first example shows a basic request to the default namespace, while the second demonstrates specifying a custom namespace. Both examples include authentication and essential request body parameters. SOURCE: https://upstash.com/docs/vector/api/endpoints/range.mdx LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/range \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "cursor": "0", "limit": 2, "includeMetadata": true }' ``` LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/range/ns \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "cursor": "0", "limit": 2, "includeMetadata": true }' ``` ---------------------------------------- TITLE: Reset All Namespaces (Python) DESCRIPTION: This example shows how to clear all vectors and metadata across all namespaces in an Upstash Vector index. It initializes the index and then calls the `reset()` method, setting the `all` argument to `True` to perform a full index reset. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/reset.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index.from_env() index.reset(all=True) ``` ---------------------------------------- TITLE: Creating a Server Action to Upsert Resources DESCRIPTION: This code defines a Next.js server action `createResource`. It uses `zod` for input validation, generates a unique `resourceId`, and then calls the `upsertEmbeddings` function (from the custom embedding model example) to store the content in Upstash Vector. SOURCE: https://upstash.com/docs/vector/integrations/ai-sdk.mdx LANGUAGE: typescript CODE: ``` 'use server' import { z } from 'zod' import { upsertEmbeddings } from '@/lib/ai/upstashVector' // A simple schema for incoming resource content const NewResourceSchema = z.object({ content: z.string().min(1), }) // Server action to parse the input and upsert to the index export async function createResource(input: { content: string }) { const { content } = NewResourceSchema.parse(input) // Generate a random ID const resourceId = crypto.randomUUID() // Upsert the chunks/embeddings to Upstash Vector await upsertEmbeddings(resourceId, content) return `Resource ${resourceId} created and embedded.` } ``` ---------------------------------------- TITLE: Filter Range Query by ID Prefix in Python DESCRIPTION: Shows how to use the `prefix` parameter with the `range` method to retrieve only vectors whose IDs start with a specified string. This is useful for efficiently querying subsets of data based on ID patterns. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/range.mdx LANGUAGE: python CODE: ``` index.range(prefix="id-") ``` ---------------------------------------- TITLE: Install Upstash Vector Node.js Client DESCRIPTION: Instructions for installing the Upstash Vector Node.js client using npm or pnpm. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: bash CODE: ``` npm install @upstash/vector pnpm add @upstash/vector ``` ---------------------------------------- TITLE: Delete Vectors by IDs Array (TypeScript) DESCRIPTION: Demonstrates how to delete multiple vectors by providing an array of their IDs to the index.delete method. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/delete.mdx LANGUAGE: typescript CODE: ``` const response = await index.delete(["2", "3"]); // { deleted: 2 } ``` ---------------------------------------- TITLE: Example Metadata Structure for Filtering DESCRIPTION: An example JSON object demonstrating the structure of metadata that can be used for filtering vector similarity search results in Upstash Vector. It includes various data types like strings, numbers, booleans, nested objects, and arrays. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: json CODE: ``` { "city": "Istanbul", "country": "Turkey", "is_capital": false, "population": 15460000, "geography": { "continent": "Asia", "coordinates": { "latitude": 41.0082, "longitude": 28.9784 } }, "economy": { "currency": "TRY", "major_industries": [ "Tourism", "Textiles", "Finance" ] } } ``` ---------------------------------------- TITLE: Filter Strings Using SQL GLOB Operator DESCRIPTION: Demonstrates how to use the `GLOB` operator in SQL-like syntax to filter string values based on a UNIX glob pattern. It supports `*` (zero or more characters), `?` (exactly one character), and `[]` (character list/range) wildcards, and is case-sensitive. The example filters city names where the second character is 's' or 'z' and ends with anything other than 'm' through 'z'. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` city GLOB '?[sz]*[^m-z]' ``` ---------------------------------------- TITLE: Upsert Vectors with Dictionaries (Python) DESCRIPTION: Shows how to insert or update vectors in an Upstash Vector index by constructing a list of dictionaries. Each dictionary defines a vector's ID, numerical data, optional metadata, and unstructured data, which are subsequently passed to the `index.upsert` method. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/upsert.mdx LANGUAGE: python CODE: ``` import random from upstash_vector import Index index = Index.from_env() dimension = 128 # Adjust based on your index's dimension upsert_amount = 100 vectors = [ { "id": f"generated-id-{i}", "vector": [random.random() for _ in range(dimension)], "metadata": {"some_field": f"some_value-{i}"}, "data": f"some-unstructured-data-{i}", } for i in range(upsert_amount) ] index.upsert(vectors=vectors) ``` ---------------------------------------- TITLE: Delete Vectors by Metadata Filter in Python DESCRIPTION: Demonstrates how to delete vectors based on a metadata filter expression. All vectors whose metadata matches the provided filter will be removed from the index. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/delete.mdx LANGUAGE: python CODE: ``` index.delete(filter="age > 30") ``` ---------------------------------------- TITLE: Access Upstash Vector Index for Data Operations DESCRIPTION: Demonstrates how to access an index to perform data operations, specifically using the fetch method with options to include metadata and vectors. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: typescript CODE: ``` await index.fetch([....], { includeMetadata: true, includeVectors: true }); ``` ---------------------------------------- TITLE: Install Upstash Vector Python SDK DESCRIPTION: Instructions to install the `upstash-vector` Python SDK using pip, the standard Python package installer. This command adds the necessary library to your Python environment. SOURCE: https://upstash.com/docs/vector/sdks/py/gettingstarted.mdx LANGUAGE: bash CODE: ``` pip install upstash-vector ``` ---------------------------------------- TITLE: Filter Array Elements Using SQL CONTAINS Operator DESCRIPTION: Demonstrates the `CONTAINS` operator, used to filter array values where the array includes the specified literal. This operator is specifically applicable to `array` types. The example checks if 'Tourism' is present within the `economy.major_industries` array. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` economy.major_industries CONTAINS 'Tourism' ``` ---------------------------------------- TITLE: Fetch API Reference DESCRIPTION: Detailed API documentation for the vector fetch operation, including arguments and response structure. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/fetch.mdx LANGUAGE: APIDOC CODE: ``` Arguments: IDs: string[] | number[] (required) Description: The IDs of the vectors you want to fetch. OR FetchPayload: object (required) ids: string[] | number[] Description: The IDs of the vectors you want to fetch. prefix: string Description: An id prefix to match vector IDs. Warning: For fetching larger datasets with prefix, it is recommended to use the paginated `range` command instead. Options: Object includeMetadata: boolean Description: Whether to include the metadata of the vectors in the response. Setting this `true` would be the best practice, since it will make it easier to identify the vectors. includeVectors: boolean Description: Whether to include the vector themselves in the response. includeData: boolean Description: Whether to include the data field in the response. namespace: string Description: Namespace to fetch from. If not set, default namespace is used. Response: FetchResult[]: Vector[] (required) Description: This field is `null` if no vector with the specified id is found. id: string | number (required) Description: The ID of the resulting vector. vector: number[] Description: The vectors (if `includeVectors` is set to true) sparseVector: SparseVector Description: The resulting sparseVector (if `includeVectors` is set to true) metadata: Record Description: The metadata of the vectors (if `includeMetadata` is set to true) data: string Description: The data of the vector (if `includeData` is set to true) ``` ---------------------------------------- TITLE: Upsert Vector Data using cURL DESCRIPTION: Demonstrates how to insert or update vector data into Upstash Vector using a cURL command. This example shows an upsert operation with an ID and a vector array, requiring an authorization token. SOURCE: https://upstash.com/docs/vector/api/get-started.mdx LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"id": "id-0", "vector": [0.87, 0.99]}' ``` ---------------------------------------- TITLE: Fetch vectors by ID (TypeScript) DESCRIPTION: Demonstrates fetching vectors using an array of specific IDs. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/fetch.mdx LANGUAGE: typescript CODE: ``` await index.fetch(["2", "3"]); // [{ id: "2" }, { id: "3" }] ``` ---------------------------------------- TITLE: Delete Single Vector by ID (TypeScript) DESCRIPTION: Shows how to delete a single vector by passing its ID directly to the index.delete method. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/delete.mdx LANGUAGE: typescript CODE: ``` const response = await index.delete("2"); // { deleted: 1 } ``` ---------------------------------------- TITLE: Example JSON Response for Vector Query with Metadata DESCRIPTION: This JSON snippet illustrates the structure of a typical response from an Upstash Vector query when `includeMetadata` is set to true. It shows the `id`, `score`, and the `metadata` object for each returned vector. SOURCE: https://upstash.com/docs/vector/features/metadata.mdx LANGUAGE: json CODE: ``` { "result": [ { "id": "id-0", "score": 1, "metadata": { "url": "https://imgur.com/z9AVZLb" } }, { "id": "id-3", "score": 0.99961007, "metadata": { "url": "https://imgur.com/zfOPmnI" } } ] } ``` ---------------------------------------- TITLE: Upsert a Single Vector in Upstash Vector (PHP) DESCRIPTION: Demonstrates how to upsert a single vector into an Upstash Vector index using the `upsert()` method. This includes examples for both a simple index and an index within a specific namespace. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/upsert-vectors.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; use function Upstash\Vector\createRandomVector; $index = new Index( url: "", token: "" ); $index->upsert(new VectorUpsert( id: '1', vector: createRandomVector(dimensions: 1536) )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; use function Upstash\Vector\createRandomVector; $index = new Index( url: "", token: "" ); $index->namespace('my-namespace')->upsert(new VectorUpsert( id: '1', vector: createRandomVector(dimensions: 1536) )); ``` ---------------------------------------- TITLE: Range Query with ID Prefix (TypeScript) DESCRIPTION: Illustrates how to use the `prefix` option with the `index.range` method to retrieve vectors whose IDs start with a specific string. The example includes the TypeScript code and the corresponding expected output. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/range.mdx LANGUAGE: typescript CODE: ``` const responseRange = await index.range({ cursor: 0, limit: 2, prefix: "test-", }); /* { nextCursor: '2', vectors: [ { id: 'test-1' }, { id: 'test-2' }, ] } */ ``` ---------------------------------------- TITLE: Fetch Vectors using cURL DESCRIPTION: Examples demonstrating how to fetch vectors using cURL, including fetching from the default namespace and a specified namespace. SOURCE: https://upstash.com/docs/vector/api/endpoints/fetch.mdx LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/fetch \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "ids": ["id-0"], "includeMetadata": true }' ``` LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/fetch/ns \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "ids": ["id-0", "id-1"], "includeMetadata": true }' ``` ---------------------------------------- TITLE: TypeScript Examples for Upstash Vector Index Reset DESCRIPTION: Examples demonstrating how to use the `index.reset()` method in TypeScript to clear vectors, both for the default namespace and all namespaces. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/reset.mdx LANGUAGE: typescript CODE: ``` const responseReset = await index.reset(); // 'Successful' ``` LANGUAGE: typescript CODE: ``` const responseReset = await index.reset({ all: true }); // 'Successful' ``` ---------------------------------------- TITLE: Range Method API Specification DESCRIPTION: Comprehensive API documentation for the `range` method, detailing its arguments, their types, descriptions, and the structure of the `RangeResponse` including nested vector properties. This method is used for paginated retrieval of vectors and is stateless, requiring all parameters in each request. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/range.mdx LANGUAGE: APIDOC CODE: ``` Range Method: Description: The range method is used to retrieve vectors in chunks with pagination. This method supports a variety of options to configure the query to your needs. The range command is stateless, meaning you need to pass all of the parameters in each subsequent request. Arguments: cursor: string | number (required) Description: The cursor to the last retrieved vector. Should be set to 0 in the initial range request. prefix: string Description: An string prefix to match vector IDs. All vectors with IDs that start with this prefix will be retrieved. limit: number (required) Description: The number of maximum vectors wanted in the response of range. (page size) includeMetadata: boolean Description: Whether to include the metadata of the vectors in the response. Setting this true would be the best practice, since it will make it easier to identify the vectors. includeVectors: boolean Description: Whether to include the vector themselves in the response. includeData: boolean Description: Whether to include the data field in the response. Namespace: { namespace?: string } Description: Namespace to call range for. If not set, default namespace is used. Response (RangeResponse): nextCursor: string | number (required) Description: Cursor to use in the next range request. vectors: Vector | Vector[] (required) Vector fields: id: string | number (required) Description: The ID of the vector vector: number[] Description: The vectors (if includeVectors is set to true) metadata: Record Description: The metadata of the vectors (if includeMetadata is set to true) data: string Description: The data of the vector (if includeData is set to true) ``` ---------------------------------------- TITLE: Fetch Vectors API Reference DESCRIPTION: Detailed documentation for the Fetch Vectors API, including endpoint, authentication, request body parameters, path parameters, and response structure. SOURCE: https://upstash.com/docs/vector/api/endpoints/fetch.mdx LANGUAGE: APIDOC CODE: ``` API Endpoint: GET https://{endpoint}/fetch/{namespace} Authentication: Bearer Token (via Authorization header) Request Body Parameters: ids: string[] Description: Array of vector ids to fetch. prefix: string Description: Prefix of vector ids to fetch. At most 1000 vectors will be returned. includeMetadata: boolean (default: false) Description: Whether to include the metadata of the vectors in the response, if any. Recommended to set to true. includeVectors: boolean (default: false) Description: Whether to include the vector values in the response. Recommended to set to false. includeData: boolean (default: false) Description: Whether to include the data of the vectors in the response, if any. Path Parameters: namespace: string (default: "") Description: The namespace to use. When no namespace is specified, the default namespace will be used. Response (200 OK): result: Array of Objects (Vectors) Description: Array of vectors in the same order they provided in the ids array. Array elements can be null if no such vector exists. Properties: id: string (required) Description: The id of the vector. vector: number[] Description: The dense vector value for dense and hybrid indexes. sparseVector: Object[] Description: The sparse vector value for sparse and hybrid indexes. Properties: indices: number[] Description: Indices of the non-zero valued dimensions. values: number[] Description: Values of the non-zero valued dimensions. metadata: Object Description: The metadata of the vector, if any. data: string Description: The unstructured data of the vector, if any. ``` ---------------------------------------- TITLE: Upstash Vector Range API Specification DESCRIPTION: Comprehensive documentation for the Upstash Vector Range API endpoint, detailing the HTTP method, authentication, request body parameters, path parameters, and the structure of the successful response. This specification outlines all fields, their types, requirements, and default values. SOURCE: https://upstash.com/docs/vector/api/endpoints/range.mdx LANGUAGE: APIDOC CODE: ``` GET https://{endpoint}/range/{namespace} Authentication: Bearer Token Request Body Parameters: cursor (string, required): The offset to the last retrieved vector. Should be set to "0" in the initial range. prefix (string, optional): Prefix of the vector IDs to range over. limit (number, required): The number of maximum vectors that you want in the response of range (page size). includeMetadata (boolean, default: false): Whether to include the metadata of the vectors in the response, if any. Recommended to set to true. includeVectors (boolean, default: false): Whether to include the vector values in the response. Recommended to set to false. includeData (boolean, default: false): Whether to include the data of the vectors in the response, if any. Path Parameters: namespace (string, default: ""): The namespace to use. When no namespace is specified, the default namespace will be used. Response Body: nextCursor (string, required): The offset for the next range. Place this in the `cursor` field for the next range. It will be an empty string if there are no other vectors to range. vectors (Object[], required): An array of vector objects. id (string, required): The ID of the vector. vector (number[]): The dense vector value for dense and hybrid indexes. sparseVector (Object[]): The sparse vector value for sparse and hybrid indexes. indices (number[]): Indices of the non-zero valued dimensions. values (number[]): Values of the non-zero valued dimensions. metadata (Object): The metadata of the vector, if any. data (string): The unstructured data of the vector, if any. ``` ---------------------------------------- TITLE: Initialize Upstash Vector Client with Configuration Object DESCRIPTION: Example of initializing the Upstash Vector client by passing a configuration object directly to the constructor, including url and token. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: typescript CODE: ``` import { Index } from "@upstash/vector"; const index = new Index({ url: "", token: "" }); ``` ---------------------------------------- TITLE: Python Example: Fetch Vector with Specific Namespace DESCRIPTION: Illustrates how to specify a namespace when fetching a vector, allowing operations within a particular data partition in the Upstash Vector index. When no namespace is provided, the default namespace will be used. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/fetch.mdx LANGUAGE: python CODE: ``` index.fetch("id-4", namespace="ns") ``` ---------------------------------------- TITLE: Update Existing Vector Metadata in Upstash Vector (PHP) DESCRIPTION: Shows how to update metadata for an existing vector in an Upstash Vector index using the `update()` method. This method specifically modifies existing data rather than overriding the entire vector. Examples include simple and namespaced updates. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/upsert-vectors.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpdate; use Upstash\Vector\Enums\UpdateMode; $index = new Index( url: "", token: "" ); $index->update(new VectorUpdate( id: '1', metadata: ['foo' => 'baz'], metadataUpdateMode: UpdateMode::OVERWRITE, )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpdate; use Upstash\Vector\Enums\UpdateMode; $index = new Index( url: "", token: "" ); $index->namespace('my-namespace')->update(new VectorUpdate( id: '1', metadata: ['foo' => 'baz'], metadataUpdateMode: UpdateMode::OVERWRITE, )); ``` ---------------------------------------- TITLE: Upstash Vector Index.resumable_query Method Parameters DESCRIPTION: Detailed documentation for the parameters accepted by the `resumable_query` method of the `Index` class, used to configure the query behavior including vector/data input, result inclusion options, filtering, and namespace. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/resumable-query.mdx LANGUAGE: APIDOC CODE: ``` Index.resumable_query( vector: list[float], sparse_vector: any, data: str, include_metadata: bool, include_vector: bool, include_data: bool, top_k: int, filter: any, namespace: str, weighting_strategy: any, fusion_algorithm: any, query_mode: any, max_idle: int ) vector: The reference vector for similarity comparison. sparse_vector: The sparse vector value to query. data: A string for text-based queries (mutually exclusive with vector). include_metadata: A boolean flag indicating whether to include metadata in the query results. include_vector: A boolean flag indicating whether to include vectors in the query results. include_data: A boolean flag indicating whether to include data in the query results. top_k: The number of top matching vectors to retrieve. filter: Metadata filtering of the vector is used to query your data based on the filters and narrow down the query results. namespace: The namespace to use. When not specified, the default namespace is used. weighting_strategy: Weighting strategy to be used for sparse vectors. fusion_algorithm: Fusion algorithm to use while fusing scores from hybrid vectors. query_mode: Query mode for hybrid indexes with Upstash-hosted embedding models. max_idle: The maximum idle time for the query in seconds. ``` ---------------------------------------- TITLE: Delete Vectors by IDs using cURL DESCRIPTION: Example cURL command to delete specific vectors by providing an array of their IDs. This request targets the default namespace. SOURCE: https://upstash.com/docs/vector/api/endpoints/delete.mdx LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/delete \ -X DELETE \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "ids": [ "id-0", "id-1" ] }' ``` ---------------------------------------- TITLE: Install LlamaParse and Upstash Vector Libraries DESCRIPTION: This command installs the necessary Python packages for LlamaParse, LlamaIndex, Upstash Vector integration, and environment variable management. SOURCE: https://upstash.com/docs/vector/integrations/llamaparse.mdx LANGUAGE: bash CODE: ``` pip install llama-index upstash-vector llama-index-vector-stores-upstash python-dotenv ``` ---------------------------------------- TITLE: Install LlamaIndex and Upstash Vector Dependencies DESCRIPTION: This command installs the necessary Python packages for LlamaIndex, Upstash Vector integration, and environment variable management using pip. SOURCE: https://upstash.com/docs/vector/integrations/llamaindex.mdx LANGUAGE: bash CODE: ``` pip install llama-index upstash-vector llama-index-vector-stores-upstash python-dotenv ``` ---------------------------------------- TITLE: Install Required Python Libraries DESCRIPTION: Installs `llama-index`, `upstash-vector`, `llama-index-vector-stores-upstash`, and `python-dotenv` using pip, which are necessary for document parsing and vector store integration. SOURCE: https://upstash.com/docs/vector/tutorials/llamaparse.mdx LANGUAGE: bash CODE: ``` pip install llama-index upstash-vector llama-index-vector-stores-upstash python-dotenv ``` ---------------------------------------- TITLE: Install Required Python Libraries DESCRIPTION: Install the necessary Python packages, including llama-index, upstash-vector, llama-index-vector-stores-upstash, and python-dotenv, using pip. SOURCE: https://upstash.com/docs/vector/tutorials/llamaindex.mdx LANGUAGE: bash CODE: ``` pip install llama-index upstash-vector llama-index-vector-stores-upstash python-dotenv ``` ---------------------------------------- TITLE: Upstash Vector Index Delete Method API Reference DESCRIPTION: Defines the parameters and return value for the `delete` method used to remove vectors from an Upstash Vector index. It specifies that only one of `ids`, `prefix`, or `filter` can be provided. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/delete.mdx LANGUAGE: APIDOC CODE: ``` Method: delete Parameters: ids: list[string] - A list of identifiers of vectors to be deleted. prefix: string - A string prefix to match vector IDs. All vectors with IDs that start with this prefix will be deleted. filter: string - A metadata filter to match vectors to be deleted. Constraints: Only one of `ids`, `prefix`, or `filter` can be provided. Returns: deleted: integer - An integer indicating how many vectors were deleted with the command. ``` ---------------------------------------- TITLE: Fetch Specific Vectors by ID using Upstash Vector in PHP DESCRIPTION: Demonstrates how to retrieve one or more specific vectors from an Upstash Vector database using their unique IDs. It shows both simple usage and usage within a namespace, with options to include metadata, vectors, and data in the results. The `fetch()` method returns a `Upstash\Vector\VectorFetchResult` object. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/fetch.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorFetch; $index = new Index( url: "", token: "", ); $results = $index->fetch(new VectorFetch( ids: ['1', '2'], includeMetadata: true, // (optional) if true the fetch results will contain metadata. includeVectors: true, // (optional) if true the fetch results will contain the indexed vectors. includeData: true, // (optional) if true the fetch results will contain the string data. )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorFetch; $index = new Index( url: "", token: "", ); $results = $index->namespace('my-namespace')->fetch(new VectorFetch( ids: ['1', '2'], includeMetadata: true, // (optional) if true the fetch results will contain metadata. includeVectors: true, // (optional) if true the fetch results will contain the indexed vectors. includeData: true, // (optional) if true the fetch results will contain the string data. )); ``` ---------------------------------------- TITLE: Fetch Vectors by ID Prefix using Upstash Vector in PHP DESCRIPTION: Illustrates how to retrieve all vectors whose IDs match a specified prefix from an Upstash Vector database. It covers both simple usage and usage within a namespace, with options to include metadata, vectors, and data in the results. The `fetch()` method returns a `Upstash\Vector\VectorFetchResult` object. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/fetch.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorFetchByPrefix; $index = new Index( url: "", token: "", ); $results = $index->fetch(new VectorFetchByPrefix( prefix: 'users:', includeMetadata: true, // (optional) if true the fetch results will contain metadata. includeVectors: true, // (optional) if true the fetch results will contain the indexed vectors. includeData: true, // (optional) if true the fetch results will contain the string data. )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorFetchByPrefix; $index = new Index( url: "", token: "", ); $results = $index->namespace('my-namespace')->fetch(new VectorFetchByPrefix( prefix: 'users:', includeMetadata: true, // (optional) if true the fetch results will contain metadata. includeVectors: true, // (optional) if true the fetch results will contain the indexed vectors. includeData: true, // (optional) if true the fetch results will contain the string data. )); ``` ---------------------------------------- TITLE: Override or Pass Command-Level Metadata Types in Upstash Vector DESCRIPTION: Explains how to override or pass specific metadata types directly to individual commands, such as `upsert`. This flexibility is beneficial when an index-level type is not defined, or when a particular operation requires a different metadata structure than the default index-level type. SOURCE: https://upstash.com/docs/vector/sdks/ts/getting-started.mdx LANGUAGE: typescript CODE: ``` import { Index } from "@upstash/vector"; type Metadata = { genre: string, year: number }; const index = new Index(); index.upsert({ id: 1, vector: [...], metadata: { genre: "comedy", year: 1990 }}); ``` ---------------------------------------- TITLE: Query Upstash Vector Index with Dense and Sparse Vectors DESCRIPTION: This snippet shows how to query an Upstash Vector hybrid index using both dense and sparse vectors. The query combines a dense vector with a sparse vector (defined by indices and values) to retrieve the top-k most relevant results, optionally including metadata. SOURCE: https://upstash.com/docs/vector/features/hybridindexes.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index from upstash_vector.types import SparseVector index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.query( vector=[0.5, 0.4], sparse_vector=SparseVector([3, 5], [0.3, 0.5]), top_k=5, include_metadata=True, ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.query({ vector: [0.5, 0.4], sparseVector: { indices: [2, 3], values: [0.13, 0.87], }, includeData: true, topK: 3, }) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) scores, err := index.Query(vector.Query{ Vector: []float32{0.5, 0.4}, SparseVector: &vector.SparseVector{ Indices: []int32{3, 5}, Values: []float32{0.3, 05}, }, TopK: 5, IncludeMetadata: true, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; use Upstash\Vector\SparseVector; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->query(new VectorQuery( vector: [0.5, 0.4], sparseVector: new SparseVector( indices: [3, 5], values: [0.3, 0.5], ), topK: 5, includeMetadata: true, )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"vector": [0.5, 0.4], "sparseVector": {"indices": [3, 5], "values": [0.3, 0.5]}, "topK": 5, "includeMetadata": true}' ``` ---------------------------------------- TITLE: Range Query with Improved Metadata Types (TypeScript) DESCRIPTION: Demonstrates how to define and apply a custom TypeScript type for metadata when calling the `index.range` method. This allows for type-safe access and interaction with metadata fields without explicit type casting, improving code readability and maintainability. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/range.mdx LANGUAGE: typescript CODE: ``` type Metadata = { title: string; genre: "sci-fi" | "fantasy" | "horror" | "action"; }; const responseRange = await index.range({ cursor: 0, limit: 2, includeMetadata: true, }); if (responseRange[0].metadata) { // Since we passed the Metadata type parameter above, // we can interact with metadata fields without having to // do any typecasting. const { title, genre } = results[0].metadata; console.log(`The best match in fantasy was ${title}`); } ``` ---------------------------------------- TITLE: Metadata Filter Syntax: Equals Operator DESCRIPTION: Example usage of the equals operator (=) in Upstash Vector's metadata filter syntax. This operator filters keys whose value is exactly equal to the given literal. It is applicable to string, number, and boolean values. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` country = 'Turkey' AND population = 15460000 AND is_capital = false ``` ---------------------------------------- TITLE: Delete a Single Vector with Namespace in Python DESCRIPTION: Illustrates how to delete a single vector within a specific namespace. If no namespace is provided, the default namespace will be used for the operation. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/delete.mdx LANGUAGE: python CODE: ``` index.delete("id-4", namespace="ns") ``` ---------------------------------------- TITLE: Reset Specific Upstash Vector Namespace (PHP) DESCRIPTION: Illustrates how to reset a named namespace within an Upstash Vector index using the PHP SDK. This targets and clears data only from the specified namespace. It requires an initialized `Index` object and chains the `namespace()` method before calling `reset()` to target a particular namespace. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/reset.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; $index = new Index( url: "", token: "", ); $index->namespace('my-namespace')->reset(); ``` ---------------------------------------- TITLE: Metadata Filter Syntax: Greater Than Operator DESCRIPTION: Example usage of the greater than operator (>) in Upstash Vector's metadata filter syntax. This operator filters keys whose value is greater than the given literal. It is applicable to number values. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` population > 10000000 OR geography.coordinates.latitude > 39.5 ``` ---------------------------------------- TITLE: Fetch Records from Upstash Vector Index by IDs DESCRIPTION: Example of fetching multiple records from an Upstash Vector index using an array of their unique IDs. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: typescript CODE: ``` const fetchResult = await index.fetch(["id-1", "id-2"]); ``` ---------------------------------------- TITLE: Upstash Vector Index Reset Method API Documentation DESCRIPTION: Detailed API documentation for the `index.reset()` method, including its arguments (`namespace`, `all`) and expected response. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/reset.mdx LANGUAGE: APIDOC CODE: ``` reset(options?: object): string Description: Clears all vectors and metadata from a particular namespace or all namespaces of an index. Arguments: namespace: string (optional) Description: Specifies a namespace to reset. Leave empty for the default namespace. all: true | undefined (optional) Description: Whether to reset all namespaces. Can only be set to `true`. Response: string Description: Returns 'Success' if the index is successfully resetted. ``` ---------------------------------------- TITLE: Install Core Dependencies for AI SDK and Upstash Vector DESCRIPTION: Installs essential npm packages required for the RAG chatbot, including @ai-sdk/openai for OpenAI integration, ai for AI SDK core functionalities, zod for schema validation, and @upstash/vector for Upstash Vector database interactions. This snippet provides commands for npm, pnpm, and bun. SOURCE: https://upstash.com/docs/vector/integrations/ai-sdk.mdx LANGUAGE: npm CODE: ``` npm install @ai-sdk/openai ai zod @upstash/vector ``` LANGUAGE: pnpm CODE: ``` pnpm install @ai-sdk/openai ai zod @upstash/vector ``` LANGUAGE: bun CODE: ``` bun install @ai-sdk/openai ai zod @upstash/vector ``` ---------------------------------------- TITLE: API Reference: Resumable Query Endpoint DESCRIPTION: Detailed API documentation for the POST /resumable-query/{namespace} endpoint, including authentication, request parameters (body and path), and the structure of the successful response. SOURCE: https://upstash.com/docs/vector/api/endpoints/resumable-query/start-with-vector.mdx LANGUAGE: APIDOC CODE: ``` APIDOC: Endpoint: POST https://{endpoint}/resumable-query/{namespace} Authentication: Bearer Token Description: Perform queries that can be resumed to fetch additional results. Request: Path Parameters: namespace (string, default: ""): The namespace to use. When no namespace is specified, the default namespace will be used. Body Parameters: vector (number[], required): The query vector. Note: The query vector should have the same dimensions as your index. topK (number, default: 10): The total number of the vectors that you want to receive as a query result. The response will be sorted based on the distance metric score, and at most `topK` many vectors will be returned. includeMetadata (boolean, default: false): Whether to include the metadata of the vectors in the response, if any. It is recommended to set this to `true` to easily identify vectors. includeVectors (boolean, default: false): Whether to include the vector values in the response. It is recommended to set this to `false` as the vector values can be quite big, and not needed most of the time. includeData (boolean, default: false): Whether to include the data of the vectors in the response, if any. filter (string, default: ""): Metadata filter to apply. maxIdle (number): Maximum idle time for the resumable query in seconds. weightingStrategy (string): For sparse vectors of sparse and hybrid indexes, specifies what kind of weighting strategy should be used while querying the matching non-zero dimension values of the query vector with the documents. If not provided, no weighting will be used. Only possible value is `IDF` (inverse document frequency). fusionAlgorithm (string): Fusion algorithm to use while fusing scores from dense and sparse components of a hybrid index. If not provided, defaults to `RRF` (Reciprocal Rank Fusion). Other possible value is `DBSF` (Distribution-Based Score Fusion). Response (200 OK): uuid (string, required): A unique identifier for the resumable query. scores (Object[]): id (string, required): The id of the vector. score (number, required): The similarity score of the vector, calculated based on the distance metric of your index. vector (number[]): The dense vector value for dense and hybrid indexes. sparseVector (Object[]): indices (number[]): Indices of the non-zero valued dimensions. values (number[]): Values of the non-zero valued dimensions. metadata (Object): The metadata of the vector, if any. data (string): The unstructured data of the vector, if any. ``` ---------------------------------------- TITLE: Fetch All Results Using Resumable Query Loop (Python) DESCRIPTION: Provides a complete example of how to iteratively fetch all available results from a resumable query using a `while` loop, ensuring all data is retrieved in batches and the query is properly stopped. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/resumable-query.mdx LANGUAGE: python CODE: ``` query = index.resumable_query( vector=[0.1, 0.2], top_k=2, include_metadata=True ) results = query.start() while True: next_batch = query.fetch_next(2) if not next_batch: break results.extend(next_batch) query.stop() ``` ---------------------------------------- TITLE: Install Upstash Vector Laravel SDK DESCRIPTION: Installs the `upstash/vector-laravel` package using Composer, adding it to your Laravel project's dependencies. SOURCE: https://upstash.com/docs/vector/sdks/php/laravel.mdx LANGUAGE: shell CODE: ``` composer require upstash/vector-laravel ``` ---------------------------------------- TITLE: Delete Vectors by IDs in a Specific Namespace using cURL DESCRIPTION: Example cURL command to delete specific vectors by providing an array of their IDs within a specified namespace ('ns'). SOURCE: https://upstash.com/docs/vector/api/endpoints/delete.mdx LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/delete/ns \ -X DELETE \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "ids": [ "id-0", "id-1" ] }' ``` ---------------------------------------- TITLE: Fetch Next Batch of Resumable Query Results DESCRIPTION: This snippet shows how to use the handle obtained from an initial resumable query to fetch subsequent batches of results. It demonstrates calling the 'fetch_next' (or equivalent) method multiple times to paginate through the remaining similar vectors until the entire index is iterated. SOURCE: https://upstash.com/docs/vector/features/resumablequery.mdx LANGUAGE: python CODE: ``` # next batch of the results next_result = handle.fetch_next( additional_k=3, ) for r in next_result: print(r) # it is possible to call fetch_next more than once next_result = handle.fetch_next( additional_k=5, ) for r in next_result: print(r) ``` LANGUAGE: javascript CODE: ``` // next batch of the results let nextResult = await fetchNext(3); for (let r of nextResult) { console.log(r); } // it is possible to call fetch_next more than once nextResult = await fetchNext(3); for (let r of nextResult) { console.log(r); } ``` LANGUAGE: go CODE: ``` // next batch of the results scores, err = handle.Next(vector.ResumableQueryNext{ AdditionalK: 3, }) if err != nil { log.Fatal(err) } for _, score := range scores { fmt.Printf("%+v\n", score) } // it is possible to call Next more than once scores, err = handle.Next(vector.ResumableQueryNext{ AdditionalK: 5, }) if err != nil { log.Fatal(err) } for _, score := range scores { fmt.Printf("%+v\n", score) } ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/resumable-query-next \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "uuid": "550e8400-e29b-41d4-a716-446655440000", "additionalK": 3 }' ``` ---------------------------------------- TITLE: Delete Vectors by ID Prefix (TypeScript) DESCRIPTION: Illustrates deleting multiple vectors whose IDs start with a specific prefix using the delete method's prefix option. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/delete.mdx LANGUAGE: typescript CODE: ``` const response = await index.delete({ prefix: "article_", }); // { deleted: 3 } ``` ---------------------------------------- TITLE: API Reference: POST /resumable-query-data/{namespace} DESCRIPTION: Detailed API specification for performing resumable queries. This includes the endpoint URL, required authentication method, and a comprehensive list of all request body parameters (e.g., `data`, `topK`, `includeMetadata`, `maxIdle`, `weightingStrategy`, `fusionAlgorithm`, `queryMode`) and path parameters (`namespace`), along with their types, descriptions, and default values. SOURCE: https://upstash.com/docs/vector/api/endpoints/resumable-query/start-with-data.mdx LANGUAGE: APIDOC CODE: ``` Endpoint: POST https://{endpoint}/resumable-query-data/{namespace} Authentication: bearer Request Body Parameters: - data (string, required): The text data to be embedded and used for querying. - topK (number, default: 10): The total number of the vectors that you want to receive as a query result. The response will be sorted based on the distance metric score, and at most `topK` many vectors will be returned. - includeMetadata (boolean, default: false): Whether to include the metadata of the vectors in the response, if any. It is recommended to set this to `true` to easily identify vectors. - includeVectors (boolean, default: false): Whether to include the vector values in the response. It is recommended to set this to `false` as the vector values can be quite big, and not needed most of the time. - includeData (boolean, default: false): Whether to include the data of the vectors in the response, if any. - filter (string, default: ""): [Metadata filter](/vector/features/filtering) to apply. - maxIdle (number): Maximum idle time for the resumable query in seconds. - weightingStrategy (string): For sparse vectors of sparse and hybrid indexes, specifies what kind of weighting strategy should be used while querying the matching non-zero dimension values of the query vector with the documents. If not provided, no weighting will be used. Only possible value is `IDF` (inverse document frequency). - fusionAlgorithm (string): Fusion algorithm to use while fusing scores from dense and sparse components of a hybrid index. If not provided, defaults to `RRF` (Reciprocal Rank Fusion). Other possible value is `DBSF` (Distribution-Based Score Fusion). - queryMode (string): Query mode for hybrid indexes with Upstash-hosted embedding models. Specifies whether to run the query in only the dense index, only the sparse index, or in both. If not provided, defaults to `HYBRID`. Possible values are `HYBRID`, `DENSE`, and `SPARSE`. Path Parameters: - namespace (string, default: ""): The namespace to use. When no namespace is specified, the default namespace will be used. Response: Same as Resumable Query. ``` ---------------------------------------- TITLE: Define UpstashRecord Type for Vector Data DESCRIPTION: Defines the UpstashRecord TypeScript type, specifying the expected structure for records inserted into Upstash Vector indexes, including id, vector, and optional metadata. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: typescript CODE: ``` type UpstashRecord = { id: number | string; vector: number[]; metadata?: Record; }; ``` ---------------------------------------- TITLE: Integrate LlamaIndex with Upstash Vector for Document Indexing DESCRIPTION: This Python code demonstrates how to initialize the Upstash Vector store, load documents from a specified directory, and create a LlamaIndex vector store index for Retrieval-Augmented Generation (RAG) applications. SOURCE: https://upstash.com/docs/vector/integrations/llamaindex.mdx LANGUAGE: python CODE: ``` from llama_index.core import VectorStoreIndex, SimpleDirectoryReader from llama_index.vector_stores.upstash import UpstashVectorStore from llama_index.core import StorageContext import os from dotenv import load_dotenv # Load environment variables load_dotenv() # Set OpenAI API key openai.api_key = os.environ["OPENAI_API_KEY"] # Initialize Upstash Vector store upstash_vector_store = UpstashVectorStore( url=os.environ["UPSTASH_VECTOR_REST_URL"], token=os.environ["UPSTASH_VECTOR_REST_TOKEN"], ) # Load documents using SimpleDirectoryReader documents = SimpleDirectoryReader("./documents/").load_data() # Create a storage context and initialize the index storage_context = StorageContext.from_defaults(vector_store=upstash_vector_store) index = VectorStoreIndex.from_documents( documents, storage_context=storage_context ) ``` ---------------------------------------- TITLE: Python Example: Fetch Vectors by ID Prefix DESCRIPTION: Demonstrates fetching all vectors whose IDs start with a given prefix. A warning is included, advising the use of the paginated `range` command for larger datasets to prevent timeouts. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/fetch.mdx LANGUAGE: python CODE: ``` index.fetch(prefix="id-") ``` ---------------------------------------- TITLE: Calculate Cosine Similarity Score DESCRIPTION: Formula for normalizing the cosine similarity between two vectors (v1, v2) to a score between 0 and 1. A score of 1 indicates the highest similarity, and 0 indicates the lowest. SOURCE: https://upstash.com/docs/vector/features/similarityfunctions.mdx LANGUAGE: Formula CODE: ``` (1 + cosine_similarity(v1, v2)) / 2; ``` ---------------------------------------- TITLE: Example: Perform Resumable Query using cURL DESCRIPTION: Demonstrates how to send a POST request to the resumable query endpoint using cURL, with and without specifying a namespace. Includes common body parameters like vector, topK, includeMetadata, and maxIdle. SOURCE: https://upstash.com/docs/vector/api/endpoints/resumable-query/start-with-vector.mdx LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/resumable-query \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "vector": [0.1, 0.2], "topK": 2, "includeMetadata": true, "maxIdle": 3600 }' ``` LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/resumable-query/ns \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "vector": [0.1, 0.2], "topK": 2, "includeMetadata": true, "maxIdle": 3600 }' ``` ---------------------------------------- TITLE: Upsert Sparse Vectors in Upstash Vector (PHP) DESCRIPTION: Explains how to upsert sparse vectors into an Upstash Vector index. Sparse vectors require specifying indices and their corresponding values. Examples are provided for both simple and namespaced sparse vector upserts. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/upsert-vectors.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; use Upstash\Vector\SparseVector; $index = new Index( url: "", token: "" ); $index->upsert(new VectorUpsert( id: '1', sparseVector: new SparseVector( indices: [0, 1], values: [1.0, 2.0], ), )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; use Upstash\Vector\SparseVector; $index = new Index( url: "", token: "" ); $index->namespace('my-namespace')->upsert(new VectorUpsert( id: '1', sparseVector: new SparseVector( indices: [0, 1], values: [1.0, 2.0], ), )); ``` ---------------------------------------- TITLE: Upsert Hybrid Vectors in PHP DESCRIPTION: Demonstrates how to upsert a hybrid vector (combining dense and sparse components) into an Upstash Vector index using PHP. It shows both a direct upsert and an upsert within a specified namespace. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/upsert-vectors.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; use Upstash\Vector\SparseVector; use function Upstash\Vector\createRandomVector; $index = new Index( url: "", token: "", ); $index->upsert(new VectorUpsert( id: '1', vector: createRandomVector(dimensions: 1536), sparseVector: new SparseVector( indices: [0, 1], values: [1.0, 2.0], ), )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; use Upstash\Vector\SparseVector; use function Upstash\Vector\createRandomVector; $index = new Index( url: "", token: "", ); $index->namespace('my-namespace')->upsert(new VectorUpsert( id: '1', vector: createRandomVector(dimensions: 1536), sparseVector: new SparseVector( indices: [0, 1], values: [1.0, 2.0], ), )); ``` ---------------------------------------- TITLE: Parse Documents using LlamaParse DESCRIPTION: This Python code demonstrates how to initialize LlamaParse and use it with SimpleDirectoryReader to extract structured content from a document, such as a text file. SOURCE: https://upstash.com/docs/vector/integrations/llamaparse.mdx LANGUAGE: python CODE: ``` from llama_parse import LlamaParse from llama_index.core import SimpleDirectoryReader # Initialize the parser parser = LlamaParse(result_type="markdown") # Parse a document file_extractor = {".txt": parser} documents = SimpleDirectoryReader( input_files=["./documents/global_warming.txt"], file_extractor=file_extractor ).load_data() ``` ---------------------------------------- TITLE: Upstash Vector Index Info API Endpoint Documentation DESCRIPTION: Comprehensive documentation for the GET /info endpoint of the Upstash Vector API, detailing its purpose, authentication method, request parameters, and the full structure of the expected JSON response, including nested objects and their fields. SOURCE: https://upstash.com/docs/vector/api/endpoints/info.mdx LANGUAGE: APIDOC CODE: ``` API Endpoint: GET https://{endpoint}/info Authentication: Bearer Token Description: Returns some information about the index. Info will be updated eventually, so it might take some time to see the effect of changes in this endpoint. Request: No additional data required. Response Fields: vectorCount: number (required) Description: The number of vectors in the index, that are ready to use. This is the total number of vectors across all namespaces. pendingVectorCount: number (required) Description: The number of vectors in the index, that are still processing and not ready to use. This is the total number of pending vectors across all namespaces. indexSize: number (required) Description: The total size of the index, in bytes. dimension: number (required) Description: Dimension of the vectors. similarityFunction: string (required) Description: Name of the similarity function used in indexing and queries. indexType: string (required) Description: Type of the index. Possible values: "DENSE", "SPARSE", "HYBRID" denseIndex: object Description: Information about the dense vector index configuration. Fields: dimension: number (required) Description: Dimension of the dense vectors. similarityFunction: string (required) Description: Similarity function used for dense vector comparisons. Possible values: "COSINE", "EUCLIDEAN", "DOT_PRODUCT" embeddingModel: string (required) Description: Name of the embedding model used for dense vectors. sparseIndex: object Description: Information about the sparse vector index configuration. Fields: embeddingModel: string (required) Description: Name of the embedding model used for sparse vectors. namespaces: object (required) Description: Map of namespace names to namespace. Every index has at least one namespace called default namespace, whose name is the empty string "". Fields: vectorCount: number (required) Description: The number of vectors in the namespace, that are ready to use. pendingVectorCount: number (required) Description: The number of vectors in the namespace, that are still processing and not ready to use. ``` ---------------------------------------- TITLE: Upstash Vector QueryOptions Type Definition DESCRIPTION: Defines the QueryOptions type for the Upstash Vector client's query method, detailing parameters like vector, topK, includeVectors, and includeMetadata. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: APIDOC CODE: ``` type QueryOptions = { vector: number[]; topK: number; includeVectors?: boolean; includeMetadata?: boolean; }; ``` ---------------------------------------- TITLE: Initialize UpstashVectorStore with OpenAI Embeddings DESCRIPTION: This snippet shows how to configure the `UpstashVectorStore` to use OpenAI's embedding models. By setting the `embedding` parameter to an instance of `OpenAIEmbeddings`, the vector store will leverage OpenAI's capabilities for generating vector representations of documents. SOURCE: https://upstash.com/docs/vector/tutorials/langchain.mdx LANGUAGE: python CODE: ``` from langchain_openai import OpenAIEmbeddings store = UpstashVectorStore(embedding=OpenAIEmbeddings()) ``` ---------------------------------------- TITLE: Upstash Vector Query API Successful Response (JSON) DESCRIPTION: Example of a successful JSON response from the Upstash Vector Query API, showing returned vector IDs, scores, and optional metadata for a query. SOURCE: https://upstash.com/docs/vector/api/endpoints/query.mdx LANGUAGE: json CODE: ``` { "result": [ { "id": "id-0", "score": 1.0, "metadata": { "link": "upstash.com" } }, { "id": "id-1", "score": 0.99996454 } ] } ``` ---------------------------------------- TITLE: Example JSON Response for Fetch Vectors DESCRIPTION: An example of a successful JSON response when fetching vectors, showing the structure of returned vector data including ID and optional metadata. SOURCE: https://upstash.com/docs/vector/api/endpoints/fetch.mdx LANGUAGE: json CODE: ``` { "result": [ { "id": "id-0", "metadata": { "link": "upstash.com" } }, { "id": "id-1" } ] } ``` ---------------------------------------- TITLE: Configure Upstash Vector API Credentials in .env DESCRIPTION: This snippet demonstrates how to create a `.env` file to securely store Upstash Vector API credentials. These environment variables (`UPSTASH_VECTOR_REST_URL` and `UPSTASH_VECTOR_REST_TOKEN`) are essential for the application to connect and interact with the Upstash Vector database. SOURCE: https://upstash.com/docs/vector/tutorials/gradio-application.mdx LANGUAGE: bash CODE: ``` UPSTASH_VECTOR_REST_URL=your_upstash_url UPSTASH_VECTOR_REST_TOKEN=your_upstash_token ``` ---------------------------------------- TITLE: Perform Semantic Search by Text Query DESCRIPTION: This Python code demonstrates how to perform a semantic similarity search in the Upstash Vector store using a text query. It first embeds the query text using the Hugging Face model and then retrieves the top `k` most semantically similar documents based on the generated embedding. SOURCE: https://upstash.com/docs/vector/tutorials/huggingface-embeddings.mdx LANGUAGE: python CODE: ``` query_text = "What are the effects of global warming?" query_embedding = embeddings.embed_query(query_text) result_text_query = vector_store.similarity_search( query=query_text, k=5 ) print("Results for text-based similarity search:") for res in result_text_query: print(res.page_content) ``` ---------------------------------------- TITLE: Start Resumable Query to Fetch Initial Results (Python) DESCRIPTION: Illustrates how to initiate a resumable query to retrieve the first batch of results, showing both synchronous (`query.start()`) and asynchronous (`await query.async_start()`) approaches. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/resumable-query.mdx LANGUAGE: python CODE: ``` initial_results = query.start() ``` LANGUAGE: python CODE: ``` initial_results = await query.async_start() ``` ---------------------------------------- TITLE: Delete Vectors by ID Prefix in Python DESCRIPTION: Shows how to delete all vectors whose IDs start with a specified prefix. This method is useful for batch deletion of related vectors identified by a common prefix. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/delete.mdx LANGUAGE: python CODE: ``` index.delete(prefix="id-") ``` ---------------------------------------- TITLE: Fetch Additional Results from Resumable Query (Python) DESCRIPTION: Shows how to retrieve subsequent batches of results from an active resumable query using `fetch_next()` for synchronous operations and `async_fetch_next()` for asynchronous ones. Returns an empty list if no more results are available. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/resumable-query.mdx LANGUAGE: python CODE: ``` next_results = query.fetch_next(number_of_results) ``` LANGUAGE: python CODE: ``` next_results = await query.async_fetch_next(number_of_results) ``` ---------------------------------------- TITLE: Perform Resumable Query with Data Input DESCRIPTION: Illustrates how to perform a resumable query by providing raw data as input instead of a vector. This example also covers fetching subsequent results using `fetchNext` and stopping the query to manage resources. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/resumable-query.mdx LANGUAGE: typescript CODE: ``` const { result, fetchNext, stop } = await index.resumableQuery({ maxIdle: 3600, topK: 50, data: "lord of the rings", includeMetadata: true, includeData: true, }); console.log(result); /* [ { id: '6345', score: 1.00000012, data: "hobbit", metadata: { sentence: "Upstash is great." } }, // ... more results ] */ const nextBatch = await fetchNext(5); // Fetch next 5 results console.log(nextBatch); await stop(); // Stop the resumable query ``` ---------------------------------------- TITLE: Query Upstash Vector with Text Data for Custom Reranking DESCRIPTION: Illustrates how to query a hybrid Upstash Vector index using text data, specifying `DENSE` or `SPARSE` query modes. This enables separate retrieval of dense and sparse components for custom reranking, particularly useful when using Upstash-hosted embedding models that handle text embedding internally. SOURCE: https://upstash.com/docs/vector/features/hybridindexes.mdx LANGUAGE: Python CODE: ``` from upstash_vector import Index from upstash_vector.types import SparseVector, QueryMode index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) dense_results = index.query( data="Upstash Vector", query_mode=QueryMode.DENSE, ) sparse_results = index.query( data="Upstash Vector", query_mode=QueryMode.SPARSE, ) # Rerank dense and sparse results as you like here ``` LANGUAGE: JavaScript CODE: ``` import { Index, QueryMode } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) const denseResults = await index.query({ data: "Upstash Vector", queryMode: QueryMode.DENSE, }) const sparseResults = await index.query({ data: "Upstash Vector", queryMode: QueryMode.SPARSE, }) // Rerank dense and sparse results as you like here ``` LANGUAGE: Go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) denseScores, err := index.QueryData(vector.QueryData{ Data: "Upstash Vector", QueryMode: vector.QueryModeDense, }) sparseScores, err := index.QueryData(vector.QueryData{ Data: "Upstash Vector", QueryMode: vector.QueryModeSparse, }) // Rerank dense and sparse results as you like here } ``` LANGUAGE: PHP CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataQuery; use Upstash\Vector\Enums\QueryMode; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $denseResults = $index->queryData(new DataQuery( data: 'Upstash Vector', topK: 3, queryMode: QueryMode::DENSE, )); $sparseResults = $index->queryData(new DataQuery( data: 'Upstash Vector', topK: 3, queryMode: QueryMode::SPARSE, )); // Rerank dense and sparse results as you like here ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query-data \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"data": "Upstash Vector", "queryMode": "DENSE"}' curl $UPSTASH_VECTOR_REST_URL/query-data \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"data": "Upstash Vector", "queryMode": "SPARSE"}' ``` ---------------------------------------- TITLE: Stop Resumable Query and Release Resources (Python) DESCRIPTION: Demonstrates how to properly terminate a resumable query to release server-side resources, covering both synchronous (`query.stop()`) and asynchronous (`await query.async_stop()`) methods. It's crucial to stop queries when done. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/resumable-query.mdx LANGUAGE: python CODE: ``` stop_result = query.stop() assert stop_result == 'Success' ``` LANGUAGE: python CODE: ``` stop_result = await query.async_stop() assert stop_result == 'Success' ``` ---------------------------------------- TITLE: Parse Document with LlamaParse DESCRIPTION: Initializes the LlamaParse parser to process a document (e.g., `global_warming.txt`) into markdown format. It then uses `SimpleDirectoryReader` to load and parse the specified file. SOURCE: https://upstash.com/docs/vector/tutorials/llamaparse.mdx LANGUAGE: python CODE: ``` from llama_parse import LlamaParse from llama_index.core import SimpleDirectoryReader # Initialize the LlamaParse parser with the desired result format parser = LlamaParse(result_type="markdown") # "markdown" and "text" are available # Parse the document using the parser file_extractor = {".txt": parser} documents = SimpleDirectoryReader(input_files=["./documents/global_warming.txt"], file_extractor=file_extractor).load_data() ``` ---------------------------------------- TITLE: Configure Environment Variables for Upstash Vector and Llama Cloud DESCRIPTION: This snippet shows the required environment variables for connecting to Upstash Vector and authenticating with Llama Cloud, to be placed in a .env file. SOURCE: https://upstash.com/docs/vector/integrations/llamaparse.mdx LANGUAGE: bash CODE: ``` UPSTASH_VECTOR_REST_URL=your_upstash_url UPSTASH_VECTOR_REST_TOKEN=your_upstash_token LLAMA_CLOUD_API_KEY=your_llama_cloud_api_key ``` ---------------------------------------- TITLE: Upsert Dense and Sparse Vectors into Upstash Vector Index DESCRIPTION: This code demonstrates how to upsert both dense and sparse vectors into an Upstash Vector index. It shows examples across multiple programming languages and a `curl` command, highlighting the requirement to provide both vector types for hybrid indexes. SOURCE: https://upstash.com/docs/vector/features/hybridindexes.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index, Vector from upstash_vector.types import SparseVector index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.upsert( vectors=[ Vector(id="id-0", vector=[0.1, 0.5], sparse_vector=SparseVector([1, 2], [0.1, 0.2])), Vector(id="id-1", vector=[0.3, 0.7], sparse_vector=SparseVector([123, 44232], [0.5, 0.4])), ] ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.upsert([ { id: 'id-0', vector: [0.1, 0.5], sparseVector: { indices: [2, 3], values: [0.13, 0.87], }, }]) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) err := index.UpsertMany([]vector.Upsert{ { Id: "id-0", Vector: []float32{0.1, 0.5}, SparseVector: &vector.SparseVector{ Indices: []int32{1, 2}, Values: []float32{0.1, 0.2}, }, }, { Id: "id-1", Vector: []float32{0.3, 0.7}, SparseVector: &vector.SparseVector{ Indices: []int32{123, 44232}, Values: []float32{0.5, 0.4}, }, }, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; use Upstash\Vector\SparseVector; use function Upstash\Vector\createRandomVector; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->upsert(new VectorUpsert( id: 'id-0', vector: createRandomVector(384), sparseVector: new SparseVector( indices: [1, 2, 3], values: [5, 6, 7], ), )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '[ {"id": "id-0", "vector": [0.1, 0.5], "sparseVector": {"indices": [1, 2], "values": [0.1, 0.2]}}, {"id": "id-1", "vector": [0.3, 0.7], "sparseVector": {"indices": [123, 44232], "values": [0.5, 0.4]}} ]' ``` ---------------------------------------- TITLE: Update Vector with cURL Examples DESCRIPTION: Examples demonstrating how to update vectors, data, or metadata using cURL commands, including specifying metadata, vector values, and namespaces. SOURCE: https://upstash.com/docs/vector/api/endpoints/update.mdx LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/update \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "id": "id-1", "metadata": { "link": "upstash.com" } }' ``` LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/update/ns \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "id": "id-2", "vector": [0.1, 0.2] }' ``` ---------------------------------------- TITLE: Successful Vector Deletion JSON Response DESCRIPTION: Example JSON response indicating the successful deletion of vectors, showing the count of deleted items. This is a typical 200 OK response. SOURCE: https://upstash.com/docs/vector/api/endpoints/delete.mdx LANGUAGE: json CODE: ``` { "result": { "deleted": 2 } } ``` ---------------------------------------- TITLE: Query Upstash Vector Hybrid Index DESCRIPTION: Shows how to query a hybrid vector index using the Upstash Vector SDK's `query()` method. It provides examples for both direct index querying and querying within a namespace, combining `vector` for dense components and `sparseVector` for sparse components. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/query.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; use Upstash\Vector\SparseVector; $index = new Index( url: "", token: "", ); $results = $index->query(new VectorQuery( vector: [0.1, 0.2, ...], sparseVector: new SparseVector( indices: [1, 2, 3], values: [5.0, 6.0, 7.0], ), topK: 15, )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; use Upstash\Vector\SparseVector; $index = new Index( url: "", token: "", ); $results = $index->namespace('my-namespace')->query(new VectorQuery( vector: [0.1, 0.2, ...], sparseVector: new SparseVector( indices: [1, 2, 3], values: [5.0, 6.0, 7.0], ), topK: 15, )); ``` ---------------------------------------- TITLE: Define Metadata Type for Upstash Vector Index DESCRIPTION: Defines a TypeScript type Metadata for structured metadata to be stored with vector values, enabling type-checking. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: typescript CODE: ``` type Metadata = { title: string, genre: 'sci-fi' | 'fantasy' | 'horror' | 'action' } ``` ---------------------------------------- TITLE: Query Upstash Vector with Text Data DESCRIPTION: This section illustrates how to query an Upstash Vector index directly with text data. If the sparse index was created with an Upstash-hosted sparse embedding model, Upstash automatically embeds the text behind the scenes before performing the query. This simplifies the query process by abstracting the embedding step. SOURCE: https://upstash.com/docs/vector/features/sparseindexes.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.query( data="Upstash Vector", top_k=5, ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.query( { data: "Upstash Vector", topK: 1, }, ) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) scores, err := index.QueryData(vector.QueryData{ Data: "Upstash Vector", TopK: 5, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\DataQuery; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->queryData(new DataQuery( data: 'Upstash Vector', topK: 5, )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query-data \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"data": "Upstash Vector", "topK": 5}' ``` ---------------------------------------- TITLE: Query Upstash Vector Sparse Index DESCRIPTION: Illustrates how to query a sparse vector index using the Upstash Vector SDK's `query()` method. It includes examples for direct index querying and querying within a namespace, specifically using `sparseVector` with `indices` and `values`. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/query.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; use Upstash\Vector\SparseVector; $index = new Index( url: "", token: "", ); $results = $index->query(new VectorQuery( sparseVector: new SparseVector( indices: [1, 2, 3], values: [5.0, 6.0, 7.0], ), topK: 15, )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; use Upstash\Vector\SparseVector; $index = new Index( url: "", token: "", ); $results = $index->namespace('my-namespace')->query(new VectorQuery( sparseVector: new SparseVector( indices: [1, 2, 3], values: [5.0, 6.0, 7.0], ), topK: 15, )); ``` ---------------------------------------- TITLE: Stop an Upstash Vector Resumable Query DESCRIPTION: This snippet demonstrates how to explicitly terminate an active resumable query using language-specific handles or a direct REST API call. Stopping queries is essential to manage server resources and adhere to active query limits. SOURCE: https://upstash.com/docs/vector/features/resumablequery.mdx LANGUAGE: python CODE: ``` handle.stop() ``` LANGUAGE: javascript CODE: ``` await stop(); ``` LANGUAGE: go CODE: ``` handle.Close() ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/resumable-query-end \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "uuid": "550e8400-e29b-41d4-a716-446655440000" }' ``` ---------------------------------------- TITLE: Filter Strings Using SQL NOT GLOB Operator DESCRIPTION: Illustrates the `NOT GLOB` operator, which filters string values that do not match a given UNIX glob pattern. It shares properties with the `GLOB` operator, including case sensitivity and wildcard support. The example shows how to filter city names that do not start with 'A'. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` city NOT GLOB 'A*' ``` ---------------------------------------- TITLE: cURL Examples for Fetching Random Vector DESCRIPTION: Examples demonstrating how to make cURL requests to the random vector endpoint, showing usage both without and with a specified namespace. SOURCE: https://upstash.com/docs/vector/api/endpoints/fetch-random.mdx LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/random \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" ``` LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/random/ns \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" ``` ---------------------------------------- TITLE: Customize Retry Behavior for Upstash Vector Index (Python) DESCRIPTION: This Python code snippet demonstrates how to initialize the `upstash-vector` Index with custom retry settings. It shows how to configure the SDK to attempt requests 5 times, with a 2-second interval between each retry, overriding the default mechanism. SOURCE: https://upstash.com/docs/vector/sdks/py/features.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index # Try 5 times with a 2-second interval between retries index = Index.from_env(retries=5, retry_interval=2.0) ``` ---------------------------------------- TITLE: Upsert Records with Metadata Typing in Upstash Vector DESCRIPTION: Example of upserting a record into an Upstash Vector index, demonstrating how to include structured metadata that conforms to a defined TypeScript type. SOURCE: https://upstash.com/docs/vector/sdks/ts/overview-backup.mdx LANGUAGE: typescript CODE: ``` await index.upsert([{ id: '1234', vector: [ .... // embedding values ], metadata: { title: 'Lord of The Rings', genre: 'drama', category: 'classic' } }]) ``` ---------------------------------------- TITLE: Query Upstash Vector with Sparse Vectors DESCRIPTION: This section demonstrates how to query an Upstash Vector index using sparse vectors. Sparse vectors are represented by two arrays: one for non-zero dimension indices (int32) and another for their corresponding float32 values. The system uses an inner product similarity metric, considering only matching non-zero dimensions between the query and indexed vectors. SOURCE: https://upstash.com/docs/vector/features/sparseindexes.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index from upstash_vector.types import SparseVector index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.query( sparse_vector=SparseVector([3, 5], [0.3, 0.5]), top_k=5, include_metadata=True, ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.query( { sparseVector: { indices: [2, 3], values: [0.13, 0.87], }, includeData: true, topK: 3, }, ) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) scores, err := index.Query(vector.Query{ SparseVector: &vector.SparseVector{ Indices: []int32{3, 5}, Values: []float32{0.3, 05}, }, TopK: 5, IncludeMetadata: true, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; use Upstash\Vector\SparseVector; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->query(new VectorQuery( sparseVector: new SparseVector( indices: [3, 5], values: [0.3, 0.5], ), topK: 5, includeMetadata: true, )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"sparseVector": {"indices": [3, 5], "values": [0.3, 0.5]}, "topK": 5, "includeMetadata": true}' ``` ---------------------------------------- TITLE: Upsert Sparse Vectors with Upstash Vector DESCRIPTION: This snippet demonstrates how to upsert sparse vectors into an Upstash Vector index. Sparse vectors are represented by two arrays: one for non-zero dimension indices (signed 32-bit integers) and another for their corresponding 32-bit float values. It's important to note that sparse vectors are limited to 1,000 non-zero valued dimensions. SOURCE: https://upstash.com/docs/vector/features/sparseindexes.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index, Vector from upstash_vector.types import SparseVector index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.upsert( vectors=[ Vector(id="id-0", sparse_vector=SparseVector([1, 2], [0.1, 0.2])), Vector(id="id-1", sparse_vector=SparseVector([123, 44232], [0.5, 0.4])), ] ) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.upsert([{ id: 'id-0', sparseVector: { indices: [2, 3], values: [0.13, 0.87], }, }]) ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) err := index.UpsertMany([]vector.Upsert{ { Id: "id-0", SparseVector: &vector.SparseVector{ Indices: []int32{1, 2}, Values: []float32{0.1, 0.2}, }, }, { Id: "id-1", SparseVector: &vector.SparseVector{ Indices: []int32{123, 44232}, Values: []float32{0.5, 0.4}, }, }, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpsert; use Upstash\Vector\SparseVector; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->upsertMany([ new VectorUpsert( id: 'id-0', sparseVector: new SparseVector( indices: [1, 2], values: [0.1, 0.2], ), ), new VectorUpsert( id: 'id-1', sparseVector: new SparseVector( indices: [123, 44232], values: [0.5, 0.4], ), ), ]); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '[ {"id": "id-0", "sparseVector": {"indices": [1, 2], "values": [0.1, 0.2]}}, {"id": "id-1", "sparseVector": {"indices": [123, 44232], "values": [0.5, 0.4]}} ]' ``` ---------------------------------------- TITLE: Initiate a Resumable Vector Query DESCRIPTION: This snippet demonstrates how to start a resumable query with Upstash Vector. You provide an initial vector, a 'topK' value for the first batch, and options like 'includeMetadata'. The server responds with the first batch of results and a handle to continue the query. SOURCE: https://upstash.com/docs/vector/features/resumablequery.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) result, handle = index.resumable_query( vector=[0.1, 0.2], top_k=2, include_metadata=True, ) # first batch of the results for r in result: print(r) ``` LANGUAGE: javascript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }); const { result, fetchNext, stop } = await index.resumableQuery({ vector: [0.1, 0.2], topK: 2, includeMetadata: true, }); // first batch of the results for (let r of result) { console.log(r); } ``` LANGUAGE: go CODE: ``` package main import ( "fmt" "log" "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) scores, handle, err := index.ResumableQuery(vector.ResumableQuery{ Vector: []float32{0.1, 0.2}, TopK: 2, IncludeMetadata: true, }) if err != nil { log.Fatal(err) } defer handle.Close() // first batch of the results for _, score := range scores { fmt.Printf("%+v\n", score) } } ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/resumable-query \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "vector": [0.1, 0.2], "topK": 2, "includeMetadata": true }' ``` ---------------------------------------- TITLE: Filter Nested Objects Using SQL Dot Accessor DESCRIPTION: Explains how to filter keys within nested objects by referencing them using the `.` accessor. This allows filtering at arbitrary depths within JSON-like structures, enabling precise targeting of data within complex objects. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` economy.currency != 'USD' AND geography.coordinates.latitude >= 35.0 ``` ---------------------------------------- TITLE: Manually Initialize Upstash Vector Client (PHP) DESCRIPTION: This PHP example shows how to manually initialize the Upstash Vector client by passing the REST URL and Token directly to the `Index` constructor. This method is useful for applications that need to interact with multiple Upstash Vector projects or manage configurations dynamically. SOURCE: https://upstash.com/docs/vector/sdks/php/getting-started.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; $index = new Index( url: "", token: "" ); ``` ---------------------------------------- TITLE: Retrieve Upstash Vector Index Info using cURL DESCRIPTION: An example cURL command demonstrating how to make an authenticated GET request to the Upstash Vector `/info` endpoint to retrieve index information. This command requires the `UPSTASH_VECTOR_REST_URL` and `UPSTASH_VECTOR_REST_TOKEN` environment variables to be set. SOURCE: https://upstash.com/docs/vector/api/endpoints/info.mdx LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/info \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" ``` ---------------------------------------- TITLE: Metadata Filter Syntax: Less Than Operator DESCRIPTION: Example usage of the less than operator (<) in Upstash Vector's metadata filter syntax. This operator filters keys whose value is less than the given literal. It is applicable to number values. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` population < 20000000 OR geography.coordinates.longitude < 30.0 ``` ---------------------------------------- TITLE: Add Sample Documents to Upstash Vector Store with Embeddings DESCRIPTION: This Python snippet creates a list of sample `Document` objects. It then uses the `add_documents` method of the `UpstashVectorStore` to embed these documents using the configured Hugging Face model and store them in the Upstash Vector database. Parameters like `batch_size` and `embedding_chunk_size` are used for efficient processing. SOURCE: https://upstash.com/docs/vector/tutorials/gradio-application.mdx LANGUAGE: python CODE: ``` # Sample documents to embed and store documents = [ Document(page_content="Global warming is causing sea levels to rise."), Document(page_content="AI is transforming many industries."), Document(page_content="Renewable energy is vital for sustainable development.") ] vector_store.add_documents(documents=documents, batch_size=100, embedding_chunk_size=200) ``` ---------------------------------------- TITLE: Delete Vectors by Metadata Filter in PHP DESCRIPTION: This snippet demonstrates how to delete vectors based on a metadata filter query. It uses the `VectorDeleteByMetadataFilter` class to specify deletion criteria, with examples for both direct index and namespaced operations. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/delete-vectors.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorDeleteByMetadataFilter; $index = new Index( url: "", token: "", ); $index->delete(new VectorDeleteByMetadataFilter( filter: 'salary > 1000', )); ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorDeleteByMetadataFilter; $index = new Index( url: "", token: "", ); $index->namespace('my-namespace')->delete(new VectorDeleteByMetadataFilter( filter: 'salary > 1000', )); ``` ---------------------------------------- TITLE: Retrieve Index Information with TypeScript DESCRIPTION: Demonstrates how to call the `index.info()` method using TypeScript to retrieve index statistics. The example illustrates the expected JSON response structure, including details for both the default and a named namespace. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/info.mdx LANGUAGE: typescript CODE: ``` const infoResponse = await index.info(); /* { vectorCount: 17, pendingVectorCount: 0, indexSize: 551158, dimension: 1536, similarityFunction: "COSINE", namespaces: { "": { // default namespace vectorCount: 10, pendingVectorCount: 0, }, "my-namespace": { vectorCount: 7, pendingVectorCount: 0, } } }*/ ``` ---------------------------------------- TITLE: Metadata Filter Syntax: Less Than or Equals Operator DESCRIPTION: Example usage of the less than or equals operator (<=) in Upstash Vector's metadata filter syntax. This operator filters keys whose value is less than or equal to the given literal. It is applicable to number values. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` population <= 20000000 OR geography.coordinates.longitude <= 30.0 ``` ---------------------------------------- TITLE: Metadata Filter Syntax: Not Equals Operator DESCRIPTION: Example usage of the not equals operator (!=) in Upstash Vector's metadata filter syntax. This operator filters keys whose value is not equal to the given literal. It is applicable to string, number, and boolean values. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` country != 'Germany' AND population != 12500000 AND is_capital != true ``` ---------------------------------------- TITLE: Filter Values Using SQL NOT IN Operator DESCRIPTION: Describes the `NOT IN` operator, which filters keys whose value does not match any of the given literals. It applies to string, number, and boolean values. The first example filters currencies that are not 'USD' or 'EUR'. The second code block demonstrates its semantic equivalence using multiple not-equals operators combined with `AND`. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` economy.currency NOT IN ('USD', 'EUR') ``` LANGUAGE: SQL CODE: ``` economy.currency != 'USD' AND economy.currency != 'EUR' ``` ---------------------------------------- TITLE: Calculate Dot Product Similarity Score DESCRIPTION: Formula for normalizing the dot product between two vectors (v1, v2) to a score between 0 and 1. For this formula to be accurate, the vectors must first be normalized to be of unit length. A score of 1 indicates the highest similarity, and 0 indicates the lowest. SOURCE: https://upstash.com/docs/vector/features/similarityfunctions.mdx LANGUAGE: Formula CODE: ``` (1 + dot_product(v1, v2)) / 2 ``` ---------------------------------------- TITLE: Install Upstash Vector SDK DESCRIPTION: Instructions for installing the `@upstash/vector` SDK using popular Node.js package managers. This is the essential first step to integrate the SDK into your project. SOURCE: https://upstash.com/docs/vector/sdks/ts/getting-started.mdx LANGUAGE: shell npm CODE: ``` npm install @upstash/vector ``` LANGUAGE: shell pnpm CODE: ``` pnpm add @upstash/vector ``` ---------------------------------------- TITLE: Fetch Upstash Vector Index Information (PHP) DESCRIPTION: This snippet demonstrates how to initialize the Upstash Vector Index client and fetch general information about the index using the `getInfo()` method. It requires a REST URL and token for authentication. SOURCE: https://upstash.com/docs/vector/sdks/php/commands/info.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Index; $index = new Index( url: "", token: "", ); $info = $index->getInfo(); ``` ---------------------------------------- TITLE: Index Information Response Fields DESCRIPTION: Defines the structure and types of the response object returned when querying index statistics. It includes details such as total vector counts, pending vectors, index size, vector dimension, similarity function, and a map of namespaces with their respective vector counts. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/info.mdx LANGUAGE: APIDOC CODE: ``` Response Object: vectorCount: number (required) - The total number of vectors in the index, that are ready to use. pendingVectorCount: number (required) - The number of vectors in the index, that is still processing and not ready to use. indexSize: number (required) - The size of the index, in `b`. dimension: number (required) - Dimension of the vectors. similarityFunction: string (required) - Name of the similarity function used in indexing and queries. namespaces: Record (required) - A map of namespaces to their information. Each namespace object contains: vectorCount: number (required) - The total number of vectors in the index, that are ready to use. pendingVectorCount: number (required) - The number of vectors in the index, that is still processing and not ready to use. ``` ---------------------------------------- TITLE: Install and Start Flowise Locally DESCRIPTION: Instructions to install Flowise globally using npm and then start the Flowise application, making it accessible via a local web interface. SOURCE: https://upstash.com/docs/vector/integrations/flowise.mdx LANGUAGE: bash CODE: ``` npm install -g flowise ``` LANGUAGE: bash CODE: ``` npx flowise start ``` ---------------------------------------- TITLE: List Namespaces with Upstash Vector SDKs and cURL DESCRIPTION: This snippet demonstrates how to retrieve a list of all active namespaces associated with your Upstash Vector index. It covers implementations using the official SDKs for Python, JavaScript, Go, and PHP, as well as a direct cURL command for API interaction. Authentication is handled via a REST URL and token. SOURCE: https://upstash.com/docs/vector/features/namespaces.mdx LANGUAGE: Python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.list_namespaces() ``` LANGUAGE: JavaScript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.listNamespaces() ``` LANGUAGE: Go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex("UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN") index.ListNamespaces() } ``` LANGUAGE: PHP CODE: ``` use Upstash\Vector\Index; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->listNamespaces(); ``` LANGUAGE: Shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/list-namespaces \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" ``` ---------------------------------------- TITLE: Fetch vectors by ID prefix (TypeScript) DESCRIPTION: Shows how to fetch vectors using a common ID prefix, useful for grouping. SOURCE: https://upstash.com/docs/vector/sdks/ts/commands/fetch.mdx LANGUAGE: typescript CODE: ``` await index.fetch({ prefix: "test-" }); // [{ id: "test-1" }, { id: "test-2" }, { id: "test-3" }] ``` ---------------------------------------- TITLE: Upstash Vector REST API HTTP Response Codes DESCRIPTION: Documents the standard HTTP status codes returned by the Upstash Vector REST API, along with their descriptions and common scenarios for each code. SOURCE: https://upstash.com/docs/vector/api/get-started.mdx LANGUAGE: APIDOC CODE: ``` HTTP Status Codes: 200 OK: When request is accepted and successfully executed. 400 Bad Request: When there's a syntax error, an invalid/unsupported command is sent or command execution fails. 401 Unauthorized: When authentication fails; auth token is missing or invalid. 405 Method Not Allowed: When an unsupported HTTP method is used. Only HEAD, GET, POST, and PUT methods are allowed. ``` ---------------------------------------- TITLE: Python Example: Get Index Statistics DESCRIPTION: Demonstrates how to use the `info` method of the `upstash_vector.Index` class to retrieve and display statistical information about an index, including total vector counts, index size, dimensions, similarity function, and per-namespace statistics. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/info.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index.from_env() from upstash_vector import Index index = Index.from_env() # Get statistical information about the index info_result = index.info() # Display the info result print("Vector Count:", info_result.vector_count) print("Pending Vector Count:", info_result.pending_vector_count) print("Index Size:", info_result.index_size) print("Dimension:", info_result.dimension) print("Similarity Function:", info_result.similarity_function) for ns, ns_info in info_result.namespaces.items(): print("Namespace:", ns, "Vector Count:", ns_info.vector_count) print("Namespace:", ns, "Pending Vector Count:", ns_info.pending_vector_count) ``` ---------------------------------------- TITLE: Check for Field Presence Using SQL HAS FIELD Operator DESCRIPTION: Explains the `HAS FIELD` operator, used to filter keys that possess a specified JSON field. This operator is useful for checking the existence of a field within nested objects, regardless of its value. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` HAS FIELD geography.coordinates ``` ---------------------------------------- TITLE: Upstash Vector Index fetch Method API Documentation DESCRIPTION: Documents the `fetch` method of the Upstash Vector Index, detailing its input parameters (`ids`, `prefix`, `include_vectors`, `include_metadata`, `include_data`, `namespace`) and the structure of the returned `vectors` field, including `id`, `vector`, `sparse_vector`, `metadata`, and `data`. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/fetch.mdx LANGUAGE: APIDOC CODE: ``` fetch( ids: Union[str, List[str]] = None, prefix: str = None, include_vectors: bool = False, include_metadata: bool = False, include_data: bool = False, namespace: str = None ) -> List[VectorInfo] Parameters: ids (Union[str, List[str]], optional): A string or a list of strings representing the identifiers of the vectors to be fetched. prefix (str, optional): A string prefix to match vector IDs. All vectors with IDs that start with this prefix will be retrieved. include_vectors (bool, optional): A boolean flag indicating whether to include vectors in the fetch results. include_metadata (bool, optional): A boolean flag indicating whether to include metadata in the fetch results. include_data (bool, optional): A boolean flag indicating whether to include data in the fetch results. namespace (str, optional): The namespace to use. When not specified, the default namespace is used. Returns: List[VectorInfo]: A list containing information for each fetched vector. VectorInfo fields: id (str) vector (List[float]) sparse_vector (Dict[str, float]) metadata (Dict[str, Any]) data (Any) ``` ---------------------------------------- TITLE: Python Example: Fetch a Singular Vector by ID DESCRIPTION: Shows a concise way to fetch a single vector from the Upstash Vector index using its ID. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/fetch.mdx LANGUAGE: python CODE: ``` index.fetch("id-4") ``` ---------------------------------------- TITLE: Filter Array Elements by Index Using SQL Bracket Accessor DESCRIPTION: Demonstrates filtering individual array elements using the `[]` accessor with zero-based indexing. It also shows how to index from the end of the array using the `#` character with negative values (e.g., `[#-1]` references the last element), providing flexibility for array access. SOURCE: https://upstash.com/docs/vector/features/filtering.mdx LANGUAGE: SQL CODE: ``` economy.major_industries[0] = 'Tourism' ``` LANGUAGE: SQL CODE: ``` economy.major_industries[#-1] = 'Finance' ``` ---------------------------------------- TITLE: Reset Default Namespace (Python) DESCRIPTION: This example demonstrates how to clear all vectors and metadata from the default namespace of an Upstash Vector index. It initializes the index from environment variables and then calls the `reset()` method without any arguments. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/reset.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index.from_env() index.reset() ``` ---------------------------------------- TITLE: Reset Specific Namespace (Python) DESCRIPTION: This example illustrates how to clear vectors and metadata from a specified namespace within an Upstash Vector index. It initializes the index and then calls the `reset()` method, passing the target namespace name via the `namespace` argument. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/reset.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index.from_env() index.reset(namespace="ns") ``` ---------------------------------------- TITLE: Configure Multiple Upstash Vector Connections DESCRIPTION: Demonstrates how to add multiple Upstash Vector index connections within the `config/vector.php` file, allowing an application to interact with different vector databases. SOURCE: https://upstash.com/docs/vector/sdks/php/laravel.mdx LANGUAGE: php CODE: ``` return [ 'default' => env('UPSTASH_VECTOR_CONNECTION', 'default'), 'connections' => [ 'default' => [ 'url' => env('UPSTASH_VECTOR_REST_URL'), 'token' => env('UPSTASH_VECTOR_REST_TOKEN'), ], 'another' => [ 'url' => env('SECOND_UPSTASH_VECTOR_REST_URL'), 'token' => env('SECOND_UPSTASH_VECTOR_REST_TOKEN'), ] ] ]; ``` ---------------------------------------- TITLE: Delete Namespace REST API Specification DESCRIPTION: Detailed specification of the DELETE endpoint for removing a namespace from an Upstash Vector index, including path parameters, response structure, authentication method, and important notes regarding default namespaces. SOURCE: https://upstash.com/docs/vector/api/endpoints/delete-namespace.mdx LANGUAGE: APIDOC CODE: ``` API Endpoint: DELETE https://{endpoint}/delete-namespace/{namespace} Authentication: bearer Path Parameters: namespace (string, required): The namespace to delete. Request Body: None Response Fields: result (string): Returns "Success" string. Notes: The default namespace, which is the empty string "", cannot be deleted. ``` ---------------------------------------- TITLE: Delete a Specific Namespace DESCRIPTION: This snippet illustrates how to explicitly delete an existing namespace using the `delete_namespace` method. It initializes an Upstash Vector Index and then calls the deletion method for the target namespace. SOURCE: https://upstash.com/docs/vector/features/namespaces.mdx LANGUAGE: Python CODE: ``` from upstash_vector import Index index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.delete_namespace("ns") ``` LANGUAGE: JavaScript CODE: ``` import { Index } from "@upstash/vector" const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }) await index.deleteNamespace("ns") ``` LANGUAGE: Go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex("UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN") namespace := index.Namespace("ns") namespace.DeleteNamespace() } ``` LANGUAGE: PHP CODE: ``` use Upstash\Vector\Index; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->namespace('ns')->deleteNamespace(); ``` LANGUAGE: curl CODE: ``` curl $UPSTASH_VECTOR_REST_URL/delete-namespace/ns \ -X DELETE \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" ``` ---------------------------------------- TITLE: Upsert Single Vector with Namespace (cURL) DESCRIPTION: Example cURL command to upsert a single vector into a specified namespace ('ns') using a JSON object in the request body. SOURCE: https://upstash.com/docs/vector/api/endpoints/upsert.mdx LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/upsert/ns \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "id": "id-2", "vector": [0.1, 0.2], "metadata": { "link": "upstash.com" } }' ``` ---------------------------------------- TITLE: Specify Namespace for Range Query in Python DESCRIPTION: Demonstrates how to perform a `range` query within a specific namespace. This allows operations on isolated subsets of data within the same index, providing logical separation for different data categories. SOURCE: https://upstash.com/docs/vector/sdks/py/example_calls/range.mdx LANGUAGE: python CODE: ``` index.range(..., namespace="ns") ``` ---------------------------------------- TITLE: Configure Upstash Vector Client Environment Variables DESCRIPTION: These environment variables, `UPSTASH_VECTOR_REST_URL` and `UPSTASH_VECTOR_REST_TOKEN`, are used to provide the necessary credentials for the Upstash Vector client. Setting them allows the client to be initialized automatically from the environment. SOURCE: https://upstash.com/docs/vector/sdks/php/getting-started.mdx LANGUAGE: bash CODE: ``` UPSTASH_VECTOR_REST_URL="your_rest_url" UPSTASH_VECTOR_REST_TOKEN="your_rest_token" ``` ---------------------------------------- TITLE: Access Specific Upstash Vector Connection via Facade DESCRIPTION: Shows how to select and use a specific named Upstash Vector connection (e.g., 'another') configured in `config/vector.php` using the `Vector` facade. SOURCE: https://upstash.com/docs/vector/sdks/php/laravel.mdx LANGUAGE: php CODE: ``` use Upstash\Vector\Laravel\Facades\Vector; Vector::connection('another')->getInfo(); ``` ---------------------------------------- TITLE: Perform Hybrid Search with Reciprocal Rank Fusion (RRF) DESCRIPTION: Illustrates how to explicitly set Reciprocal Rank Fusion (RRF) as the fusion algorithm for hybrid queries in Upstash Vector. RRF combines results from dense and sparse indexes based on their rank, not their scores, using a specific formula. This method requires both dense vector and sparse vector inputs for the query. SOURCE: https://upstash.com/docs/vector/features/hybridindexes.mdx LANGUAGE: python CODE: ``` from upstash_vector import Index from upstash_vector.types import FusionAlgorithm, SparseVector index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.query( vector=[0.5, 0.4], sparse_vector=SparseVector([3, 5], [0.3, 0.5]), fusion_algorithm=FusionAlgorithm.RRF, ) ``` LANGUAGE: javascript CODE: ``` import { FusionAlgorithm, Index } from "@upstash/vector"; const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }); await index.query({ vector: [0.5, 0.4], sparseVector: { indices: [2, 3], values: [0.13, 0.87], }, fusionAlgorithm: FusionAlgorithm.RRF, topK: 3, }); ``` LANGUAGE: go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) scores, err := index.Query(vector.Query{ Vector: []float32{0.5, 0.4}, SparseVector: &vector.SparseVector{ Indices: []int32{3, 5}, Values: []float32{0.3, 05}, }, FusionAlgorithm: vector.FusionAlgorithmRRF, }) } ``` LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; use Upstash\Vector\SparseVector; use Upstash\Vector\Enums\FusionAlgorithm; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->query(new VectorQuery( vector: [0.5, 0.4], sparseVector: new SparseVector( indices: [3, 5], values: [0.3, 0.5], ), topK: 5, includeMetadata: true, fusionAlgorithm: FusionAlgorithm::RECIPROCAL_RANK_FUSION, )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"vector": [0.5, 0.4], "sparseVector": {"indices": [3, 5], "values": [0.3, 0.5]}, "fusionAlgorithm": "RRF"}' ``` ---------------------------------------- TITLE: Perform Hybrid Index Query with Distribution-Based Score Fusion (DBSF) DESCRIPTION: Demonstrates how to use the Upstash Vector client to perform a hybrid index query using the Distribution-Based Score Fusion (DBSF) algorithm. This method normalizes scores based on their distribution and sums them for results appearing in both dense and sparse indexes, providing a more sensitive fusion than RRF. SOURCE: https://upstash.com/docs/vector/features/hybridindexes.mdx LANGUAGE: Python CODE: ``` from upstash_vector import Index from upstash_vector.types import FusionAlgorithm, SparseVector index = Index( url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN", ) index.query( vector=[0.5, 0.4], sparse_vector=SparseVector([3, 5], [0.3, 0.5]), fusion_algorithm=FusionAlgorithm.DBSF, ) ``` LANGUAGE: JavaScript CODE: ``` import { FusionAlgorithm, Index } from "@upstash/vector"; const index = new Index({ url: "UPSTASH_VECTOR_REST_URL", token: "UPSTASH_VECTOR_REST_TOKEN", }); await index.query({ vector: [0.5, 0.4], sparseVector: { indices: [2, 3], values: [0.13, 0.87], }, fusionAlgorithm: FusionAlgorithm.DBSF, topK: 3, }); ``` LANGUAGE: Go CODE: ``` package main import ( "github.com/upstash/vector-go" ) func main() { index := vector.NewIndex( "UPSTASH_VECTOR_REST_URL", "UPSTASH_VECTOR_REST_TOKEN", ) scores, err := index.Query(vector.Query{ Vector: []float32{0.5, 0.4}, SparseVector: &vector.SparseVector{ Indices: []int32{3, 5}, Values: []float32{0.3, 05}, }, FusionAlgorithm: vector.FusionAlgorithmDBSF, }) } ``` LANGUAGE: PHP CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; use Upstash\Vector\SparseVector; use Upstash\Vector\Enums\FusionAlgorithm; $index = new Index( url: 'UPSTASH_VECTOR_REST_URL', token: 'UPSTASH_VECTOR_REST_TOKEN', ); $index->query(new VectorQuery( vector: [0.5, 0.4], sparseVector: new SparseVector( indices: [3, 5], values: [0.3, 0.5], ), topK: 5, includeMetadata: true, fusionAlgorithm: FusionAlgorithm::DISTRIBUTION_BASED_SCORE_FUSION, )); ``` LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{"vector": [0.5, 0.4], "sparseVector": {"indices": [3, 5], "values": [0.3, 0.5]}, "fusionAlgorithm": "DBSF"}' ```