Skip to content

Commit

Permalink
community[patch]: Support different Pinecone initializations dependin…
Browse files Browse the repository at this point in the history
…g on the version (#15717)

Co-authored-by: DosticJelena <[email protected]>
  • Loading branch information
efriis and DosticJelena authored Jan 8, 2024
1 parent c0eb248 commit 94911ae
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions libs/community/langchain_community/vectorstores/pinecone.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
from __future__ import annotations

import logging
import os
import uuid
import warnings
from typing import TYPE_CHECKING, Any, Callable, Iterable, List, Optional, Tuple, Union
from typing import (
TYPE_CHECKING,
Any,
Callable,
Iterable,
List,
Optional,
Tuple,
Union,
)

import numpy as np
from langchain_core.documents import Document
from langchain_core.embeddings import Embeddings
from langchain_core.utils.iter import batch_iterate
from langchain_core.vectorstores import VectorStore
from packaging import version

from langchain_community.vectorstores.utils import (
DistanceStrategy,
Expand Down Expand Up @@ -63,10 +74,9 @@ def __init__(
"Passing in `embedding` as a Callable is deprecated. Please pass in an"
" Embeddings object instead."
)
if not isinstance(index, pinecone.index.Index):
if not isinstance(index, pinecone.Index):
raise ValueError(
f"client should be an instance of pinecone.index.Index, "
f"got {type(index)}"
f"client should be an instance of pinecone.Index, " f"got {type(index)}"
)
self._index = index
self._embedding = embedding
Expand Down Expand Up @@ -359,11 +369,24 @@ def get_pinecone_index(
"Please install it with `pip install pinecone-client`."
)

indexes = pinecone.list_indexes() # checks if provided index exists
pinecone_client_version = pinecone.__version__

if index_name in indexes:
index = pinecone.Index(index_name, pool_threads=pool_threads)
elif len(indexes) == 0:
if version.parse(pinecone_client_version) >= version.parse("3.0.0.dev"):
pinecone_instance = pinecone.Pinecone(
api_key=os.environ.get("PINECONE_API_KEY"), pool_threads=pool_threads
)
indexes = pinecone_instance.list_indexes()

This comment has been minimized.

Copy link
@dregules

dregules Jan 24, 2024

AttributeError: list_indexes is no longer a top-level attribute of the pinecone package. 
Example:

    from pinecone import Pinecone
    
    pc = Pinecone(api_key='YOUR_API_KEY')

    index_name = "quickstart" # or your index name

    if index_name not in pc.list_indexes().names():
        # do something
index_names = [i.name for i in indexes.index_list["indexes"]]
else:
index_names = pinecone.list_indexes()

if index_name in index_names:
index = (
pinecone_instance.Index(index_name)
if version.parse(pinecone_client_version) >= version.parse("3.0.0")
else pinecone.Index(index_name, pool_threads=pool_threads)
)
elif len(index_names) == 0:
raise ValueError(
"No active indexes found in your Pinecone project, "
"are you sure you're using the right Pinecone API key and Environment? "
Expand All @@ -372,7 +395,7 @@ def get_pinecone_index(
else:
raise ValueError(
f"Index '{index_name}' not found in your Pinecone project. "
f"Did you mean one of the following indexes: {', '.join(indexes)}"
f"Did you mean one of the following indexes: {', '.join(index_names)}"
)
return index

Expand Down

0 comments on commit 94911ae

Please sign in to comment.