AI Model Wiki

    Skip to main content
    45 min
    Lesson 2 of 5

    Upserting & Querying Vectors

    From the course Vector Search with Pinecone

    Working with Vectors

    Generating Embeddings

    async function embed(texts: string[]): Promise<number[][]> {
      const response = await openai.embeddings.create({
        model: "text-embedding-3-small",
        input: texts,
      });
      return response.data.map(d => d.embedding);
    }
    

    Upserting Vectors

    const vectors = await embed(documents.map(d => d.text));
    
    await index.upsert(
      documents.map((doc, i) => ({
        id: doc.id,
        values: vectors[i],
        metadata: {
          text: doc.text,
          category: doc.category,
          created: doc.date,
        },
      }))
    );
    

    Querying

    const queryVector = await embed(["How does photosynthesis work?"]);
    
    const results = await index.query({
      vector: queryVector[0],
      topK: 10,
      includeMetadata: true,
    });
    
    for (const match of results.matches) {
      console.log(`Score: ${match.score} — ${match.metadata.text}`);
    }
    

    Batch Operations

    • Upsert in batches of 100-1000 vectors for optimal throughput
    • Use parallel requests for large ingestion jobs
    • Track progress with batch IDs for fault tolerance

    Common Pitfall: Storing Full Document Text in Metadata

    Pinecone metadata is capped at 40KB per vector. Storing full document text in metadata works for short snippets but fails for longer content. Store only a display snippet plus a reference ID pointing to the full document in your primary database:

    await index.upsert([{
      id: chunk.id,
      values: embedding,
      metadata: {
        text: chunk.text.slice(0, 800),    // Store only display snippet
        documentId: chunk.documentId,       // Reference to full doc in Postgres
        chunkIndex: chunk.index,
        category: chunk.category,
      },
    }]);
    

    Best Practice: Implement Retry with Exponential Backoff

    Large batch upserts occasionally hit rate limits. Implement retry to make bulk ingestion reliable:

    async function upsertWithRetry(vectors: any[], maxRetries = 3) {
      for (let attempt = 0; attempt < maxRetries; attempt++) {
        try {
          await index.upsert(vectors);
          return;
        } catch (err) {
          if (attempt === maxRetries - 1) throw err;
          await new Promise(r => setTimeout(r, Math.pow(2, attempt) * 1000));
        }
      }
    }
    

    Named Product Reference: Cohere Rerank with Pinecone

    A proven production pattern combines Pinecone for initial vector retrieval (fast, top-50 results) with Cohere's rerank-v3.5 API as a second stage to rerank the top 50 and return the best 5. This two-stage approach dramatically improves precision with added latency under 200ms.

    Key Takeaways

    • •Generate embeddings with an embedding model before upserting
    • •Include metadata with vectors for filtering and display
    • •Use topK to control the number of results returned
    • •Batch upserts in groups of 100-1000 for performance
    0 of 5 lessons marked complete

    Progress is kept in this browser only.