1
1
from __future__ import annotations
2
2
3
3
import logging
4
+ import os
4
5
import uuid
5
6
import warnings
6
- from typing import TYPE_CHECKING , Any , Callable , Iterable , List , Optional , Tuple , Union
7
+ from typing import (
8
+ TYPE_CHECKING ,
9
+ Any ,
10
+ Callable ,
11
+ Iterable ,
12
+ List ,
13
+ Optional ,
14
+ Tuple ,
15
+ Union ,
16
+ )
7
17
8
18
import numpy as np
9
19
from langchain_core .documents import Document
10
20
from langchain_core .embeddings import Embeddings
11
21
from langchain_core .utils .iter import batch_iterate
12
22
from langchain_core .vectorstores import VectorStore
23
+ from packaging import version
24
+ from pkg_resources import get_distribution
13
25
14
26
from langchain_community .vectorstores .utils import (
15
27
DistanceStrategy ,
@@ -63,10 +75,9 @@ def __init__(
63
75
"Passing in `embedding` as a Callable is deprecated. Please pass in an"
64
76
" Embeddings object instead."
65
77
)
66
- if not isinstance (index , pinecone .index . Index ):
78
+ if not isinstance (index , pinecone .Index ):
67
79
raise ValueError (
68
- f"client should be an instance of pinecone.index.Index, "
69
- f"got { type (index )} "
80
+ f"client should be an instance of pinecone.Index, " f"got { type (index )} "
70
81
)
71
82
self ._index = index
72
83
self ._embedding = embedding
@@ -359,11 +370,24 @@ def get_pinecone_index(
359
370
"Please install it with `pip install pinecone-client`."
360
371
)
361
372
362
- indexes = pinecone . list_indexes () # checks if provided index exists
373
+ pinecone_client_version = get_distribution ( "pinecone-client" ). version
363
374
364
- if index_name in indexes :
365
- index = pinecone .Index (index_name , pool_threads = pool_threads )
366
- elif len (indexes ) == 0 :
375
+ if version .parse (pinecone_client_version ) >= version .parse ("3.0.0" ):
376
+ pinecone_instance = pinecone .Pinecone (
377
+ api_key = os .environ .get ("PINECONE_API_KEY" ), pool_threads = pool_threads
378
+ )
379
+ indexes = pinecone_instance .list_indexes ()
380
+ index_names = [i .name for i in indexes .index_list ["indexes" ]]
381
+ else :
382
+ index_names = pinecone .list_indexes ()
383
+
384
+ if index_name in index_names :
385
+ index = (
386
+ pinecone_instance .Index (index_name )
387
+ if version .parse (pinecone_client_version ) >= version .parse ("3.0.0" )
388
+ else pinecone .Index (index_name , pool_threads = pool_threads )
389
+ )
390
+ elif len (index_names ) == 0 :
367
391
raise ValueError (
368
392
"No active indexes found in your Pinecone project, "
369
393
"are you sure you're using the right Pinecone API key and Environment? "
@@ -372,7 +396,7 @@ def get_pinecone_index(
372
396
else :
373
397
raise ValueError (
374
398
f"Index '{ index_name } ' not found in your Pinecone project. "
375
- f"Did you mean one of the following indexes: { ', ' .join (indexes )} "
399
+ f"Did you mean one of the following indexes: { ', ' .join (index_names )} "
376
400
)
377
401
return index
378
402
0 commit comments