Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #116 from pinecone-io/list-canopy-indexes
Browse files Browse the repository at this point in the history
support list canopy indexes
  • Loading branch information
acatav authored Oct 30, 2023
2 parents c83da5e + d343792 commit b7a35f8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/canopy/knowledge_base/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .knowledge_base import connect_to_pinecone
from .knowledge_base import list_canopy_indexes
from .knowledge_base import KnowledgeBase
44 changes: 33 additions & 11 deletions src/canopy/knowledge_base/knowledge_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,37 @@
DELETE_STARTER_CHUNKS_PER_DOC = 32


def connect_to_pinecone():
"""
Connect to Pinecone.
This method is called automatically when creating a new KnowledgeBase object.
Or when calling `list_canopy_indexes()`.
"""
try:
pinecone_init()
pinecone_whoami()
except Exception as e:
raise RuntimeError("Failed to connect to Pinecone. "
"Please check your credentials and try again") from e


def list_canopy_indexes() -> List[str]:
"""
List all Canopy indexes in the current Pinecone account.
Example:
>>> from canopy.knowledge_base import list_canopy_indexes
>>> list_canopy_indexes()
['canopy--my_index', 'canopy--my_index2']
Returns:
A list of Canopy index names.
"""

connect_to_pinecone()
return [index for index in list_indexes() if index.startswith(INDEX_NAME_PREFIX)]


class KnowledgeBase(BaseKnowledgeBase):

"""
Expand Down Expand Up @@ -160,20 +191,11 @@ def __init__(self,
# `create_canopy_index()`
self._index: Optional[Index] = None

@staticmethod
def _connect_pinecone():
try:
pinecone_init()
pinecone_whoami()
except Exception as e:
raise RuntimeError("Failed to connect to Pinecone. "
"Please check your credentials and try again") from e

def _connect_index(self,
connect_pinecone: bool = True
) -> None:
if connect_pinecone:
self._connect_pinecone()
connect_to_pinecone()

if self.index_name not in list_indexes():
raise RuntimeError(
Expand Down Expand Up @@ -297,7 +319,7 @@ def create_canopy_index(self,
"Please provide the vectors' dimension")

# connect to pinecone and create index
self._connect_pinecone()
connect_to_pinecone()

if self.index_name in list_indexes():
raise RuntimeError(
Expand Down
3 changes: 2 additions & 1 deletion src/canopy_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from urllib.parse import urljoin

from canopy.knowledge_base import KnowledgeBase
from canopy.knowledge_base import connect_to_pinecone
from canopy.models.data_models import Document
from canopy.tokenizer import Tokenizer
from canopy_cli.data_loader import (
Expand Down Expand Up @@ -71,7 +72,7 @@ def wait_for_service(chat_service_url: str):

def validate_connection():
try:
KnowledgeBase._connect_pinecone()
connect_to_pinecone()
except RuntimeError as e:
msg = (
f"{str(e)}\n"
Expand Down
11 changes: 10 additions & 1 deletion tests/system/knowledge_base/test_knowledge_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
from dotenv import load_dotenv
from datetime import datetime
from canopy.knowledge_base import KnowledgeBase
from canopy.knowledge_base import KnowledgeBase, list_canopy_indexes
from canopy.knowledge_base.chunker import Chunker
from canopy.knowledge_base.knowledge_base import INDEX_NAME_PREFIX
from canopy.knowledge_base.models import DocumentWithScore
Expand Down Expand Up @@ -226,6 +226,15 @@ def test_create_index(index_full_name, knowledge_base):
assert knowledge_base._index.describe_index_stats()


def test_list_indexes(index_full_name):
actual = list_canopy_indexes()
expected = [index for index in pinecone.list_indexes()
if index.startswith(INDEX_NAME_PREFIX)]

assert index_full_name in actual
assert sorted(actual) == sorted(expected)


def test_is_verify_index_connection_happy_path(knowledge_base):
knowledge_base.verify_index_connection()

Expand Down

0 comments on commit b7a35f8

Please sign in to comment.