-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathquickstart.py
More file actions
351 lines (287 loc) · 9.29 KB
/
quickstart.py
File metadata and controls
351 lines (287 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/usr/bin/env python3
"""
Pinecone Upsert Quickstart
This example shows how to:
1. Load Skill Seekers documents (LangChain format)
2. Create embeddings with OpenAI
3. Upsert to Pinecone with metadata
4. Query with semantic search
Requirements:
pip install pinecone-client openai
Environment:
export PINECONE_API_KEY=your-pinecone-key
export OPENAI_API_KEY=sk-...
"""
import json
import os
import time
from pathlib import Path
from typing import List, Dict
from pinecone import Pinecone, ServerlessSpec
from openai import OpenAI
def create_index(pc: Pinecone, index_name: str, dimension: int = 1536) -> None:
"""
Create Pinecone index if it doesn't exist.
Args:
pc: Pinecone client
index_name: Name of the index
dimension: Embedding dimension (1536 for OpenAI ada-002)
"""
# Check if index exists
if index_name not in pc.list_indexes().names():
print(f"Creating index: {index_name}")
pc.create_index(
name=index_name,
dimension=dimension,
metric="cosine",
spec=ServerlessSpec(
cloud="aws",
region="us-east-1"
)
)
# Wait for index to be ready
while not pc.describe_index(index_name).status["ready"]:
print("Waiting for index to be ready...")
time.sleep(1)
print(f"✅ Index created: {index_name}")
else:
print(f"ℹ️ Index already exists: {index_name}")
def load_documents(json_path: str) -> List[Dict]:
"""
Load documents from Skill Seekers JSON output.
Args:
json_path: Path to skill-seekers generated JSON file
Returns:
List of document dictionaries
"""
with open(json_path) as f:
documents = json.load(f)
print(f"✅ Loaded {len(documents)} documents")
# Show category breakdown
categories = {}
for doc in documents:
cat = doc["metadata"].get('category', 'unknown')
categories[cat] = categories.get(cat, 0) + 1
print(f" Categories: {dict(sorted(categories.items()))}")
return documents
def create_embeddings(openai_client: OpenAI, texts: List[str]) -> List[List[float]]:
"""
Create embeddings for a list of texts.
Args:
openai_client: OpenAI client
texts: List of texts to embed
Returns:
List of embedding vectors
"""
response = openai_client.embeddings.create(
model="text-embedding-ada-002",
input=texts
)
return [data.embedding for data in response.data]
def batch_upsert(
index,
openai_client: OpenAI,
documents: List[Dict],
batch_size: int = 100
) -> None:
"""
Upsert documents to Pinecone in batches.
Args:
index: Pinecone index
openai_client: OpenAI client
documents: List of documents
batch_size: Number of documents per batch
"""
print(f"\nUpserting {len(documents)} documents...")
print(f"Batch size: {batch_size}")
vectors = []
for i, doc in enumerate(documents):
# Create embedding
response = openai_client.embeddings.create(
model="text-embedding-ada-002",
input=doc["page_content"]
)
embedding = response.data[0].embedding
# Prepare vector
vectors.append({
"id": f"doc_{i}",
"values": embedding,
"metadata": {
"text": doc["page_content"][:1000], # Store snippet
"source": doc["metadata"]["source"],
"category": doc["metadata"]["category"],
"file": doc["metadata"]["file"],
"type": doc["metadata"]["type"]
}
})
# Batch upsert
if len(vectors) >= batch_size:
index.upsert(vectors=vectors)
vectors = []
print(f" Upserted {i + 1}/{len(documents)} documents...")
# Upsert remaining
if vectors:
index.upsert(vectors=vectors)
print(f"✅ Upserted all documents to Pinecone")
# Verify
stats = index.describe_index_stats()
print(f" Total vectors in index: {stats['total_vector_count']}")
def semantic_search(
index,
openai_client: OpenAI,
query: str,
top_k: int = 5,
category: str = None
) -> List[Dict]:
"""
Perform semantic search.
Args:
index: Pinecone index
openai_client: OpenAI client
query: Search query
top_k: Number of results
category: Optional category filter
Returns:
List of matches
"""
# Create query embedding
response = openai_client.embeddings.create(
model="text-embedding-ada-002",
input=query
)
query_embedding = response.data[0].embedding
# Build filter
filter_dict = None
if category:
filter_dict = {"category": {"$eq": category}}
# Query
results = index.query(
vector=query_embedding,
top_k=top_k,
include_metadata=True,
filter=filter_dict
)
return results["matches"]
def interactive_search(index, openai_client: OpenAI) -> None:
"""
Start an interactive search session.
Args:
index: Pinecone index
openai_client: OpenAI client
"""
print("\n" + "="*60)
print("INTERACTIVE SEMANTIC SEARCH")
print("="*60)
print("Search the documentation (type 'quit' to exit)\n")
while True:
user_input = input("Query: ").strip()
if user_input.lower() in ['quit', 'exit', 'q']:
print("\n👋 Goodbye!")
break
if not user_input:
continue
try:
# Search
start = time.time()
matches = semantic_search(
index=index,
openai_client=openai_client,
query=user_input,
top_k=3
)
elapsed = time.time() - start
# Display results
print(f"\n🔍 Found {len(matches)} results ({elapsed*1000:.2f}ms)\n")
for i, match in enumerate(matches, 1):
print(f"Result {i}:")
print(f" Score: {match['score']:.3f}")
print(f" Category: {match['metadata']['category']}")
print(f" File: {match['metadata']['file']}")
print(f" Text: {match['metadata']['text'][:200]}...")
print()
except Exception as e:
print(f"\n❌ Error: {e}\n")
def main():
"""
Main execution flow.
"""
print("="*60)
print("PINECONE UPSERT QUICKSTART")
print("="*60)
print()
# Configuration
INDEX_NAME = "skill-seekers-demo"
DOCS_PATH = "../../output/django-langchain.json" # Adjust path as needed
# Check API keys
if not os.getenv("PINECONE_API_KEY"):
print("❌ PINECONE_API_KEY not set")
print("\nSet environment variable:")
print(" export PINECONE_API_KEY=your-api-key")
return
if not os.getenv("OPENAI_API_KEY"):
print("❌ OPENAI_API_KEY not set")
print("\nSet environment variable:")
print(" export OPENAI_API_KEY=sk-...")
return
# Check if documents exist
if not Path(DOCS_PATH).exists():
print(f"❌ Documents not found at: {DOCS_PATH}")
print("\nGenerate documents first:")
print(" 1. skill-seekers scrape --config configs/django.json")
print(" 2. skill-seekers package output/django --target langchain")
print("\nOr adjust DOCS_PATH in the script to point to your documents.")
return
# Initialize clients
pc = Pinecone(api_key=os.getenv("PINECONE_API_KEY"))
openai_client = OpenAI()
# Step 1: Create index
print("Step 1: Creating Pinecone index...")
create_index(pc, INDEX_NAME)
index = pc.Index(INDEX_NAME)
print()
# Step 2: Load documents
print("Step 2: Loading documents...")
documents = load_documents(DOCS_PATH)
print()
# Step 3: Upsert to Pinecone
print("Step 3: Upserting to Pinecone...")
batch_upsert(index, openai_client, documents, batch_size=100)
print()
# Step 4: Example queries
print("Step 4: Running example queries...")
print("="*60 + "\n")
example_queries = [
"How do I create a Django model?",
"Explain Django views",
"What is Django ORM?",
]
for query in example_queries:
print(f"QUERY: {query}")
print("-" * 60)
matches = semantic_search(
index=index,
openai_client=openai_client,
query=query,
top_k=3
)
for match in matches:
print(f" Score: {match['score']:.3f}")
print(f" Category: {match['metadata']['category']}")
print(f" Text: {match['metadata']['text'][:150]}...")
print()
# Step 5: Interactive search
interactive_search(index, openai_client)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n👋 Interrupted. Goodbye!")
except Exception as e:
print(f"\n❌ Error: {e}")
import traceback
traceback.print_exc()
print("\nMake sure you have:")
print(" 1. Set PINECONE_API_KEY environment variable")
print(" 2. Set OPENAI_API_KEY environment variable")
print(" 3. Installed required packages:")
print(" pip install pinecone-client openai")