TITLE: Implementing Semantic Search with Upstash Vector in Python DESCRIPTION: Complete Python implementation demonstrating document indexing and semantic search using Upstash Vector. Includes environment setup, document insertion with automatic embedding generation, and similarity search functionality. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/semantic_search.mdx#_snippet_2 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: Server Action for Resource Creation DESCRIPTION: Implementation of a server action to create new resources and upsert them to the vector index with input validation using Zod. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/ai-sdk.mdx#_snippet_4 LANGUAGE: typescript CODE: ``` 'use server' import { z } from 'zod' import { upsertEmbeddings } from '@/lib/ai/upstashVector' const NewResourceSchema = z.object({ content: z.string().min(1), }) export async function createResource(input: { content: string }) { const { content } = NewResourceSchema.parse(input) const resourceId = crypto.randomUUID() await upsertEmbeddings(resourceId, content) return `Resource ${resourceId} created and embedded.` } ``` ---------------------------------------- TITLE: Batch Querying Multiple Vectors in Python with Upstash DESCRIPTION: This example shows how to perform multiple vector queries in a single API call using the query_many method. It executes two different queries with different parameters and filtering criteria, reducing round trips to the server. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/query.mdx#_snippet_1 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: Performing Semantic Search DESCRIPTION: Code to execute a similarity search query against the vector store, returning the top 5 most relevant results. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/langchain.mdx#_snippet_5 LANGUAGE: python CODE: ``` result = store.similarity_search("Technology's role in global warming.", k=5) print(result) ``` ---------------------------------------- TITLE: Implementing LangChain with Upstash Vector Store DESCRIPTION: Python implementation showing how to initialize Upstash Vector Store, add documents, and perform similarity search using LangChain. The code demonstrates document creation, vector store initialization with embedding enabled, and executing semantic searches. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/langchain.mdx#_snippet_1 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: Querying Upstash Vector Index DESCRIPTION: Shows how to perform a similarity search on an Upstash Vector Index using various programming languages. The example queries for the top 3 most similar vectors to [0.6, 0.8], including metadata in the results. SOURCE: https://github.com/upstash/docs/blob/main/vector/overall/getstarted.mdx#_snippet_1 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: '<UPSTASH_VECTOR_REST_URL>', token: '<UPSTASH_VECTOR_REST_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: Starting a Resumable Query in JavaScript DESCRIPTION: Shows how to initiate a resumable vector query using the Upstash Vector JavaScript SDK. The code creates an index instance and performs the initial query, returning results along with functions to fetch more results or stop the query. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_1 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); } ``` ---------------------------------------- TITLE: Document Querying Implementation DESCRIPTION: Complete implementation for setting up Upstash Vector Store, creating an index, and querying parsed documents using OpenAI LLM. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/llamaparse.mdx#_snippet_4 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: Upserting Vectors with Data and Metadata DESCRIPTION: Demonstrates how to upsert vectors with both data and metadata fields. Shows implementation across multiple languages with examples of setting different combinations of vector values, metadata, and data fields. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_22 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", }, ]) ``` ---------------------------------------- TITLE: Querying Vector Index with Direct Vector Input in TypeScript DESCRIPTION: Demonstrates how to query a vector index by directly providing a vector array. Includes options for retrieving metadata and vectors in a specific namespace. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/query.mdx#_snippet_0 LANGUAGE: typescript CODE: ``` await index.query({ topK: 2, vector: [ ... ], includeMetadata: true, includeVectors: true }, { namespace: "my-namespace" }) ``` ---------------------------------------- TITLE: Querying with Reciprocal Rank Fusion in Python DESCRIPTION: Example of using RRF fusion algorithm for querying a hybrid index in Python. It demonstrates how to create an Index object and perform a query with both dense and sparse vectors. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/hybridindexes.mdx#_snippet_2 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, ) ``` ---------------------------------------- TITLE: Querying Vector Index with Text Data in TypeScript DESCRIPTION: Shows how to query the vector index using text data with metadata filtering. The example demonstrates searching for a movie description with specific genre and title filters. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/query.mdx#_snippet_1 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'" }); ``` ---------------------------------------- TITLE: Custom OpenAI Embedding Model Implementation DESCRIPTION: Implementation using OpenAI's text-embedding-ada-002 model for custom vector embeddings, including chunking and querying functionality. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/ai-sdk.mdx#_snippet_3 LANGUAGE: typescript CODE: ``` import { Index } from '@upstash/vector' import { embed, embedMany } from 'ai' import { openai } from '@ai-sdk/openai' const index = new Index({ url: process.env.UPSTASH_VECTOR_REST_URL!, token: process.env.UPSTASH_VECTOR_REST_TOKEN!, }) function generateChunks(input: string): string[] { return input .trim() .split('.') .filter(i => i !== '') } const embeddingModel = openai.embedding('text-embedding-ada-002') async function generateEmbedding(value: string): Promise<number[]> { const input = value.replaceAll('\\n', ' ') const { embedding } = await embed({ model: embeddingModel, value: input, }) return embedding } async function generateEmbeddings(value: string): Promise<Array<{ content: string; embedding: number[] }>> { const chunks = generateChunks(value) const { embeddings } = await embedMany({ model: embeddingModel, values: chunks, }) return embeddings.map((vector, i) => ({ content: chunks[i], embedding: vector, })) } export async function upsertEmbeddings(resourceId: string, content: string) { const chunkEmbeddings = await generateEmbeddings(content) const toUpsert = chunkEmbeddings.map((chunk, i) => ({ id: `${resourceId}-${i}`, vector: chunk.embedding, metadata: { resourceId, content: chunk.content, }, })) await index.upsert(toUpsert) } 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: Inserting Documents into Vector Store DESCRIPTION: Code to add processed documents to the Upstash Vector index. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/langchain.mdx#_snippet_4 LANGUAGE: python CODE: ``` inserted_vectors = store.add_documents(docs) ``` ---------------------------------------- TITLE: Querying the Vector Index DESCRIPTION: Example showing how to create a query engine and perform queries against the vector index to retrieve information from documents. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/llamaindex.mdx#_snippet_3 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: Upserting Vectors with Metadata in JavaScript DESCRIPTION: Code snippet showing how to upsert a vector with metadata in JavaScript. It initializes an Index object from the @upstash/vector package and adds a vector with URL metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_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", }, }) ``` ---------------------------------------- TITLE: LlamaIndex and Upstash Vector Setup DESCRIPTION: Initialization of LlamaIndex with Upstash Vector store, including document loading and index creation with 1536 dimensions SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/llamaindex.mdx#_snippet_2 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: Querying Vectors with Metadata Filtering in Python DESCRIPTION: Python implementation for querying Upstash Vector with metadata filtering. This example filters vectors by population size and geographic continent. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/filtering.mdx#_snippet_1 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 ) ``` ---------------------------------------- TITLE: Querying Upstash Vector Index with Dense and Sparse Modes in JavaScript DESCRIPTION: This snippet shows how to initialize an Upstash Vector index and perform both dense and sparse queries using JavaScript. It utilizes the @upstash/vector package and demonstrates setting up the index, executing queries, and preparing for result reranking. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/hybridindexes.mdx#_snippet_6 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 ``` ---------------------------------------- TITLE: Metadata Filtering in Vector Queries DESCRIPTION: Demonstrates how to filter vector query results based on metadata values using conditional expressions. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/query.mdx#_snippet_4 LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorQuery; $index = new Index( url: "<UPSTASH_VECTOR_REST_URL>", token: "<UPSTASH_VECTOR_REST_TOKEN>", ); $results = $index->query(new VectorQuery( vector: [0.1, 0.2, ...], topK: 15, filter: "country = 'PT' AND continent = 'EU'" )); ``` ---------------------------------------- TITLE: Querying Vectors with Metadata Filtering using cURL DESCRIPTION: A cURL command example for querying Upstash Vector with metadata filtering. This example filters vectors by population size and geographic continent. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/filtering.mdx#_snippet_5 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: Upserting Vectors Using Tuples in Python DESCRIPTION: This snippet shows how to upsert vectors into an Upstash Vector index using tuples. Each tuple contains an ID, vector array, metadata dictionary, and unstructured data. The approach is more compact than using Vector objects but provides the same functionality. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/upsert.mdx#_snippet_1 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: Basic Vector Query using cURL DESCRIPTION: Example of a basic vector query request using cURL, demonstrating how to query vectors with metadata inclusion and topK parameter specification. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/query.mdx#_snippet_0 LANGUAGE: sh 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: Querying Upstash Vector Index with Dense and Sparse Modes in Go DESCRIPTION: This Go code demonstrates how to create an Upstash Vector index and perform dense and sparse queries. It uses the github.com/upstash/vector-go package, showing index initialization and query execution for both query modes. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/hybridindexes.mdx#_snippet_7 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 } ``` ---------------------------------------- TITLE: Using Namespaces for Vector Operations DESCRIPTION: Demonstrates how to perform upsert and query operations within a specific namespace. The code initializes an index connection and executes vector operations in the 'ns' namespace. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/namespaces.mdx#_snippet_0 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, }) ``` ---------------------------------------- TITLE: Implementing Resumable Query with Metadata Filtering in TypeScript DESCRIPTION: This code snippet illustrates how to use the resumableQuery method with metadata filtering and type-safe metadata handling. It shows setting up a metadata type, applying a filter, and accessing typed metadata fields. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/resumable-query.mdx#_snippet_2 LANGUAGE: typescript CODE: ``` type Metadata = { title: string, genre: 'sci-fi' | 'fantasy' | 'horror' | 'action' } const { result, fetchNext, stop } = await index.resumableQuery<Metadata>({ 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: Single Vector Upsert Operation in TypeScript DESCRIPTION: Demonstrates how to upsert a single vector with metadata into the vector database. The vector must match the index's dimension count. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/upsert.mdx#_snippet_0 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: Specifying Namespace for Vector Queries in Python with Upstash DESCRIPTION: This snippet demonstrates how to specify a namespace when querying vectors from an Upstash Vector index. When no namespace is provided, the default namespace is used. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/query.mdx#_snippet_2 LANGUAGE: python CODE: ``` index.query(..., namespace="ns") ``` ---------------------------------------- TITLE: Vector-Based Similarity Search DESCRIPTION: Implementation of similarity search using direct vector query instead of text. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/huggingface-embeddings.mdx#_snippet_5 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: Querying with Embedding Models in PHP DESCRIPTION: Shows how to query vector indexes using string data that gets automatically converted to vector embeddings using configured embedding models. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/query.mdx#_snippet_3 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: 'What is the capital of France?', topK: 1, includeData: true, )); ``` ---------------------------------------- TITLE: Single Data Upsert Operation in TypeScript DESCRIPTION: Demonstrates upserting a single text data entry that will be converted to a vector embedding, including metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/upsert.mdx#_snippet_4 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: Complete Resumable Query Example in Python DESCRIPTION: A full example showing how to create an index, upsert vectors, perform a resumable query, and handle the results. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/resumable-query.mdx#_snippet_10 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: Querying with Metadata Filtering in JavaScript DESCRIPTION: Code snippet showing how to query vectors with metadata filtering in JavaScript. It uses a filter expression to select vectors whose URL metadata matches the pattern '*imgur.com*'. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_12 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*'", }) ``` ---------------------------------------- TITLE: Starting a Resumable Query in Go DESCRIPTION: Demonstrates how to initiate a resumable vector query using the Upstash Vector Go SDK. The code creates an index and performs the initial query, returning scores, a handle for subsequent requests, and handles any potential errors. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_2 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) } } ``` ---------------------------------------- TITLE: Querying Vectors by Similarity in Python with Upstash DESCRIPTION: This example demonstrates how to query vectors from an Upstash Vector index using similarity comparison. It generates a random vector, queries the index with filtering criteria, and processes the results including metadata and data. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/query.mdx#_snippet_0 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: Upserting Text Data with Upstash Vector DESCRIPTION: Examples of upserting raw text data into Upstash Vector database using various programming languages. The code demonstrates how to create an index instance and upsert data with metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/embeddingmodels.mdx#_snippet_0 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: Deleting Vectors by Metadata Filter in Python DESCRIPTION: This snippet shows how to delete vectors from an Upstash Vector index based on a metadata filter. It will delete all vectors that match the specified filter condition. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/delete.mdx#_snippet_3 LANGUAGE: python CODE: ``` index.delete(filter="age > 30") ``` ---------------------------------------- TITLE: Upserting Dense and Sparse Vectors in Multiple Languages DESCRIPTION: Examples showing how to upsert both dense and sparse vectors into an Upstash Vector index. Demonstrates initialization and vector insertion across different programming languages with proper authentication. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/hybridindexes.mdx#_snippet_0 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], }, }]) ``` ---------------------------------------- TITLE: Querying vector data with curl DESCRIPTION: Basic curl request to query vector data with topK and metadata options. Sends a text prompt to find similar vectors in the default namespace. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/query-data.mdx#_snippet_0 LANGUAGE: curl 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 }' ``` ---------------------------------------- TITLE: Deleting Vectors by Metadata Filter in Typescript DESCRIPTION: Shows how to delete vectors that match specific metadata criteria using a filter expression. Note that this performs a full scan operation and may be slow for large indexes. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/delete.mdx#_snippet_3 LANGUAGE: typescript CODE: ``` const response = await index.delete({ filter: "age > 30", }); // { deleted: 3 } ``` ---------------------------------------- TITLE: Executing Resumable Query with Data Input in TypeScript DESCRIPTION: This example shows how to use the resumableQuery method with a data input instead of a vector. It demonstrates setting query parameters, logging results, fetching additional results, and stopping the query. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/resumable-query.mdx#_snippet_1 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: Querying Vectors with Data Inclusion DESCRIPTION: Demonstrates how to query vectors while including their associated data in the results. Shows implementation with includeData parameter across different programming languages. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_24 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}`) } ``` ---------------------------------------- TITLE: Starting a Resumable Query in Python DESCRIPTION: Demonstrates how to initiate a resumable vector query using the Upstash Vector Python SDK. The code creates an index connection and performs the initial query, returning both results and a handle for subsequent requests. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_0 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) ``` ---------------------------------------- TITLE: Querying Vectors with Metadata Filtering in JavaScript DESCRIPTION: JavaScript implementation for querying Upstash Vector with metadata filtering. This example filters vectors by population size and geographic continent. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/filtering.mdx#_snippet_2 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, }); ``` ---------------------------------------- TITLE: Custom Reranking with Text Data in Python DESCRIPTION: Shows how to perform separate queries for dense and sparse components using text data in Python. This is useful for hybrid indexes with Upstash-hosted embedding models, allowing for custom reranking of text-based queries. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/hybridindexes.mdx#_snippet_5 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, ) ``` ---------------------------------------- TITLE: Fetching Multiple Vectors with Included Data in Python DESCRIPTION: This example demonstrates how to fetch multiple vectors from an index by their IDs while including vector data, metadata, and vector values in the results. It creates an Index instance from environment variables and then displays the retrieved vector information. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/fetch.mdx#_snippet_0 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: Weighted Queries Using IDF Strategy DESCRIPTION: Implements weighted querying using Inverse Document Frequency (IDF) for better term importance calculation. Uses the formula IDF(qᵢ) = log((N - n(qᵢ) + 0.5) / (n(qᵢ) + 0.5)) where N is total documents and n(qᵢ) is documents containing term qᵢ. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/sparseindexes.mdx#_snippet_9 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, ) ``` ---------------------------------------- TITLE: Resuming a Query in Go DESCRIPTION: Shows how to continue a resumable query in Go by fetching additional batches of similar vectors using the handle from the initial query. The code makes multiple Next calls with different batch sizes and handles any potential errors. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_6 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) } ``` ---------------------------------------- TITLE: Querying Hybrid Vector Index in PHP DESCRIPTION: Demonstrates querying hybrid indexes that combine both dense and sparse vectors using the Upstash Vector SDK. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/query.mdx#_snippet_2 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>", ); $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, )); ``` ---------------------------------------- TITLE: Querying Hybrid Indexes with Text Data DESCRIPTION: Examples demonstrating how to query hybrid indexes using text data with Upstash-hosted embedding models. Shows implementation across different programming languages using the query API. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/hybridindexes.mdx#_snippet_1 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, }, ) ``` ---------------------------------------- TITLE: Creating a Resumable Query in Python DESCRIPTION: Create a resumable query using the Index class with options for vector similarity search, including metadata, vectors, and namespace specifications. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/resumable-query.mdx#_snippet_0 LANGUAGE: python CODE: ``` query = index.resumable_query( vector=[0.1, 0.2], # or use 'data' parameter for text-based queries top_k=2, include_metadata=True, include_vectors=True, namespace="your_namespace" ) ``` ---------------------------------------- TITLE: Basic Vector Fetch in TypeScript DESCRIPTION: Demonstrates how to fetch multiple vectors using an array of IDs. Returns an array of vector objects with their IDs. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/fetch.mdx#_snippet_0 LANGUAGE: typescript CODE: ``` await index.fetch(["2", "3"]); // [{ id: "2" }, { id: "3" }] ``` ---------------------------------------- TITLE: Implementing Gradio Interface and QA Pipeline DESCRIPTION: Setting up the Question Answering pipeline with Hugging Face and creating the Gradio interface for user interaction. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/gradio-application.mdx#_snippet_4 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: Implementing Chat API Route with OpenAI and Upstash Vector in TypeScript DESCRIPTION: This code snippet defines the POST route for the chat API. It uses OpenAI's GPT-4 model, implements custom tools for adding and retrieving information from a knowledge base, and handles streaming responses. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/ai-sdk.mdx#_snippet_5 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: Upserting Vectors with Metadata in Python DESCRIPTION: Code snippet showing how to upsert a vector with metadata in Python. It demonstrates initializing an Index object and adding a vector with an associated URL in the metadata field. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_0 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"}], ) ``` ---------------------------------------- TITLE: Upserting a Single Vector in PHP DESCRIPTION: This snippet demonstrates how to upsert a single vector into an Upstash Vector index using the PHP SDK. It initializes an Index object with your Upstash credentials and uses the upsert() method with a VectorUpsert object containing a vector ID and randomly generated vector data. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/upsert-vectors.mdx#_snippet_0 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: '1', vector: createRandomVector(dimensions: 1536) )); ``` ---------------------------------------- TITLE: Starting a Resumable Query in Python DESCRIPTION: Start a resumable query to retrieve the initial results set from Upstash Vector. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/resumable-query.mdx#_snippet_1 LANGUAGE: python CODE: ``` initial_results = query.start() ``` ---------------------------------------- TITLE: Upserting Multiple Vectors with cURL in Upstash Vector Database DESCRIPTION: This example demonstrates how to upsert multiple vectors to the default namespace using cURL. The request includes vector IDs, dense vector values, and metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/upsert.mdx#_snippet_0 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: Querying Vectors with Metadata Filtering in PHP DESCRIPTION: PHP implementation for querying Upstash Vector with metadata filtering. This example filters vectors by population size and geographic continent. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/filtering.mdx#_snippet_4 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'", )); ``` ---------------------------------------- TITLE: Querying Vectors with Metadata Filtering in Go DESCRIPTION: Go implementation for querying Upstash Vector with metadata filtering. This example filters vectors by population size and geographic continent. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/filtering.mdx#_snippet_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") index.Query(vector.Query{ Vector: []float32{0.9215, 0.3897}, Filter: `population >= 1000000 AND geography.continent = 'Asia'`, TopK: 5, IncludeMetadata: true, }) } ``` ---------------------------------------- TITLE: Upserting a Single Vector with Namespace in PHP DESCRIPTION: This snippet shows how to upsert a vector into a specific namespace within an Upstash Vector index. It uses the namespace() method to specify the target namespace before calling upsert() with the vector data. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/upsert-vectors.mdx#_snippet_1 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->namespace('my-namespace')->upsert(new VectorUpsert( id: '1', vector: createRandomVector(dimensions: 1536) )); ``` ---------------------------------------- TITLE: Batch Upserting Multiple Vectors with Namespace in PHP DESCRIPTION: This snippet shows how to batch upsert multiple vectors into a specific namespace within an Upstash Vector index. It combines the namespace() method with upsertMany() to efficiently insert multiple vectors into the specified namespace. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/upsert-vectors.mdx#_snippet_3 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->namespace('my-namespace')->upsertMany([ new VectorUpsert( id: '1', vector: createRandomVector(dimensions: 1536) ), new VectorUpsert( id: '2', vector: createRandomVector(dimensions: 1536) ), ]); ``` ---------------------------------------- TITLE: Upserting Raw Text Data DESCRIPTION: Shows how to upsert raw text data which automatically sets the data field. The example demonstrates the process across different programming languages. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_23 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.", } ]) ``` ---------------------------------------- TITLE: Querying with Metadata Filtering in Python DESCRIPTION: Code snippet showing how to query vectors with metadata filtering in Python. It demonstrates using a metadata filter expression to find vectors whose URL metadata contains 'imgur.com'. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_11 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*'", ) ``` ---------------------------------------- TITLE: Document Parsing with LlamaParse DESCRIPTION: Python code to initialize LlamaParse and parse documents using SimpleDirectoryReader. Configures the parser for markdown output and processes text files. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/llamaparse.mdx#_snippet_2 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: Querying Vectors with Metadata Retrieval in PHP DESCRIPTION: Code snippet showing how to query vectors and include metadata in the results using PHP. It uses the VectorQuery class with includeMetadata set to true to retrieve metadata with search results. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_8 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, )); ``` ---------------------------------------- TITLE: Updating Vector Metadata in PHP DESCRIPTION: This snippet demonstrates how to update only metadata for an existing vector in Upstash Vector without changing the vector itself. It uses the update() method with VectorUpdate object and specifies the update mode as OVERWRITE to replace existing metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/upsert-vectors.mdx#_snippet_4 LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpdate; use Upstash\Vector\Enums\UpdateMode; $index = new Index( url: "<UPSTASH_VECTOR_REST_URL>", token: "<UPSTASH_VECTOR_REST_TOKEN>", ); $index->update(new VectorUpdate( id: '1', metadata: ['foo' => 'baz'], metadataUpdateMode: UpdateMode::OVERWRITE, )); ``` ---------------------------------------- TITLE: Specifying Namespace for Vector Upsert Operations in Python DESCRIPTION: This snippet shows how to specify a namespace when upserting vectors to an Upstash Vector index. By providing a namespace parameter, operations can be isolated to specific namespaces. When omitted, the default namespace is used. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/upsert.mdx#_snippet_3 LANGUAGE: python CODE: ``` index.upsert(..., namespace="ns") ``` ---------------------------------------- TITLE: Performing Resumable Query with Vector Input in TypeScript DESCRIPTION: This snippet demonstrates how to use the resumableQuery method with a vector input. It includes setting up the query parameters, logging the initial results, fetching additional results, and stopping the query. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/resumable-query.mdx#_snippet_0 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: Querying Upstash Vector Index with Dense and Sparse Modes in PHP DESCRIPTION: This PHP code illustrates how to use the Upstash Vector SDK to initialize an index and perform dense and sparse queries. It showcases the usage of the Index class, DataQuery object, and QueryMode enum for executing different types of queries. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/hybridindexes.mdx#_snippet_8 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 ``` ---------------------------------------- TITLE: Text-Based Similarity Search DESCRIPTION: Implementation of semantic search using text query to find similar documents. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/huggingface-embeddings.mdx#_snippet_4 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 # Number of top results to return ) print("Results for text-based similarity search:") for res in result_text_query: print(res.page_content) ``` ---------------------------------------- TITLE: Initializing Embeddings and Vector Store DESCRIPTION: Setting up the Hugging Face embeddings model and Upstash Vector store with necessary imports and configurations. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/gradio-application.mdx#_snippet_2 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: Listing Namespaces DESCRIPTION: Demonstrates how to retrieve a list of all active namespaces in the vector index. The code shows the implementation across different programming languages. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/namespaces.mdx#_snippet_2 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() ``` ---------------------------------------- TITLE: Vector Fetch by Prefix in TypeScript DESCRIPTION: Demonstrates fetching vectors using a prefix match pattern. Returns all vectors whose IDs start with the specified prefix. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/fetch.mdx#_snippet_2 LANGUAGE: typescript CODE: ``` await index.fetch({ prefix: "test-" }); // [{ id: "test-1" }, { id: "test-2" }, { id: "test-3" }] ``` ---------------------------------------- TITLE: Multiple Vectors Upsert Operation in TypeScript DESCRIPTION: Shows how to upsert multiple vectors simultaneously into the database. Each vector can optionally include metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/upsert.mdx#_snippet_1 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: Loading Index Client from Environment Variables DESCRIPTION: Python code to initialize the Upstash Vector Index client by automatically loading credentials from environment variables, which is useful for maintaining security and configuration separation. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/gettingstarted.mdx#_snippet_2 LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index.from_env() ``` ---------------------------------------- TITLE: Querying Sparse Vectors with Dimension Arrays DESCRIPTION: Shows how to query sparse vectors using arrays of dimension indices and values. Uses inner product similarity metric for scoring. Requires dimension indices and corresponding values, with options for top-k results and metadata inclusion. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/sparseindexes.mdx#_snippet_7 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, ) ``` ---------------------------------------- TITLE: Querying with Text Data in Upstash Vector DESCRIPTION: Demonstrates text-based querying using Upstash-hosted sparse embedding models. Automatically handles text embedding before query execution. Supports top-k results specification and returns similarity scores. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/sparseindexes.mdx#_snippet_8 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, ) ``` ---------------------------------------- TITLE: Scanning the Entire Index in Python DESCRIPTION: Example showing how to scan through all vectors in the index by using pagination with cursors, processing vectors in batches. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/range.mdx#_snippet_2 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: Vector Update Operation in TypeScript DESCRIPTION: Shows how to update an existing vector's metadata without changing the vector values themselves. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/upsert.mdx#_snippet_3 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: Upserting Vectors Using Dictionaries in Python DESCRIPTION: This snippet demonstrates how to upsert vectors into an Upstash Vector index using dictionaries. Each dictionary explicitly defines the 'id', 'vector', 'metadata', and 'data' keys. This approach offers more clarity about which values correspond to which fields. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/upsert.mdx#_snippet_2 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: Querying Vectors with Metadata Retrieval in JavaScript DESCRIPTION: Code snippet showing how to query vectors and include metadata in the results using JavaScript. It demonstrates using the includeMetadata option to retrieve metadata with search results. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_6 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, }) ``` ---------------------------------------- TITLE: Deleting Vectors by ID Array in Typescript DESCRIPTION: Demonstrates how to delete multiple vectors from an index by providing an array of vector IDs. The response indicates how many vectors were successfully deleted. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/delete.mdx#_snippet_0 LANGUAGE: typescript CODE: ``` const response = await index.delete(["2", "3"]); // { deleted: 2 } ``` ---------------------------------------- TITLE: Upserting Text Data with Upstash Vector DESCRIPTION: Demonstrates how to upsert text data into Upstash Vector using sparse embedding models. Requires Upstash Vector REST URL and token for authentication. Returns void after inserting vectors with text data and unique IDs. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/sparseindexes.mdx#_snippet_6 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.", } ]) ``` ---------------------------------------- TITLE: Querying Vectors with Metadata Retrieval in Go DESCRIPTION: Code snippet demonstrating how to query vectors and include metadata in the results using Go. It specifies IncludeMetadata:true to retrieve metadata alongside vector search results. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_7 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, }) } ``` ---------------------------------------- TITLE: Querying Vectors with Metadata Retrieval in Python DESCRIPTION: Code snippet showing how to query vectors and include metadata in the results using Python. It demonstrates setting the include_metadata flag to retrieve metadata alongside vector search results. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_5 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, ) ``` ---------------------------------------- TITLE: Batch Upserting Multiple Data Entries in PHP DESCRIPTION: Demonstrates how to efficiently upsert multiple data entries at once using upsertDataMany(). Recommended for batches up to 1,000 records for optimal performance. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/upsert-data.mdx#_snippet_2 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: '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: Querying Sparse Vector Index in PHP DESCRIPTION: Shows how to query sparse vector indexes using SparseVector objects with specific indices and values through the Upstash Vector SDK. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/query.mdx#_snippet_1 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>", ); $results = $index->query(new VectorQuery( sparseVector: new SparseVector( indices: [1, 2, 3], values: [5.0, 6.0, 7.0], ), topK: 15, )); ``` ---------------------------------------- TITLE: Upserting to a Sparse Index in PHP DESCRIPTION: This snippet demonstrates how to upsert vector data into a sparse index in Upstash Vector. It uses the SparseVector class to specify the indices and corresponding values, which is required for sparse indexes that store only non-zero vector elements. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/upsert-vectors.mdx#_snippet_6 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->upsert(new VectorUpsert( id: '1', sparseVector: new SparseVector( indices: [0, 1], values: [1.0, 2.0], ), )); ``` ---------------------------------------- TITLE: Fetching Vectors Using cURL DESCRIPTION: Example of how to fetch vectors from the default namespace using cURL with authentication. The request includes the vector ID and specifies that metadata should be included in the response. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/fetch.mdx#_snippet_0 LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/fetch \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "ids": ["id-0"], "includeMetadata": true }' ``` ---------------------------------------- TITLE: Fetching Vectors by ID Prefix with Namespace in PHP DESCRIPTION: Shows how to fetch vectors by ID prefix within a specific namespace, combining prefix matching with namespace isolation for more organized data retrieval. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/fetch.mdx#_snippet_3 LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorFetchByPrefix; $index = new Index( url: "<UPSTASH_VECTOR_REST_URL>", token: "<UPSTASH_VECTOR_REST_TOKEN>", ); $results = $index->namespace('my-namespace')->fetch(new VectorFetchByPrefix( prefix: 'users:', includeMetadata: true, includeVectors: true, includeData: true, )); ``` ---------------------------------------- TITLE: Fetching Vectors from a Specific Namespace Using cURL DESCRIPTION: Example of how to fetch vectors from a specific namespace ('ns') using cURL with authentication. The request includes multiple vector IDs and specifies that metadata should be included in the response. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/fetch.mdx#_snippet_1 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: Querying with Metadata Filtering in Go DESCRIPTION: Code snippet demonstrating how to query vectors with metadata filtering in Go. It uses a Filter parameter with a GLOB pattern to match URLs containing 'imgur.com'. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_13 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*'", }) } ``` ---------------------------------------- TITLE: Querying with Metadata Filtering in PHP DESCRIPTION: Code snippet showing how to query vectors with metadata filtering in PHP. It passes a filter expression to the VectorQuery to select vectors with URLs containing 'imgur.com'. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_14 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: "url GLOB '*imgur.com*'", )); ``` ---------------------------------------- TITLE: Executing Basic Range Query in TypeScript DESCRIPTION: Demonstrates a basic range query to retrieve vectors with metadata, specifying a cursor, limit, and namespace. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/range.mdx#_snippet_0 LANGUAGE: typescript CODE: ``` const responseRange = await index.range( { cursor: 0, limit: 2, includeMetadata: true, }, { namespace: "my-namespace" } ); ``` ---------------------------------------- TITLE: Document Embedding and Storage DESCRIPTION: Process for creating sample documents, embedding them using Hugging Face, and storing in Upstash Vector with batching support. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/huggingface-embeddings.mdx#_snippet_3 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: Patching Metadata in Upstash Vector with JSON Merge Patch Algorithm DESCRIPTION: Example demonstrating how to patch metadata in Upstash Vector using the JSON Merge Patch algorithm. This approach allows updating specific fields, adding new fields, or deleting existing fields by setting them to None. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/update.mdx#_snippet_1 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: Upserting Sparse Vectors in Python DESCRIPTION: Example of upserting sparse vectors to an Upstash Vector index in Python using the upstash_vector library. It demonstrates creating sparse vectors with dimension indices and their corresponding values. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/sparseindexes.mdx#_snippet_1 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])), ] ) ``` ---------------------------------------- TITLE: Stopping a Resumable Query in JavaScript DESCRIPTION: Shows how to properly terminate a resumable query in JavaScript to free up server resources. This should be called when you no longer need to fetch additional results. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_9 LANGUAGE: javascript CODE: ``` await stop(); ``` ---------------------------------------- TITLE: Retrieving Range of Vectors with Metadata using curl DESCRIPTION: Code snippet demonstrating how to retrieve a range of vectors with metadata using curl. It sends a request to the range endpoint with cursor pagination and includeMetadata parameter. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_20 LANGUAGE: shell CODE: ``` curl $UPSTASH_VECTOR_REST_URL/range \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "cursor" : "0", "limit" : 3, "includeMetadata": true}' ``` ---------------------------------------- TITLE: Batch querying multiple vector queries DESCRIPTION: Example of sending multiple query requests in a single API call, allowing for more efficient processing of several text inputs with different parameters. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/query-data.mdx#_snippet_2 LANGUAGE: curl CODE: ``` curl $UPSTASH_VECTOR_REST_URL/query-data \ -X POST \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '[\n {\n "data": "What is Upstash?",\n "topK": 2,\n "includeMetadata": true\n },\n {\n "data": "What is Upstash Vector?",\n "topK": 3\n }\n ]' ``` ---------------------------------------- TITLE: Querying with Distribution-Based Score Fusion in JavaScript DESCRIPTION: Example of using DBSF fusion algorithm for querying a hybrid index in JavaScript. It shows how to create an Index instance and perform a query with both dense and sparse vectors. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/hybridindexes.mdx#_snippet_3 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, }); ``` ---------------------------------------- TITLE: Upserting Sparse Vectors in JavaScript DESCRIPTION: Example of upserting sparse vectors to an Upstash Vector index in JavaScript using the @upstash/vector library. It shows how to create a sparse vector by specifying indices and their corresponding values. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/sparseindexes.mdx#_snippet_2 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], }, }]) ``` ---------------------------------------- TITLE: Data Update Operation in TypeScript DESCRIPTION: Demonstrates how to update existing data entries with new metadata while maintaining the same ID. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/upsert.mdx#_snippet_6 LANGUAGE: typescript CODE: ``` await index.upsert({ id: "1234", data: "Upstash product" metadata: { title: "Redis" } }) await index.upsert({ id: "1234", metadata: { title: "QStash" } }) ``` ---------------------------------------- TITLE: Batch Vector Query using cURL DESCRIPTION: Example of a batch query request using cURL, showing how to send multiple vector queries in a single request with different parameters. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/query.mdx#_snippet_2 LANGUAGE: sh 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: Upstash Vector Implementation with Hosted Embedding Models DESCRIPTION: Implementation of vector storage and retrieval using Upstash's hosted embedding models, including chunking logic and querying functionality. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/ai-sdk.mdx#_snippet_2 LANGUAGE: typescript CODE: ``` import { Index } from '@upstash/vector' const index = new Index({ url: process.env.UPSTASH_VECTOR_REST_URL!, token: process.env.UPSTASH_VECTOR_REST_TOKEN!, }) function generateChunks(input: string): string[] { return input .trim() .split('.') .filter(i => i !== '') } export async function upsertEmbedding(resourceId: string, content: string) { const chunks = generateChunks(content) const toUpsert = chunks.map((chunk, i) => ({ id: `${resourceId}-${i}`, data: chunk, metadata: { resourceId, content: chunk, }, })) await index.upsert(toUpsert) } export async function findRelevantContent(query: string, k = 4) { const result = await index.query({ data: query, topK: k, includeMetadata: true, }) return result } ``` ---------------------------------------- TITLE: Starting a Resumable Query using REST API with curl DESCRIPTION: Shows how to initiate a resumable vector query using direct REST API calls with curl. The request includes the target vector, topK value, and configuration to include metadata in the response. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_3 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: Deleting Vectors by ID Prefix with Namespace in PHP DESCRIPTION: This snippet shows how to delete vectors by ID prefix within a specific namespace using the Upstash Vector SDK in PHP. It combines the namespace() method with VectorDeleteByPrefix for targeted deletion. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/delete-vectors.mdx#_snippet_3 LANGUAGE: PHP CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorDeleteByPrefix; $index = new Index( url: "<UPSTASH_VECTOR_REST_URL>", token: "<UPSTASH_VECTOR_REST_TOKEN>", ); $index->namespace('my-namespace')->delete(new VectorDeleteByPrefix( prefix: 'users:', )); ``` ---------------------------------------- TITLE: Performing Resumable Vector Query with Namespace using curl DESCRIPTION: Example of making a resumable vector query request to a specific namespace in the Upstash Vector API using curl. The request targets the 'ns' namespace with the same parameters as the default query. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/resumable-query/start-with-vector.mdx#_snippet_1 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: Fetching Vectors from a Specific Namespace with cURL DESCRIPTION: HTTP request to range over vectors from a specific namespace 'ns', starting with cursor 0, limiting to 2 results per page, and including metadata in the response. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/range.mdx#_snippet_1 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: Resetting a Specific Namespace in Upstash Vector Using Python DESCRIPTION: This example shows how to reset a specific namespace in an Upstash Vector index. It initializes an Index instance and calls the reset method with a namespace parameter to clear all vectors and metadata from that particular namespace. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/reset.mdx#_snippet_1 LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index.from_env() index.reset(namespace="ns") ``` ---------------------------------------- TITLE: Querying Upstash Vector Index with Dense and Sparse Modes using curl DESCRIPTION: This curl command example shows how to perform dense and sparse queries against an Upstash Vector index using RESTful API calls. It demonstrates the necessary headers and payload structure for both query modes. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/hybridindexes.mdx#_snippet_9 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: Deleting Vectors by ID Prefix in PHP DESCRIPTION: This example illustrates how to delete vectors using an ID prefix with the Upstash Vector SDK in PHP. It uses the VectorDeleteByPrefix class to remove all vectors with IDs starting with a specified prefix. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/delete-vectors.mdx#_snippet_2 LANGUAGE: PHP CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorDeleteByPrefix; $index = new Index( url: "<UPSTASH_VECTOR_REST_URL>", token: "<UPSTASH_VECTOR_REST_TOKEN>", ); $index->delete(new VectorDeleteByPrefix( prefix: 'users:', )); ``` ---------------------------------------- TITLE: Deleting Vectors by ID Using cURL in Upstash Vector API DESCRIPTION: Shows how to delete specific vectors by their IDs using a cURL request to the Upstash Vector API. The request includes authentication via bearer token and specifies the vector IDs to delete in the request body. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/delete.mdx#_snippet_0 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: Resetting Default Namespace in TypeScript DESCRIPTION: Demonstrates how to reset the default namespace of a vector index using the basic reset method without arguments. Returns 'Successful' on successful reset. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/reset.mdx#_snippet_0 LANGUAGE: typescript CODE: ``` const responseReset = await index.reset(); // 'Successful' ``` ---------------------------------------- TITLE: Deleting Multiple Vectors by IDs in Python DESCRIPTION: This snippet demonstrates how to delete multiple vectors from an Upstash Vector index by specifying their IDs. It creates an index connection using environment variables and deletes vectors with specific IDs. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/delete.mdx#_snippet_0 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: Resuming a Query using REST API with curl DESCRIPTION: Shows how to continue a resumable query using direct REST API calls with curl. The request includes a UUID to identify the query session and the number of additional results to fetch. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_7 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: Fetching All Results with a Resumable Query in Python DESCRIPTION: Implement a pattern to fetch all available results using a resumable query with batching. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/resumable-query.mdx#_snippet_9 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: Fetching Vectors by ID Prefix in Python DESCRIPTION: This snippet shows how to fetch all vectors with IDs that start with a specific prefix. It's useful for retrieving groups of related vectors but should be used with caution for larger datasets where pagination might be more appropriate. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/fetch.mdx#_snippet_3 LANGUAGE: python CODE: ``` index.fetch(prefix="id-") ``` ---------------------------------------- TITLE: Deleting Vectors by ID in PHP DESCRIPTION: This snippet demonstrates how to delete vectors by their IDs using the Upstash Vector SDK in PHP. It initializes an Index object with the required URL and token, then uses the delete() method to remove vectors with specific IDs. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/delete-vectors.mdx#_snippet_0 LANGUAGE: PHP CODE: ``` use Upstash\Vector\Index; $index = new Index( url: "<UPSTASH_VECTOR_REST_URL>", token: "<UPSTASH_VECTOR_REST_TOKEN>", ); $index->delete(['1', '2', '3']); ``` ---------------------------------------- TITLE: Querying with Metadata Filtering using curl DESCRIPTION: Code snippet demonstrating how to query vectors with metadata filtering using curl. It sends a request with a filter parameter that uses a GLOB pattern to match URLs containing 'imgur.com'. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_15 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: Upserting Sparse Vectors in Go DESCRIPTION: Example of upserting sparse vectors to an Upstash Vector index in Go using the vector-go library. It demonstrates creating multiple sparse vectors with dimension indices and their corresponding values. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/sparseindexes.mdx#_snippet_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", ) 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}, }, }, }) } ``` ---------------------------------------- TITLE: Deleting a Vector with Namespace Specification in Python DESCRIPTION: This snippet demonstrates how to delete a vector from a specific namespace in an Upstash Vector index. When no namespace is provided, the default namespace is used. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/delete.mdx#_snippet_2 LANGUAGE: python CODE: ``` index.delete("id-4", namespace="ns") ``` ---------------------------------------- TITLE: Deleting Namespaces DESCRIPTION: Shows how to delete a specific namespace using different programming languages. The code connects to the vector index and removes the specified namespace. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/namespaces.mdx#_snippet_1 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") ``` ---------------------------------------- TITLE: Filtering Vectors by ID Prefix in Python DESCRIPTION: Example demonstrating how to filter vectors by ID prefix, which retrieves all vectors with IDs starting with the specified prefix. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/range.mdx#_snippet_1 LANGUAGE: python CODE: ``` index.range(prefix="id-") ``` ---------------------------------------- TITLE: Upserting Single Data Entry in PHP DESCRIPTION: Demonstrates how to upsert a single piece of data using Upstash Vector's embedding models. Uses the Index class to establish connection and DataUpsert for data insertion. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/upsert-data.mdx#_snippet_0 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: '1', data: 'The capital of Japan is Tokyo', )); ``` ---------------------------------------- TITLE: Type-Safe Vector Querying in TypeScript DESCRIPTION: Illustrates how to implement type-safe querying using TypeScript generics. Includes custom metadata type definition and type-safe metadata access in the query results. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/query.mdx#_snippet_2 LANGUAGE: typescript CODE: ``` type Metadata = { title: string, genre: 'sci-fi' | 'fantasy' | 'horror' | 'action' } const results = await index.query<Metadata>({ vector: [ ... // query embedding ], includeVectors: true, topK: 1, filter: "genre = 'fantasy' and title = 'Lord of the Rings'" }) if (results[0].metadata) { const { title, genre } = results[0].metadata; console.log(`The best match in fantasy was ${title}`) } ``` ---------------------------------------- TITLE: Resuming a Query in Python DESCRIPTION: Shows how to continue a resumable query in Python by fetching additional batches of similar vectors using the handle from the initial query. The example demonstrates multiple calls to fetch_next with different batch sizes. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_4 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) ``` ---------------------------------------- TITLE: Querying Vectors with Metadata Retrieval using curl DESCRIPTION: Code snippet demonstrating how to query vectors and include metadata in the results using curl. It sends a POST request to the query endpoint with includeMetadata set to true in the JSON payload. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_9 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: Initializing Hugging Face and Upstash Vector DESCRIPTION: Setup code for loading environment variables and initializing Hugging Face embeddings model with Upstash Vector store. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/huggingface-embeddings.mdx#_snippet_2 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: Deleting Vectors by Metadata Filter in PHP DESCRIPTION: This code demonstrates how to delete vectors based on a metadata filter using the Upstash Vector SDK in PHP. It utilizes the VectorDeleteByMetadataFilter class to remove vectors that match a specific condition. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/delete-vectors.mdx#_snippet_4 LANGUAGE: PHP CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorDeleteByMetadataFilter; $index = new Index( url: "<UPSTASH_VECTOR_REST_URL>", token: "<UPSTASH_VECTOR_REST_TOKEN>", ); $index->delete(new VectorDeleteByMetadataFilter( filter: 'salary > 1000', )); ``` ---------------------------------------- TITLE: Stopping a Resumable Query in Python DESCRIPTION: Demonstrates how to properly terminate a resumable query in Python to free up server resources. This should be called when you no longer need to fetch additional results. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_8 LANGUAGE: python CODE: ``` handle.stop() ``` ---------------------------------------- TITLE: Initializing Upstash Vector Store DESCRIPTION: Python code to load environment variables and initialize the Upstash Vector store with automatic embedding generation. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/langchain.mdx#_snippet_2 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: Querying Vectors with ID Prefix in TypeScript DESCRIPTION: Shows how to use the range method to retrieve vectors with IDs starting with a specific prefix. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/range.mdx#_snippet_1 LANGUAGE: typescript CODE: ``` const responseRange = await index.range({ cursor: 0, limit: 2, prefix: "test-", }); ``` ---------------------------------------- TITLE: Upsert Multiple Vectors in Upstash Vector using curl DESCRIPTION: A curl example that demonstrates how to upsert multiple vectors with text data to be embedded in the default namespace. The request includes both required fields (id, data) and optional metadata for each vector. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/upsert-data.mdx#_snippet_0 LANGUAGE: sh 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." } ]' ``` ---------------------------------------- TITLE: Document Embedding and Storage DESCRIPTION: Creating and storing sample documents in Upstash Vector with batching configuration for efficient processing. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/gradio-application.mdx#_snippet_3 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: Stopping a Resumable Query in Go DESCRIPTION: Demonstrates how to properly terminate a resumable query in Go to free up server resources. This should be called when you no longer need to fetch additional results. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_10 LANGUAGE: go CODE: ``` handle.Close() ``` ---------------------------------------- TITLE: Inserting Data into Upstash Vector Index DESCRIPTION: Demonstrates how to insert a vector with metadata into an Upstash Vector Index using various programming languages. The example shows inserting a 2-dimensional vector with an associated ID and metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/overall/getstarted.mdx#_snippet_0 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: Resuming a Query in JavaScript DESCRIPTION: Demonstrates continuing a resumable query in JavaScript by fetching additional batches of similar vectors using the fetchNext function from the initial query. The example shows multiple calls to fetch more results with different batch sizes. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_5 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); } ``` ---------------------------------------- TITLE: Retrieving Range of Vectors with Metadata in Go DESCRIPTION: Code snippet demonstrating how to retrieve a range of vectors with metadata in Go. It uses the Range method with cursor pagination and IncludeMetadata option to fetch metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_18 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, }) } ``` ---------------------------------------- TITLE: Retrieving Range of Vectors with Metadata in PHP DESCRIPTION: Code snippet showing how to retrieve a range of vectors with metadata in PHP. It uses the VectorRange class with includeMetadata parameter to fetch metadata with the vectors. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_19 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, )); ``` ---------------------------------------- TITLE: Retrieving Range of Vectors with Metadata in JavaScript DESCRIPTION: Code snippet showing how to retrieve a range of vectors with metadata in JavaScript. It uses the range method with cursor pagination and the includeMetadata option. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_17 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, }) ``` ---------------------------------------- TITLE: Upsert Single Vector with Custom Namespace in Upstash Vector using curl DESCRIPTION: A curl command that demonstrates upserting a single vector with text data to a custom namespace called 'ns'. The request includes the vector's ID, the raw text to be embedded, and custom metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/upsert-data.mdx#_snippet_1 LANGUAGE: sh 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: Initializing LlamaIndex with Upstash Vector Store DESCRIPTION: Python code to initialize LlamaIndex with Upstash Vector store, load documents, and create a vector index. Includes environment setup and document processing. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/llamaindex.mdx#_snippet_2 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: Upserting Vectors with Metadata in Go DESCRIPTION: Code snippet demonstrating how to upsert a vector with metadata in Go. It creates an Index instance and adds a vector with URL metadata using the Upsert method. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_2 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"}, }) } ``` ---------------------------------------- TITLE: Updating Vector Metadata with Namespace in PHP DESCRIPTION: This snippet shows how to update metadata for a vector in a specific namespace within an Upstash Vector index. It combines the namespace() method with update() to modify metadata for a vector in the specified namespace. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/upsert-vectors.mdx#_snippet_5 LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorUpdate; use Upstash\Vector\Enums\UpdateMode; $index = new Index( url: "<UPSTASH_VECTOR_REST_URL>", token: "<UPSTASH_VECTOR_REST_TOKEN>", ); $index->namespace('my-namespace')->update(new VectorUpdate( id: '1', metadata: ['foo' => 'baz'], metadataUpdateMode: UpdateMode::OVERWRITE, )); ``` ---------------------------------------- TITLE: Upserting Vectors with Metadata in PHP DESCRIPTION: Code snippet showing how to upsert a vector with metadata in PHP. It initializes an Index object and uses the VectorUpsert class to add a vector with URL metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_3 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", ], )); ``` ---------------------------------------- TITLE: Stopping a Resumable Query using REST API with curl DESCRIPTION: Shows how to properly terminate a resumable query using direct REST API calls with curl. The request includes a UUID to identify the query session that should be stopped. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_11 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: Upserting Single Data Entry with Namespace in PHP DESCRIPTION: Shows how to upsert data within a specific namespace using Upstash Vector. This allows for better organization and segregation of vector data. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/upsert-data.mdx#_snippet_1 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->namespace('my-namespace')->upsertData(new DataUpsert( id: '1', data: 'The capital of Japan is Tokyo', )); ``` ---------------------------------------- TITLE: Performing Resumable Vector Query with curl DESCRIPTION: Example of making a resumable vector query request to the Upstash Vector API using curl. This request includes a 2-dimensional query vector, requesting the top 2 results with metadata included and a maximum idle time of 3600 seconds. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/resumable-query/start-with-vector.mdx#_snippet_0 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 }' ``` ---------------------------------------- TITLE: Setting Custom Max Idle Time in JavaScript DESCRIPTION: Demonstrates how to configure the maximum idle time for a resumable query in JavaScript. This determines how long a query can remain inactive before being automatically terminated by the server. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_13 LANGUAGE: javascript CODE: ``` const { result, fetchNext, stop } = await index.resumableQuery({ vector: [0.1, 0.2], topK: 2, includeMetadata: true, maxIdle: 7200, // two hours, in seconds }); ``` ---------------------------------------- TITLE: Fetching Vectors by ID Prefix in PHP DESCRIPTION: Demonstrates how to fetch vectors by matching their ID prefixes. This is useful for retrieving groups of related vectors that share a common ID prefix pattern. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/fetch.mdx#_snippet_2 LANGUAGE: php CODE: ``` use Upstash\Vector\Index; use Upstash\Vector\VectorFetchByPrefix; $index = new Index( url: "<UPSTASH_VECTOR_REST_URL>", token: "<UPSTASH_VECTOR_REST_TOKEN>", ); $results = $index->fetch(new VectorFetchByPrefix( prefix: 'users:', includeMetadata: true, includeVectors: true, includeData: true, )); ``` ---------------------------------------- TITLE: Query Results Example DESCRIPTION: Sample output from running a similarity search query, showing how results are ordered by relevance to the input query. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/langchain.mdx#_snippet_2 LANGUAGE: plaintext CODE: ``` Similarity Search Results: LangChain is a framework for building intelligent apps. Semantic search enables advanced query matching. Upstash Vector is a scalable vector database. ``` ---------------------------------------- TITLE: Namespace Vector Upsert Operation in TypeScript DESCRIPTION: Demonstrates upserting vectors into a specific namespace. Namespaces allow for logical separation of vectors within the same index. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/upsert.mdx#_snippet_2 LANGUAGE: typescript CODE: ``` await index.upsert([ { id: "6789", vector: [0.6, 0.7, 0.8, 0.9, 0.9], }, ], { namespace: "my-namespace" }); ``` ---------------------------------------- TITLE: Accessing Index Information Properties in PHP DESCRIPTION: This snippet shows how to access various properties of the IndexInfo object returned by getInfo(). It includes vector counts, index size, dimensions, similarity function, and namespace information. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/info.mdx#_snippet_1 LANGUAGE: php CODE: ``` // To know the number of vectors ready to query. $info->vectorCount; // To know the number of vectors that are getting indexed. $info->pendingVectorCount; // To know the size of the index in bytes. $info->indexSize; // To know the dimensions of your vector index. $info->dimension; // To know which similarity function is being used. $info->similarityFunction; // To get information about a specific index you can (More on next section): $namespaceInfo = $info->namespace('my-namespace'); ``` ---------------------------------------- TITLE: Querying Content with Upstash Vector DESCRIPTION: Python implementation for indexing parsed documents in Upstash Vector and performing semantic queries. Includes environment setup, vector store initialization, and query execution. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/llamaparse.mdx#_snippet_3 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: Stopping an Async Resumable Query in Python DESCRIPTION: Stop an asynchronous resumable query to release server-side resources. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/resumable-query.mdx#_snippet_7 LANGUAGE: python CODE: ``` stop_result = await query.async_stop() assert stop_result == 'Success' ``` ---------------------------------------- TITLE: Initializing Index Client with Explicit Credentials DESCRIPTION: Python code to initialize the Upstash Vector Index client using explicit URL and token credentials that can be obtained from the Upstash console after creating a vector database. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/gettingstarted.mdx#_snippet_1 LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index(url="UPSTASH_VECTOR_REST_URL", token="UPSTASH_VECTOR_REST_TOKEN") ``` ---------------------------------------- TITLE: Querying the Vector Index DESCRIPTION: Example of how to query the document using the initialized query engine SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/llamaindex.mdx#_snippet_3 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: Retrieving Namespace Information in PHP using Upstash Vector SDK DESCRIPTION: This snippet demonstrates how to fetch information about specific namespaces using the getNamespaceInfo() method. It shows how to get info for both the default namespace and a custom namespace. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/info.mdx#_snippet_2 LANGUAGE: php CODE: ``` use Upstash\Vector\Index; $index = new Index( url: "<UPSTASH_VECTOR_REST_URL>", token: "<UPSTASH_VECTOR_REST_TOKEN>", ); // Fetch the information of the default namespace. $defaultNamespaceInfo = $index->getNamespaceInfo(); // Fetch the information on a specific namespace. $myNamespaceInfo = $index->namespace('my-namespace')->getNamespaceInfo(); ``` ---------------------------------------- TITLE: Querying Text Data with Upstash Vector DESCRIPTION: Examples of querying raw text data from Upstash Vector database using various programming languages. The code shows how to perform similarity searches with specified parameters like top_k and metadata inclusion. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/embeddingmodels.mdx#_snippet_1 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: Upserting to a Hybrid Index in PHP DESCRIPTION: This snippet demonstrates how to upsert to a hybrid index in Upstash Vector, which requires both dense and sparse vector data. It combines a standard vector with a SparseVector object to provide both vector types as required by hybrid indexes. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/upsert-vectors.mdx#_snippet_8 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: '1', vector: createRandomVector(dimensions: 1536), sparseVector: new SparseVector( indices: [0, 1], values: [1.0, 2.0], ), )); ``` ---------------------------------------- TITLE: Setting Custom Max Idle Time using REST API with curl DESCRIPTION: Demonstrates how to configure the maximum idle time for a resumable query using direct REST API calls with curl. This determines how long a query can remain inactive before being automatically terminated. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/resumablequery.mdx#_snippet_15 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, "maxIdle": 7200 }' ``` ---------------------------------------- TITLE: Fetching Vectors with Pagination using cURL DESCRIPTION: HTTP request to range over vectors from the default namespace, starting with cursor 0, limiting to 2 results per page, and including metadata in the response. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/range.mdx#_snippet_0 LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/range \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" \ -d '{ "cursor": "0", "limit": 2, "includeMetadata": true }' ``` ---------------------------------------- TITLE: Complete Example of Vector Upsert Operation DESCRIPTION: A comprehensive Python example that demonstrates how to initialize the client, generate a random vector, attach metadata, and upsert it into the Upstash Vector database. This showcases the basic workflow of the SDK. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/gettingstarted.mdx#_snippet_3 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 Sparse Vectors in PHP DESCRIPTION: Example of upserting sparse vectors to an Upstash Vector index in PHP using the Upstash Vector library. It shows how to create and upsert multiple sparse vectors with their dimension indices and values. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/sparseindexes.mdx#_snippet_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], ), ), ]); ``` ---------------------------------------- TITLE: Retrieving Vector Index Information with cURL DESCRIPTION: Example of how to query the Upstash Vector Index Info API using cURL. This request requires an Upstash Vector REST URL and a bearer token for authentication. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/info.mdx#_snippet_0 LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/info \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" ``` ---------------------------------------- TITLE: Execute Resumable Vector Query with cURL DESCRIPTION: Example of making a POST request to resume a vector similarity search using cURL. Requires authentication via bearer token and accepts a query UUID and number of additional results to fetch. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/resumable-query/resume.mdx#_snippet_0 LANGUAGE: sh 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": 2 }' ``` ---------------------------------------- TITLE: Implementing Command-Level TypeScript Types DESCRIPTION: Using TypeScript types at the individual command level for specific vector operations. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/getting-started.mdx#_snippet_5 LANGUAGE: typescript CODE: ``` import { Index } from "@upstash/vector"; type Metadata = { genre: string, year: number }; const index = new Index(); index.upsert<Metadata>({ id: 1, vector: [...], metadata: { genre: "comedy", year: 1990 }}); ``` ---------------------------------------- TITLE: Retrieving Index Statistics with Upstash Vector Info Method in Python DESCRIPTION: This snippet demonstrates how to use the info method in Upstash Vector to get statistical information about a vector index. It initializes an index from environment variables, calls the info method, and then displays various properties including vector counts, index size, dimensions, similarity function, and namespace statistics. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/info.mdx#_snippet_0 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: Upserting a Vector using cURL and Upstash Vector REST API DESCRIPTION: This command uses cURL to upsert a vector with ID 'id-0' into an Upstash Vector database. It requires the UPSTASH_VECTOR_REST_URL and UPSTASH_VECTOR_REST_TOKEN environment variables to be set for authentication. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/get-started.mdx#_snippet_0 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: Fetching a Vector with Namespace Specification in Python DESCRIPTION: This example demonstrates how to fetch a vector while specifying a namespace to operate on. When a namespace is provided, the fetch operation retrieves vectors from that specific namespace rather than the default one. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/fetch.mdx#_snippet_2 LANGUAGE: python CODE: ``` index.fetch("id-4", namespace="ns") ``` ---------------------------------------- TITLE: Successful query response format DESCRIPTION: Example of a successful response from the query endpoint showing an array of matched vectors with their IDs, similarity scores, and optional metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/query-data.mdx#_snippet_3 LANGUAGE: json CODE: ``` { "result": [ { "id": "id-0", "score": 1.0, "metadata": { "link": "upstash.com" } }, { "id": "id-1", "score": 0.99996454 } ] } ``` ---------------------------------------- TITLE: Initializing Client with Environment Variables DESCRIPTION: Example of initializing the Upstash Vector client using environment variables. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/overview-backup.mdx#_snippet_2 LANGUAGE: typescript CODE: ``` import { Index } from "@upstash/vector"; const index = new Index(); ``` ---------------------------------------- TITLE: Creating an Async Resumable Query in Python DESCRIPTION: Create a resumable query using the AsyncIndex class for asynchronous vector similarity search operations. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/resumable-query.mdx#_snippet_4 LANGUAGE: python CODE: ``` query = await async_index.resumable_query( vector=[0.1, 0.2], top_k=2, include_metadata=True, include_vectors=True, namespace='your_namespace' ) ``` ---------------------------------------- TITLE: Metadata Filter Examples using SQL-like Syntax DESCRIPTION: Various examples of filter expressions using SQL-like syntax supported by Upstash Vector for filtering based on different metadata conditions. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/filtering.mdx#_snippet_6 LANGUAGE: sql CODE: ``` country = 'Turkey' AND population = 15460000 AND is_capital = false ``` LANGUAGE: sql CODE: ``` country != 'Germany' AND population != 12500000 AND is_capital != true ``` LANGUAGE: sql CODE: ``` population < 20000000 OR geography.coordinates.longitude < 30.0 ``` LANGUAGE: sql CODE: ``` population <= 20000000 OR geography.coordinates.longitude <= 30.0 ``` LANGUAGE: sql CODE: ``` population > 10000000 OR geography.coordinates.latitude > 39.5 ``` LANGUAGE: sql CODE: ``` population >= 10000000 OR geography.coordinates.latitude >= 39.5 ``` LANGUAGE: sql CODE: ``` city GLOB '?[sz]*[^m-z]' ``` LANGUAGE: sql CODE: ``` city NOT GLOB 'A*' ``` LANGUAGE: sql CODE: ``` country IN ('Germany', 'Turkey', 'France') ``` LANGUAGE: sql CODE: ``` economy.currency NOT IN ('USD', 'EUR') ``` LANGUAGE: sql CODE: ``` economy.major_industries CONTAINS 'Tourism' ``` LANGUAGE: sql CODE: ``` economy.major_industries NOT CONTAINS 'Steel Production' ``` LANGUAGE: sql CODE: ``` HAS FIELD geography.coordinates ``` LANGUAGE: sql CODE: ``` HAS NOT FIELD geography.coordinates.longitude ``` LANGUAGE: sql CODE: ``` country = 'Turkey' AND population > 10000000 ``` LANGUAGE: sql CODE: ``` country = 'Turkey' AND (population > 10000000 OR is_capital = false) ``` LANGUAGE: sql CODE: ``` economy.currency != 'USD' AND geography.coordinates.latitude >= 35.0 ``` LANGUAGE: sql CODE: ``` economy.major_industries[0] = 'Tourism' ``` LANGUAGE: sql CODE: ``` economy.major_industries[#-1] = 'Finance' ``` ---------------------------------------- TITLE: Accessing Namespace Information Properties in PHP DESCRIPTION: This snippet shows how to access properties of the NamespaceInfo object returned by getNamespaceInfo(). It includes the number of vectors ready to query and the number of pending vectors. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/info.mdx#_snippet_3 LANGUAGE: php CODE: ``` // To know the number of vectors ready to query. $myNamespaceInfo->vectorCount; // To know the number of vectors that are getting indexed. $myNamespaceInfo->pendingVectorCount; ``` ---------------------------------------- TITLE: Error Handling for Resumable Queries in Python DESCRIPTION: Handle errors that can occur when trying to fetch results from a stopped query or stopping an already stopped query. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/resumable-query.mdx#_snippet_8 LANGUAGE: python CODE: ``` with pytest.raises(ClientError): query.fetch_next(1) query.async_fetch_next(1) for async with pytest.raises(ClientError): query.stop() # or await query.async_stop() for async ``` ---------------------------------------- TITLE: Using Vector Facade in Laravel DESCRIPTION: Example of accessing Upstash Vector using the Laravel facade pattern to fetch index information. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/laravel.mdx#_snippet_2 LANGUAGE: php CODE: ``` use Upstash\Vector\Laravel\Facades\Vector; Vector::getInfo(); // Fetches the index info. ``` ---------------------------------------- TITLE: Upserting Vectors Using Vector Object in Python DESCRIPTION: This snippet demonstrates how to upsert vectors into an Upstash Vector index using Vector objects. It initializes an index from environment variables, creates multiple Vector objects with random vector values, metadata, and unstructured data, then upserts them into the index. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/upsert.mdx#_snippet_0 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: Retrieving Range of Vectors with Metadata in Python DESCRIPTION: Code snippet showing how to retrieve a range of vectors with metadata in Python. It demonstrates using the range method with cursor pagination and the include_metadata flag. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_16 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, ) ``` ---------------------------------------- TITLE: Multiple Data Upsert Operation in TypeScript DESCRIPTION: Shows how to upsert multiple text data entries that will be converted to vector embeddings, with optional metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/upsert.mdx#_snippet_5 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: Deleting a Namespace in Upstash Vector using cURL DESCRIPTION: This example demonstrates how to delete a namespace named 'ns' from an Upstash Vector index using cURL. The request requires authentication via a bearer token stored in the UPSTASH_VECTOR_REST_TOKEN environment variable. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/delete-namespace.mdx#_snippet_0 LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/delete-namespace/ns \ -X DELETE \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" ``` ---------------------------------------- TITLE: Vector Fetch with Not Found Handling in TypeScript DESCRIPTION: Shows how the fetch operation handles non-existent vectors, returning null for vectors that aren't found while still returning existing vectors. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/fetch.mdx#_snippet_1 LANGUAGE: typescript CODE: ``` await index.fetch(["2", "3"]); // [{ id: "2" }, null] ``` ---------------------------------------- TITLE: Manual Initialization of Upstash Vector Client DESCRIPTION: PHP code showing how to manually initialize the Upstash Vector client by directly passing URL and token parameters. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/getting-started.mdx#_snippet_3 LANGUAGE: php CODE: ``` use Upstash\Vector\Index; $index = new Index( url: "<UPSTASH_VECTOR_REST_URL>", token: "<UPSTASH_VECTOR_REST_TOKEN>", ); ``` ---------------------------------------- TITLE: Stopping a Resumable Query with cURL in Upstash Vector DESCRIPTION: This example demonstrates how to use cURL to stop a resumable query by sending a POST request to the resumable-query-end endpoint. It requires the Upstash Vector REST URL and authentication token as environment variables. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/resumable-query/stop.mdx#_snippet_0 LANGUAGE: sh 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: Accessing Specific Upstash Vector Connection in Laravel DESCRIPTION: Example of switching between different Vector connections in a Laravel application using the connection method. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/laravel.mdx#_snippet_7 LANGUAGE: php CODE: ``` use Upstash\Vector\Laravel\Facades\Vector; Vector::connection('another')->getInfo(); ``` ---------------------------------------- TITLE: Calculating Euclidean Distance Score in Vector Indexing DESCRIPTION: Formula for calculating the Euclidean distance similarity score between two vectors. The score is normalized to a range of 0-1 by using the reciprocal of 1 plus the squared distance. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/similarityfunctions.mdx#_snippet_1 LANGUAGE: plaintext CODE: ``` 1 / (1 + squared_distance(v1, v2)) ``` ---------------------------------------- TITLE: Resetting All Namespaces in Upstash Vector Using Python DESCRIPTION: This snippet demonstrates how to reset all namespaces in an Upstash Vector index at once. It initializes an Index instance and calls the reset method with the all parameter set to True to clear vectors and metadata from all namespaces. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/reset.mdx#_snippet_2 LANGUAGE: python CODE: ``` from upstash_vector import Index index = Index.from_env() index.reset(all=True) ``` ---------------------------------------- TITLE: Creating Chat UI with Vercel AI SDK in TypeScript React DESCRIPTION: This code snippet implements the chat UI using the Vercel AI SDK's useChat hook. It renders messages, handles input changes, and submits user messages to the chat API. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/ai-sdk.mdx#_snippet_6 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 ( <div className="mx-auto max-w-md py-6"> <h1 className="text-xl font-bold mb-4">RAG Chatbot with Upstash Vector</h1> {/* Render messages */} <div className="space-y-2 mb-8"> {messages.map(m => ( <div key={m.id} className="border p-2 rounded"> <strong>{m.role}:</strong> <div> {/* If the model calls a tool, show which tool it called */} {m.content.length > 0 ? ( m.content ) : ( <i>calling tool: {m?.toolInvocations?.[0]?.toolName}</i> )} </div> </div> ))} </div> {/* Text input */} <form onSubmit={handleSubmit} className="flex gap-2"> <input className="flex-1 border rounded px-2 py-1" placeholder="Say something..." value={input} onChange={handleInputChange} /> <button className="px-4 py-1 bg-black text-white rounded" type="submit"> Send </button> </form> </div> ) } ``` ---------------------------------------- TITLE: Batch Upserting Multiple Vectors in PHP DESCRIPTION: This snippet demonstrates how to upsert multiple vectors at once into an Upstash Vector index using the upsertMany() method. It creates an array of VectorUpsert objects, each with a unique ID and vector data, improving performance by batching operations together. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/upsert-vectors.mdx#_snippet_2 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->upsertMany([ new VectorUpsert( id: '1', vector: createRandomVector(dimensions: 1536) ), new VectorUpsert( id: '2', vector: createRandomVector(dimensions: 1536) ), ]); ``` ---------------------------------------- TITLE: Fetching a Single Vector by ID in Python DESCRIPTION: A simplified example showing how to fetch a single vector by providing just the vector ID. This is a more concise alternative to the list-based approach when only one vector is needed. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/fetch.mdx#_snippet_1 LANGUAGE: python CODE: ``` index.fetch("id-4") ``` ---------------------------------------- TITLE: Batch Upserting with Namespace in PHP DESCRIPTION: Shows how to perform batch upserting operations within a specific namespace. Combines the benefits of batch processing with namespace organization. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/upsert-data.mdx#_snippet_3 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->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: Resetting All Namespaces with curl in Upstash Vector DESCRIPTION: This example demonstrates how to reset all namespaces in an Upstash Vector index by using the 'all' query parameter with a curl DELETE request. It requires the Upstash Vector REST URL and authentication token as environment variables. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/reset.mdx#_snippet_2 LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/reset?all \ -X DELETE \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" ``` ---------------------------------------- TITLE: Resetting Specific Namespace with curl in Upstash Vector DESCRIPTION: This example shows how to reset a specific namespace named 'ns' in an Upstash Vector index using a curl DELETE request. It requires the Upstash Vector REST URL and authentication token as environment variables. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/reset.mdx#_snippet_1 LANGUAGE: sh CODE: ``` curl $UPSTASH_VECTOR_REST_URL/reset/ns \ -X DELETE \ -H "Authorization: Bearer $UPSTASH_VECTOR_REST_TOKEN" ``` ---------------------------------------- TITLE: Initializing Vector Client with Environment Variables DESCRIPTION: Creating a new Vector Index instance using environment variables for authentication. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/getting-started.mdx#_snippet_2 LANGUAGE: typescript CODE: ``` import { Index } from "@upstash/vector"; const index = new Index(); ``` ---------------------------------------- TITLE: Namespace-Specific Vector Query using cURL DESCRIPTION: Example of querying vectors in a specific namespace using cURL, showing how to target a particular namespace for vector queries. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/query.mdx#_snippet_1 LANGUAGE: sh 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: Updating Vector Value in a Namespace using cURL DESCRIPTION: This example shows how to update the vector value of a vector with ID 'id-2' in the 'ns' namespace. It sets the vector to a 2-dimensional array [0.1, 0.2] using the Upstash Vector REST API. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/update.mdx#_snippet_1 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: Resetting All Namespaces in TypeScript DESCRIPTION: Shows how to reset all namespaces of a vector index by passing the all:true argument. This will clear all vectors and metadata across all namespaces. Returns 'Successful' on successful reset. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/reset.mdx#_snippet_1 LANGUAGE: typescript CODE: ``` const responseReset = await index.reset({ all: true }); // 'Successful' ``` ---------------------------------------- TITLE: Injecting Upstash Vector Index Into Laravel Controllers DESCRIPTION: Example of dependency injection pattern for accessing Upstash Vector in Laravel controllers by injecting the IndexInterface. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/laravel.mdx#_snippet_3 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: Upserting Vectors with Metadata using curl DESCRIPTION: Code snippet demonstrating how to upsert a vector with metadata using curl. It sends a POST request to the Upstash Vector REST API with a JSON payload containing the vector and URL metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/metadata.mdx#_snippet_4 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: Environment Configuration Setup DESCRIPTION: Environment variables configuration for Upstash Vector credentials including REST URL and token. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/gradio-application.mdx#_snippet_1 LANGUAGE: env CODE: ``` UPSTASH_VECTOR_REST_URL=your_upstash_url UPSTASH_VECTOR_REST_TOKEN=your_upstash_token ``` ---------------------------------------- TITLE: Upserting to a Sparse Index with Namespace in PHP DESCRIPTION: This snippet shows how to upsert vector data into a sparse index within a specific namespace in Upstash Vector. It combines the namespace() method with a SparseVector object to target the specified namespace with sparse vector data. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/commands/upsert-vectors.mdx#_snippet_7 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->namespace('my-namespace')->upsert(new VectorUpsert( id: '1', sparseVector: new SparseVector( indices: [0, 1], values: [1.0, 2.0], ), )); ``` ---------------------------------------- TITLE: Environment Variables Configuration DESCRIPTION: Configuration settings for Upstash Vector credentials in the .env file. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/langchain.mdx#_snippet_0 LANGUAGE: plaintext CODE: ``` UPSTASH_VECTOR_REST_URL=your_upstash_url UPSTASH_VECTOR_REST_TOKEN=your_upstash_token ``` ---------------------------------------- TITLE: Upserting Single Vector to a Specific Namespace with cURL DESCRIPTION: This example shows how to upsert a single vector to a specific namespace ('ns') using cURL. The request includes a vector ID, dense vector values, and metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/upsert.mdx#_snippet_1 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: Deleting Vectors by ID Prefix in Typescript DESCRIPTION: Demonstrates how to delete all vectors with IDs starting with a specific prefix. This is useful for batch operations where vectors share a common ID pattern. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/commands/delete.mdx#_snippet_2 LANGUAGE: typescript CODE: ``` const response = await index.delete({ prefix: "article_", }); // { deleted: 3 } ``` ---------------------------------------- TITLE: Installing Required Dependencies with Package Managers DESCRIPTION: Commands to install necessary packages including AI SDK, OpenAI, Zod, and Upstash Vector using different package managers. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/ai-sdk.mdx#_snippet_0 LANGUAGE: bash CODE: ``` npm install @ai-sdk/openai ai zod @upstash/vector ``` ---------------------------------------- TITLE: Initializing Upstash Vector Client from Environment Variables DESCRIPTION: PHP code snippet demonstrating how to initialize the Upstash Vector client using environment variables. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/getting-started.mdx#_snippet_2 LANGUAGE: php CODE: ``` use Upstash\Vector\Index; $index = Index::fromEnv(); ``` ---------------------------------------- TITLE: Specifying Namespace for Vector Updates in Upstash Vector DESCRIPTION: A code snippet showing how to specify a namespace when updating vectors in Upstash Vector. When no namespace is provided, the update operation will use the default namespace. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/update.mdx#_snippet_2 LANGUAGE: python CODE: ``` index.update(..., namespace="ns") ``` ---------------------------------------- TITLE: Installing Upstash Vector SDK DESCRIPTION: Commands to install the @upstash/vector package using npm or pnpm package managers. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/getting-started.mdx#_snippet_0 LANGUAGE: shell CODE: ``` npm install @upstash/vector ``` LANGUAGE: shell CODE: ``` pnpm add @upstash/vector ``` ---------------------------------------- TITLE: Deleting Vectors by ID in Custom Namespace Using cURL in Upstash Vector API DESCRIPTION: Demonstrates how to delete vectors by their IDs within a specific namespace using a cURL request. The namespace 'ns' is specified in the URL path, and authentication is provided via bearer token. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/delete.mdx#_snippet_1 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: Installing Required Packages for LangChain and Upstash Vector DESCRIPTION: Commands to install the necessary Python packages including upstash-vector, langchain, langchain-community, and python-dotenv using pip package manager. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/langchain.mdx#_snippet_0 LANGUAGE: bash CODE: ``` pip install upstash-vector langchain langchain-community python-dotenv ``` ---------------------------------------- TITLE: Querying vector data in specific namespace DESCRIPTION: Curl request that targets a specific namespace 'ns' when performing vector similarity search, which allows for data organization and isolation within the vector database. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/query-data.mdx#_snippet_1 LANGUAGE: curl 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 }' ``` ---------------------------------------- TITLE: Installing Required Python Packages DESCRIPTION: Command to install necessary Python packages including llama-index, upstash-vector, and related dependencies. SOURCE: https://github.com/upstash/docs/blob/main/vector/integrations/llamaindex.mdx#_snippet_0 LANGUAGE: bash CODE: ``` pip install llama-index upstash-vector llama-index-vector-stores-upstash python-dotenv ``` ---------------------------------------- TITLE: Representing Dense vs Sparse Vectors in Python DESCRIPTION: Example showing the difference between dense vector representation (with thousands of dimensions) and sparse vector representation (with significantly fewer non-zero dimensions). SOURCE: https://github.com/upstash/docs/blob/main/vector/features/sparseindexes.mdx#_snippet_0 LANGUAGE: python CODE: ``` dense = [0.1, 0.3, , ...thousands of non-zero values..., 0.5, 0.2] sparse = ( [23, 42, 5523, 123987, 240001], # some low number of dimension indices [0.1, 0.3, 0.1, 0.2, 0.5], # non-zero values corresponding to dimensions ) ``` ---------------------------------------- TITLE: Error Response for Non-existent Namespace in Upstash Vector DESCRIPTION: The JSON error response structure when attempting to delete a namespace that doesn't exist. Returns a 404 Not Found status with an explanatory error message. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/delete-namespace.mdx#_snippet_2 LANGUAGE: json CODE: ``` { "error": "Namespace ns for the index $NAME does not exist", "status": 404 } ``` ---------------------------------------- TITLE: Range Vectors Response Format DESCRIPTION: Example JSON response from the Range Vectors endpoint showing the nextCursor for pagination and an array of vectors with their IDs and optional metadata. SOURCE: https://github.com/upstash/docs/blob/main/vector/api/endpoints/range.mdx#_snippet_2 LANGUAGE: json CODE: ``` { "result": { "nextCursor": "2", "vectors": [ { "id": "id-0", "metadata": { "link": "upstash.com" } }, { "id": "id-1" } ] } } ``` ---------------------------------------- TITLE: Installing Required Dependencies DESCRIPTION: Command to install necessary Python packages including LangChain, sentence-transformers, and Upstash Vector client. SOURCE: https://github.com/upstash/docs/blob/main/vector/tutorials/huggingface-embeddings.mdx#_snippet_0 LANGUAGE: bash CODE: ``` pip install langchain sentence_transformers upstash-vector python-dotenv langchain-community langchain-huggingface ``` ---------------------------------------- TITLE: Custom Reranking with Separate Dense and Sparse Queries in Go DESCRIPTION: Demonstrates how to perform separate queries for dense and sparse components in Go, allowing for custom reranking. This approach is useful when standard fusion algorithms don't meet specific requirements. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/hybridindexes.mdx#_snippet_4 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 } ``` ---------------------------------------- TITLE: Example Metadata Structure in JSON Format DESCRIPTION: A sample metadata structure that demonstrates the different value types that can be filtered in Upstash Vector, including nested objects and arrays. SOURCE: https://github.com/upstash/docs/blob/main/vector/features/filtering.mdx#_snippet_0 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: Basic Upstash Vector Configuration in Laravel DESCRIPTION: Default configuration file for Upstash Vector with a single connection setup using environment variables. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/php/laravel.mdx#_snippet_5 LANGUAGE: php CODE: ``` return [ 'default' => env('UPSTASH_VECTOR_CONNECTION', 'default'), 'connections' => [ 'default' => [ 'url' => env('UPSTASH_VECTOR_REST_URL'), 'token' => env('UPSTASH_VECTOR_REST_TOKEN'), ], ], ]; ``` ---------------------------------------- TITLE: Fetching More Results Asynchronously in Python DESCRIPTION: Fetch the next batch of results asynchronously in a resumable query. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/py/example_calls/resumable-query.mdx#_snippet_6 LANGUAGE: python CODE: ``` next_results = await query.async_fetch_next(number_of_results) ``` ---------------------------------------- TITLE: Defining Index-Level TypeScript Types DESCRIPTION: Implementing strong type safety at the index level by defining metadata types for vector operations. SOURCE: https://github.com/upstash/docs/blob/main/vector/sdks/ts/getting-started.mdx#_snippet_4 LANGUAGE: typescript CODE: ``` import { Index } from "@upstash/vector"; type Metadata = { genre: string, year: number }; const index = new Index<Metadata>(); ```