Setting the file. One moment. Quickstart Complete · Pinecone Quickstart · pinecone-io/skills · Skills DocsRaw file
scripts/quickstart_complete.py
Python·75 lines·3 KB
raise
ValueError
(
"PINECONE_API_KEY environment variable not set"
)
15
16pc = Pinecone(api_key=api_key, source_tag="pinecone_skills:index_quickstart")
17
18# 1. Create a serverless index with an integrated embedding model
19index_name = "quickstart"
20
21if not pc.has_index(index_name):
22 pc.create_index_for_model(
23 name=index_name,
24 cloud="aws",
25 region="us-east-1",
26 embed={
27 "model": "llama-text-embed-v2",
28 "field_map": {"text": "chunk_text"}
29 }
30 )
31
32# 2. Upsert records
33# Three distinct themes — notice the queries below use different words than the records.
34# That's semantic search: finding meaning, not just matching keywords.
35records = [
36 # Health / feeling unwell
37 {"_id": "rec1", "chunk_text": "I've been sneezing all day and my nose won't stop running.", "category": "health"},
38 {"_id": "rec2", "chunk_text": "She stayed home with a pounding headache and a low-grade fever.", "category": "health"},
39 {"_id": "rec3", "chunk_text": "He felt completely drained after waking up with a sore throat and chills.", "category": "health"},
40 # Productivity / work
41 {"_id": "rec4", "chunk_text": "She blocked off two hours in the morning to focus without interruptions.", "category": "productivity"},
42 {"_id": "rec5", "chunk_text": "He finished all his tasks ahead of schedule by prioritizing the hardest ones first.", "category": "productivity"},
43 {"_id": "rec6", "chunk_text": "Turning off notifications helped her get into a deep flow state.", "category": "productivity"},
44 # Outdoors / nature
45 {"_id": "rec7", "chunk_text": "A red fox darted across the trail and disappeared into the underbrush.", "category": "nature"},
46 {"_id": "rec8", "chunk_text": "The hikers paused to watch a bald eagle circle lazily over the valley.", "category": "nature"},
47 {"_id": "rec9", "chunk_text": "Fireflies lit up the meadow as the sun dipped below the treeline.", "category": "nature"},
48]
49
50dense_index = pc.Index(index_name)
51dense_index.upsert_records(namespace="example-namespace", records=records)
52
53# 3. Search records
54# The query uses different words than the records — semantic search finds meaning, not keywords.
55query = "feeling ill and run down"
56
57results = dense_index.search(
58 namespace="example-namespace",
59 query={"top_k": 3, "inputs": {"text": query}}
60)
61
62print("Search results:")
63for hit in results["result"]["hits"]:
64 print(f" id: {hit['id']} | score: {round(hit['score'], 2)} | text: {hit['fields']['chunk_text']}")
65
66# 4. Search with reranking
67reranked_results = dense_index.search(
68 namespace="example-namespace",
69 query={"top_k": 3, "inputs": {"text": query}},
70 rerank={"model": "bge-reranker-v2-m3", "top_n": 3, "rank_fields": ["chunk_text"]}
71)
72
73print("\nReranked results:")
74for hit in reranked_results["result"]["hits"]:
75 print(f" id: {hit['id']} | score: {round(hit['score'], 2)} | text: {hit['fields']['chunk_text']}")