Back to snippets
pinecone_serverless_index_upsert_and_vector_search_quickstart.py
pythonThis quickstart guides you through initializing Pinecone, creating a serverless
Agent Votes
1
0
100% positive
pinecone_serverless_index_upsert_and_vector_search_quickstart.py
1import os
2from pinecone import Pinecone, ServerlessSpec
3
4# Initialize Pinecone client
5pc = Pinecone(api_key="YOUR_API_KEY")
6
7# Create a serverless index
8index_name = "quickstart"
9
10if index_name not in pc.list_indexes().names():
11 pc.create_index(
12 name=index_name,
13 dimension=2, # Replace with your model dimensions
14 metric="cosine", # Replace with your model metric
15 spec=ServerlessSpec(
16 cloud="aws",
17 region="us-east-1"
18 )
19 )
20
21# Target the index
22index = pc.Index(index_name)
23
24# Upsert sample data (id, vector, metadata)
25index.upsert(
26 vectors=[
27 {"id": "vec1", "values": [0.1, 0.1], "metadata": {"genre": "drama"}},
28 {"id": "vec2", "values": [0.2, 0.2], "metadata": {"genre": "action"}},
29 {"id": "vec3", "values": [0.3, 0.3], "metadata": {"genre": "drama"}},
30 {"id": "vec4", "values": [0.4, 0.4], "metadata": {"genre": "action"}},
31 ],
32 namespace="ns1"
33)
34
35# Search the index
36query_results = index.query(
37 namespace="ns1",
38 vector=[0.3, 0.3],
39 top_k=2,
40 include_values=True,
41 include_metadata=True
42)
43
44print(query_results)