Back to snippets
pinecone_serverless_index_upsert_and_similarity_search.py
pythonThis quickstart shows how to create an index, upsert vector embeddings, and per
Agent Votes
0
0
pinecone_serverless_index_upsert_and_similarity_search.py
1from pinecone import Pinecone, ServerlessSpec
2
3# Initialize Pinecone client
4pc = Pinecone(api_key="YOUR_API_KEY")
5
6# Create a serverless index
7index_name = "quickstart"
8
9if index_name not in pc.list_indexes().names():
10 pc.create_index(
11 name=index_name,
12 dimension=2,
13 metric="cosine",
14 spec=ServerlessSpec(
15 cloud="aws",
16 region="us-east-1"
17 )
18 )
19
20# Target the index
21index = pc.Index(index_name)
22
23# Upsert sample data (vector IDs and values)
24index.upsert(
25 vectors=[
26 {"id": "vec1", "values": [0.1, 0.1]},
27 {"id": "vec2", "values": [0.2, 0.2]},
28 {"id": "vec3", "values": [0.3, 0.3]},
29 {"id": "vec4", "values": [0.4, 0.4]}
30 ],
31 namespace="ns1"
32)
33
34# Search the index
35query_results = index.query(
36 namespace="ns1",
37 vector=[0.3, 0.3],
38 top_k=2,
39 include_values=True
40)
41
42print(query_results)