-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpinecone.py
More file actions
191 lines (159 loc) · 7.02 KB
/
pinecone.py
File metadata and controls
191 lines (159 loc) · 7.02 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
"""
TurboQuant Pinecone Adapter
==============================
Compressed metadata storage for Pinecone (reduces metadata costs).
Requirements: pip install pinecone-client
Usage:
import pinecone
from turboquant.core import TurboQuantEncoder
from turboquant.adapters.pinecone import PineconeTurboCache
pc = pinecone.Pinecone(api_key="...")
index = pc.Index("my-index")
encoder = TurboQuantEncoder(dim=768)
cache = PineconeTurboCache(encoder, index)
# Store compressed backup alongside Pinecone vectors
cache.put("doc:1", vector, metadata={"title": "Hello"})
results = cache.search(query_vector, k=10)
"""
import base64
import numpy as np
from typing import Any, Dict, List, Optional, Tuple
from _base import BaseTurboAdapter
from core import TurboQuantEncoder, CompressedVector
class PineconeTurboCache(BaseTurboAdapter):
"""
Pinecone adapter with TurboQuant compression.
Strategy: Store the compressed vector as base64 in Pinecone metadata.
This allows TurboQuant reranking after Pinecone's approximate search.
Features:
- Pinecone native ANN + TurboQuant reranking
- Compressed backup in metadata (recover vectors without original source)
- Batch upsert
- Namespace support
"""
def __init__(self, encoder: TurboQuantEncoder, index: Any,
namespace: str = ""):
super().__init__(encoder)
self.index = index
self.namespace = namespace
def _raw_get(self, key: str) -> Optional[bytes]:
result = self.index.fetch(ids=[key], namespace=self.namespace)
vec_data = result.get("vectors", {}).get(key)
if vec_data and "tq_compressed" in vec_data.get("metadata", {}):
return base64.b64decode(vec_data["metadata"]["tq_compressed"])
return None
def _raw_set(self, key: str, value: bytes, ttl: Optional[int] = None) -> None:
# Cannot upsert without the original vector for Pinecone
raise NotImplementedError("Use put() which handles both vector and compressed data")
def _raw_delete(self, key: str) -> bool:
self.index.delete(ids=[key], namespace=self.namespace)
return True
def _raw_keys(self, pattern: str = "*") -> List[str]:
# Pinecone doesn't support key listing; use list() if available
try:
result = self.index.list(namespace=self.namespace)
return [v for v in result.get("vectors", [])]
except Exception:
return []
def put(self, key: str, vector: np.ndarray,
metadata: Optional[dict] = None, ttl: Optional[int] = None) -> dict:
"""Upsert vector to Pinecone with compressed backup in metadata."""
vector = np.asarray(vector, dtype=np.float32).ravel()
compressed = self.encoder.encode(vector)
data = compressed.to_bytes()
meta = metadata.copy() if metadata else {}
meta["tq_compressed"] = base64.b64encode(data).decode()
meta["tq_ratio"] = round(compressed.compression_ratio(), 1)
self.index.upsert(
vectors=[{"id": key, "values": vector.tolist(), "metadata": meta}],
namespace=self.namespace,
)
original_bytes = len(vector) * 4
self._stats["puts"] += 1
self._stats["bytes_original"] += original_bytes
self._stats["bytes_compressed"] += len(data)
return {
"key": key,
"original_bytes": original_bytes,
"compressed_bytes": len(data),
"ratio": f"{original_bytes / len(data):.1f}x",
}
def put_batch(self, items: Dict[str, np.ndarray],
metadata: Optional[Dict[str, dict]] = None,
ttl: Optional[int] = None) -> dict:
"""Batch upsert to Pinecone."""
vectors_to_upsert = []
total_orig = 0
total_comp = 0
for key, vector in items.items():
vector = np.asarray(vector, dtype=np.float32).ravel()
compressed = self.encoder.encode(vector)
data = compressed.to_bytes()
total_orig += len(vector) * 4
total_comp += len(data)
meta = (metadata or {}).get(key, {}).copy()
meta["tq_compressed"] = base64.b64encode(data).decode()
meta["tq_ratio"] = round(compressed.compression_ratio(), 1)
vectors_to_upsert.append({
"id": key, "values": vector.tolist(), "metadata": meta,
})
# Pinecone batch limit: 100 vectors
if len(vectors_to_upsert) >= 100:
self.index.upsert(vectors=vectors_to_upsert, namespace=self.namespace)
vectors_to_upsert = []
if vectors_to_upsert:
self.index.upsert(vectors=vectors_to_upsert, namespace=self.namespace)
self._stats["puts"] += len(items)
self._stats["bytes_original"] += total_orig
self._stats["bytes_compressed"] += total_comp
return {
"count": len(items),
"original_bytes": total_orig,
"compressed_bytes": total_comp,
"ratio": f"{total_orig / max(total_comp, 1):.1f}x",
}
def search(self, query: np.ndarray, k: int = 10,
keys: Optional[List[str]] = None,
mode: str = "rerank",
filter: Optional[dict] = None) -> List[dict]:
"""
Search modes:
- "pinecone": Native Pinecone ANN only
- "rerank": Pinecone ANN candidates + TurboQuant reranking
"""
query = np.asarray(query, dtype=np.float32).ravel()
query_kwargs = {
"vector": query.tolist(),
"top_k": k * 3 if mode == "rerank" else k,
"include_metadata": True,
"namespace": self.namespace,
}
if filter:
query_kwargs["filter"] = filter
results = self.index.query(**query_kwargs)
if mode == "rerank":
query_c = self.encoder.encode(query)
reranked = []
for match in results.get("matches", []):
meta = match.get("metadata", {})
if "tq_compressed" in meta:
compressed_data = base64.b64decode(meta["tq_compressed"])
candidate = CompressedVector.from_bytes(compressed_data)
score = self.encoder.similarity(query_c, candidate)
clean_meta = {k: v for k, v in meta.items()
if k not in ("tq_compressed", "tq_ratio")}
reranked.append({
"id": match["id"],
"score": score,
"pinecone_score": match["score"],
"metadata": clean_meta,
})
reranked.sort(key=lambda x: x["score"], reverse=True)
return reranked[:k]
else:
return [{
"id": m["id"],
"score": m["score"],
"metadata": {k: v for k, v in m.get("metadata", {}).items()
if k not in ("tq_compressed", "tq_ratio")},
} for m in results.get("matches", [])[:k]]