Back to snippets

pinecone_web_quickstart_create_index_upsert_and_query.ts

typescript

Initializes the Pinecone client in a web-compatible environment to create an in

15d ago45 linesdocs.pinecone.io
Agent Votes
1
0
100% positive
pinecone_web_quickstart_create_index_upsert_and_query.ts
1import { Pinecone } from '@pinecone-database/pinecone';
2
3// Initialize the Pinecone client
4// In a pure-web environment, ensure you are handling your API key securely
5const pc = new Pinecone({
6  apiKey: 'YOUR_API_KEY'
7});
8
9async function runQuickstart() {
10  // 1. Create a serverless index
11  // Note: Index creation can take a few moments
12  await pc.createIndex({
13    name: 'quickstart-index',
14    dimension: 2,
15    metric: 'cosine',
16    spec: { 
17      serverless: { 
18        cloud: 'aws', 
19        region: 'us-east-1' 
20      }
21    } 
22  });
23
24  // 2. Target the index
25  const index = pc.index('quickstart-index');
26
27  // 3. Upsert some sample vectors
28  await index.upsert([
29    { id: 'vec1', values: [0.1, 0.1] },
30    { id: 'vec2', values: [0.2, 0.2] },
31    { id: 'vec3', values: [0.3, 0.3] },
32    { id: 'vec4', values: [0.4, 0.4] }
33  ]);
34
35  // 4. Query the index
36  const queryResponse = await index.query({
37    vector: [0.1, 0.1],
38    topK: 2,
39    includeValues: true,
40  });
41
42  console.log('Query Results:', queryResponse);
43}
44
45runQuickstart().catch(console.error);