Spaces:
Sleeping
Sleeping
File size: 2,031 Bytes
f866820 |
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 |
# RAG-document-assistant/scripts/check_pinecone.py
"""
Pinecone connectivity check for pinecone>=5.x SDK.
Purpose:
Verifies Pinecone connectivity by reading the API key and attempting to list indexes.
Useful for initial setup verification and troubleshooting connection issues.
Inputs:
Reads API key from ~/secrets/pinecone.key
Outputs:
Prints connectivity status and list of available indexes
Exits with code 0 for success, non-zero for errors
Usage:
python scripts/check_pinecone.py
"""
import os
import sys
from pathlib import Path
KEY_PATH = os.path.expanduser("~/secrets/pinecone.key")
def read_key(path):
if not os.path.exists(path):
print(f"ERROR: Pinecone key not found at {path}")
sys.exit(2)
with open(path, "r", encoding="utf-8") as f:
return f.read().strip()
def main():
key = read_key(KEY_PATH)
try:
from pinecone import Pinecone
except Exception as e:
print("Pinecone client not installed or import failed:", str(e))
print("Install in aienv with: pip install 'pinecone>=5.0.0'")
sys.exit(3)
try:
# Create client instance. If you use a specific region (ServerlessSpec), set via env or here.
pc = Pinecone(api_key=key)
# list_indexes() may return a response object; attempt to extract names robustly
idxs_resp = pc.list_indexes()
# try common patterns
if hasattr(idxs_resp, "names"):
idxs = idxs_resp.names()
elif isinstance(idxs_resp, (list, tuple)):
idxs = list(idxs_resp)
elif hasattr(idxs_resp, "indexes"):
idxs = list(idxs_resp.indexes)
else:
# fallback: stringify
idxs = [str(idxs_resp)]
except Exception as e:
print("Pinecone init/list_indexes failed:", str(e))
sys.exit(4)
print("Pinecone connectivity OK. Indexes found:", len(idxs))
for idx in idxs[:20]:
print(" -", idx)
sys.exit(0)
if __name__ == "__main__":
main() |