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", }, }, ]); ```