Skip to content

Commit fb6663a

Browse files
committed
support different Pinecone initialization depending on the version
1 parent baeac23 commit fb6663a

File tree

1 file changed

+33
-9
lines changed
  • libs/community/langchain_community/vectorstores

1 file changed

+33
-9
lines changed

libs/community/langchain_community/vectorstores/pinecone.py

+33-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
from __future__ import annotations
22

33
import logging
4+
import os
45
import uuid
56
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+
)
717

818
import numpy as np
919
from langchain_core.documents import Document
1020
from langchain_core.embeddings import Embeddings
1121
from langchain_core.utils.iter import batch_iterate
1222
from langchain_core.vectorstores import VectorStore
23+
from packaging import version
24+
from pkg_resources import get_distribution
1325

1426
from langchain_community.vectorstores.utils import (
1527
DistanceStrategy,
@@ -63,10 +75,9 @@ def __init__(
6375
"Passing in `embedding` as a Callable is deprecated. Please pass in an"
6476
" Embeddings object instead."
6577
)
66-
if not isinstance(index, pinecone.index.Index):
78+
if not isinstance(index, pinecone.Index):
6779
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)}"
7081
)
7182
self._index = index
7283
self._embedding = embedding
@@ -359,11 +370,24 @@ def get_pinecone_index(
359370
"Please install it with `pip install pinecone-client`."
360371
)
361372

362-
indexes = pinecone.list_indexes() # checks if provided index exists
373+
pinecone_client_version = get_distribution("pinecone-client").version
363374

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:
367391
raise ValueError(
368392
"No active indexes found in your Pinecone project, "
369393
"are you sure you're using the right Pinecone API key and Environment? "
@@ -372,7 +396,7 @@ def get_pinecone_index(
372396
else:
373397
raise ValueError(
374398
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)}"
376400
)
377401
return index
378402

0 commit comments

Comments
 (0)