14
14
from langchain .text_splitter import RecursiveCharacterTextSplitter
15
15
from langchain_community .document_loaders .pdf import PyPDFLoader
16
16
from langchain_openai import OpenAIEmbeddings
17
- from langchain_pinecone import PineconeVectorStore as LCPinecone
17
+ from langchain_pinecone import PineconeVectorStore
18
18
19
19
# pinecone integration
20
20
# import pinecone
21
21
from pinecone import Pinecone , ServerlessSpec
22
22
from pinecone .core .client .exceptions import PineconeApiException
23
+ from pinecone .models import IndexList
23
24
24
25
# this project
25
26
from models .conf import settings
28
29
logging .basicConfig (level = logging .DEBUG if settings .debug_mode else logging .ERROR )
29
30
30
31
31
- # pylint: disable=too-few-public-methods
32
- # class TextSplitter:
33
- # """
34
- # Custom text splitter that adds metadata to the Document object
35
- # which is required by PineconeHybridSearchRetriever.
36
- # """
37
-
38
- # def create_documents(self, texts):
39
- # """Create documents"""
40
- # documents = []
41
- # for text in texts:
42
- # # Create a Document object with the text and metadata
43
- # document = Document(page_content=text, metadata={"context": text})
44
- # documents.append(document)
45
- # return documents
46
-
47
-
48
32
class PineconeIndex :
49
33
"""Pinecone helper class."""
50
34
@@ -53,7 +37,7 @@ class PineconeIndex:
53
37
_index_name : str = None
54
38
_text_splitter : RecursiveCharacterTextSplitter = None
55
39
_openai_embeddings : OpenAIEmbeddings = None
56
- _vector_store : LCPinecone = None
40
+ _vector_store : PineconeVectorStore = None
57
41
58
42
def __init__ (self , index_name : str = None ):
59
43
self .init ()
@@ -92,15 +76,15 @@ def index_stats(self) -> dict:
92
76
def initialized (self ) -> bool :
93
77
"""initialized read-only property."""
94
78
indexes = self .pinecone .list_indexes ()
95
- return self .index_name in indexes
79
+ return self .index_name in indexes . names ()
96
80
97
81
@property
98
- def vector_store (self ) -> LCPinecone :
82
+ def vector_store (self ) -> PineconeVectorStore :
99
83
"""Pinecone lazy read-only property."""
100
84
if self ._vector_store is None :
101
85
if not self .initialized :
102
86
self .init_index ()
103
- self ._vector_store = LCPinecone (
87
+ self ._vector_store = PineconeVectorStore (
104
88
index = self .index ,
105
89
embedding = self .openai_embeddings ,
106
90
text_key = settings .pinecone_vectorstore_text_key ,
@@ -134,8 +118,9 @@ def text_splitter(self) -> RecursiveCharacterTextSplitter:
134
118
135
119
def init_index (self ):
136
120
"""Verify that an index named self.index_name exists in Pinecone. If not, create it."""
121
+ indexes : IndexList = None
137
122
indexes = self .pinecone .list_indexes ()
138
- if self .index_name not in indexes :
123
+ if self .index_name not in indexes . names () :
139
124
logging .debug ("Index does not exist." )
140
125
self .create ()
141
126
0 commit comments