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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500 | class PineconeVectorStore(BasePydanticVectorStore):
"""```python
# 松果向量存储。
# 在这个向量存储中,嵌入和文档都存储在一个松果索引中。
# 在查询时,索引使用松果来查询前k个最相似的节点。
Args:
pinecone_index (Optional[Union[pinecone.Pinecone.Index, pinecone.Index]]): 松果索引实例,
对于客户端>=3.0.0的情况使用pinecone.Pinecone.Index;对于旧版本的客户端使用pinecone.Index。
insert_kwargs (Optional[Dict]): 在`upsert`调用期间的插入kwargs。
add_sparse_vector (bool): 是否将稀疏向量添加到索引中。
tokenizer (Optional[Callable]): 用于生成稀疏向量的分词器。
default_empty_query_vector (Optional[List[float]]): 默认的空查询向量。
默认为None。如果不是None,则会在查询为空时使用该向量作为查询向量。
示例:
`pip install llama-index-vector-stores-pinecone`
```python
import os
from llama_index.vector_stores.pinecone import PineconeVectorStore
from pinecone import Pinecone, ServerlessSpec
# 设置松果API密钥
os.environ["PINECONE_API_KEY"] = "<您的松果API密钥,来自app.pinecone.io>"
api_key = os.environ["PINECONE_API_KEY"]
# 创建松果向量存储
pc = Pinecone(api_key=api_key)
pc.create_index(
name="quickstart",
dimension=1536,
metric="dotproduct",
spec=ServerlessSpec(cloud="aws", region="us-west-2"),
)
pinecone_index = pc.Index("quickstart")
vector_store = PineconeVectorStore(
pinecone_index=pinecone_index,
)
```
```"""
stores_text: bool = True
flat_metadata: bool = False
api_key: Optional[str]
index_name: Optional[str]
environment: Optional[str]
namespace: Optional[str]
insert_kwargs: Optional[Dict]
add_sparse_vector: bool
text_key: str
batch_size: int
remove_text_from_metadata: bool
_pinecone_index: Any = PrivateAttr()
_tokenizer: Optional[Callable] = PrivateAttr()
def __init__(
self,
pinecone_index: Optional[
Any
] = None, # Dynamic import prevents specific type hinting here
api_key: Optional[str] = None,
index_name: Optional[str] = None,
environment: Optional[str] = None,
namespace: Optional[str] = None,
insert_kwargs: Optional[Dict] = None,
add_sparse_vector: bool = False,
tokenizer: Optional[Callable] = None,
text_key: str = DEFAULT_TEXT_KEY,
batch_size: int = DEFAULT_BATCH_SIZE,
remove_text_from_metadata: bool = False,
default_empty_query_vector: Optional[List[float]] = None,
**kwargs: Any,
) -> None:
insert_kwargs = insert_kwargs or {}
if tokenizer is None and add_sparse_vector:
tokenizer = get_default_tokenizer()
self._tokenizer = tokenizer
super().__init__(
index_name=index_name,
environment=environment,
api_key=api_key,
namespace=namespace,
insert_kwargs=insert_kwargs,
add_sparse_vector=add_sparse_vector,
text_key=text_key,
batch_size=batch_size,
remove_text_from_metadata=remove_text_from_metadata,
)
# TODO: Make following instance check stronger -- check if pinecone_index is not pinecone.Index, else raise
# ValueError
if isinstance(pinecone_index, str):
raise ValueError(
"`pinecone_index` cannot be of type `str`; should be an instance of pinecone.Index, "
)
self._pinecone_index = pinecone_index or self._initialize_pinecone_client(
api_key, index_name, environment, **kwargs
)
@classmethod
def _initialize_pinecone_client(
cls,
api_key: Optional[str],
index_name: Optional[str],
environment: Optional[str],
**kwargs: Any,
) -> Any:
"""根据版本初始化Pinecone客户端。
如果客户端版本<3.0.0,则使用基于pods的初始化;否则,使用无服务器初始化。
"""
if not index_name:
raise ValueError(
"`index_name` is required for Pinecone client initialization"
)
pinecone = _import_pinecone()
if (
not _is_pinecone_v3()
): # If old version of Pinecone client (version bifurcation temporary):
if not environment:
raise ValueError("environment is required for Pinecone client < 3.0.0")
pinecone.init(api_key=api_key, environment=environment)
return pinecone.Index(index_name)
else: # If new version of Pinecone client (serverless):
pinecone_instance = pinecone.Pinecone(
api_key=api_key, source_tag="llamaindex"
)
return pinecone_instance.Index(index_name)
@classmethod
def from_params(
cls,
api_key: Optional[str] = None,
index_name: Optional[str] = None,
environment: Optional[str] = None,
namespace: Optional[str] = None,
insert_kwargs: Optional[Dict] = None,
add_sparse_vector: bool = False,
tokenizer: Optional[Callable] = None,
text_key: str = DEFAULT_TEXT_KEY,
batch_size: int = DEFAULT_BATCH_SIZE,
remove_text_from_metadata: bool = False,
default_empty_query_vector: Optional[List[float]] = None,
**kwargs: Any,
) -> "PineconeVectorStore":
pinecone_index = cls._initialize_pinecone_client(
api_key, index_name, environment, **kwargs
)
return cls(
pinecone_index=pinecone_index,
api_key=api_key,
index_name=index_name,
environment=environment,
namespace=namespace,
insert_kwargs=insert_kwargs,
add_sparse_vector=add_sparse_vector,
tokenizer=tokenizer,
text_key=text_key,
batch_size=batch_size,
remove_text_from_metadata=remove_text_from_metadata,
default_empty_query_vector=default_empty_query_vector,
**kwargs,
)
@classmethod
def class_name(cls) -> str:
return "PinconeVectorStore"
def add(
self,
nodes: List[BaseNode],
**add_kwargs: Any,
) -> List[str]:
"""将节点添加到索引中。
Args:
节点: List[BaseNode]: 带有嵌入的节点列表
"""
ids = []
entries = []
for node in nodes:
node_id = node.node_id
metadata = node_to_metadata_dict(
node,
remove_text=self.remove_text_from_metadata,
flat_metadata=self.flat_metadata,
)
entry = {
ID_KEY: node_id,
VECTOR_KEY: node.get_embedding(),
METADATA_KEY: metadata,
}
if self.add_sparse_vector and self._tokenizer is not None:
sparse_vector = generate_sparse_vectors(
[node.get_content(metadata_mode=MetadataMode.EMBED)],
self._tokenizer,
)[0]
entry[SPARSE_VECTOR_KEY] = sparse_vector
ids.append(node_id)
entries.append(entry)
self._pinecone_index.upsert(
entries,
namespace=self.namespace,
batch_size=self.batch_size,
**self.insert_kwargs,
)
return ids
def delete(self, ref_doc_id: str, **delete_kwargs: Any) -> None:
"""使用ref_doc_id删除节点。
Args:
ref_doc_id(str):要删除的文档的doc_id。
"""
# delete by filtering on the doc_id metadata
self._pinecone_index.delete(
filter={"doc_id": {"$eq": ref_doc_id}},
namespace=self.namespace,
**delete_kwargs,
)
@property
def client(self) -> Any:
"""返回松果客户端。"""
return self._pinecone_index
def query(self, query: VectorStoreQuery, **kwargs: Any) -> VectorStoreQueryResult:
"""查询前k个最相似节点的索引。
Args:
query_embedding(List[float]):查询嵌入
similarity_top_k(int):前k个最相似节点
"""
sparse_vector = None
if (
query.mode in (VectorStoreQueryMode.SPARSE, VectorStoreQueryMode.HYBRID)
and self._tokenizer is not None
):
if query.query_str is None:
raise ValueError(
"query_str must be specified if mode is SPARSE or HYBRID."
)
sparse_vector = generate_sparse_vectors([query.query_str], self._tokenizer)[
0
]
if query.alpha is not None:
sparse_vector = {
"indices": sparse_vector["indices"],
"values": [v * (1 - query.alpha) for v in sparse_vector["values"]],
}
# pinecone requires a query embedding, so default to 0s if not provided
if query.query_embedding is not None:
dimension = len(query.query_embedding)
else:
dimension = self._pinecone_index.describe_index_stats()["dimension"]
query_embedding = [0.0] * dimension
if query.mode in (VectorStoreQueryMode.DEFAULT, VectorStoreQueryMode.HYBRID):
query_embedding = cast(List[float], query.query_embedding)
if query.alpha is not None:
query_embedding = [v * query.alpha for v in query_embedding]
if query.filters is not None:
if "filter" in kwargs or "pinecone_query_filters" in kwargs:
raise ValueError(
"Cannot specify filter via both query and kwargs. "
"Use kwargs only for pinecone specific items that are "
"not supported via the generic query interface."
)
filter = _to_pinecone_filter(query.filters)
elif "pinecone_query_filters" in kwargs:
filter = kwargs.pop("pinecone_query_filters")
else:
filter = kwargs.pop("filter", {})
response = self._pinecone_index.query(
vector=query_embedding,
sparse_vector=sparse_vector,
top_k=query.similarity_top_k,
include_values=kwargs.pop("include_values", True),
include_metadata=kwargs.pop("include_metadata", True),
namespace=self.namespace,
filter=filter,
**kwargs,
)
top_k_nodes = []
top_k_ids = []
top_k_scores = []
for match in response.matches:
try:
node = metadata_dict_to_node(match.metadata)
node.embedding = match.values
except Exception:
# NOTE: deprecated legacy logic for backward compatibility
_logger.debug(
"Failed to parse Node metadata, fallback to legacy logic."
)
metadata, node_info, relationships = legacy_metadata_dict_to_node(
match.metadata, text_key=self.text_key
)
text = match.metadata[self.text_key]
id = match.id
node = TextNode(
text=text,
id_=id,
metadata=metadata,
start_char_idx=node_info.get("start", None),
end_char_idx=node_info.get("end", None),
relationships=relationships,
)
top_k_ids.append(match.id)
top_k_nodes.append(node)
top_k_scores.append(match.score)
return VectorStoreQueryResult(
nodes=top_k_nodes, similarities=top_k_scores, ids=top_k_ids
)
|