99import json
1010import logging
1111import os
12+ from typing import Optional
1213
1314from langchain .text_splitter import RecursiveCharacterTextSplitter
1415from langchain_community .document_loaders .pdf import PyPDFLoader
1516from langchain_openai import OpenAIEmbeddings
1617from langchain_pinecone import PineconeVectorStore
1718
1819# pinecone integration
19- from pinecone import Pinecone , ServerlessSpec
20+ from pinecone import AwsRegion , CloudProvider , Pinecone , ServerlessSpec , VectorType
21+ from pinecone .core .openapi .db_data .models import (
22+ IndexDescription as PineconeIndexDescription ,
23+ )
24+ from pinecone .db_control .models import IndexList
25+ from pinecone .db_data import Index
2026from pinecone .exceptions import PineconeApiException
21- from pinecone . models import IndexList
27+ from pydantic import SecretStr
2228
2329# this project
2430from models .conf import settings
@@ -31,20 +37,20 @@ class PineconeIndex:
3137 """Pinecone helper class."""
3238
3339 _pinecone = None
34- _index : Pinecone . Index = None
35- _index_name : str = None
36- _text_splitter : RecursiveCharacterTextSplitter = None
37- _openai_embeddings : OpenAIEmbeddings = None
38- _vector_store : PineconeVectorStore = None
40+ _index : Optional [ Index ] = None
41+ _index_name : Optional [ str ] = None
42+ _text_splitter : Optional [ RecursiveCharacterTextSplitter ] = None
43+ _openai_embeddings : Optional [ OpenAIEmbeddings ] = None
44+ _vector_store : Optional [ PineconeVectorStore ] = None
3945
40- def __init__ (self , index_name : str = None ):
46+ def __init__ (self , index_name : Optional [ str ] = None ):
4147 self .init ()
4248 self .index_name = index_name or settings .pinecone_index_name
4349 logging .debug ("PineconeIndex initialized with index_name: %s" , self .index_name )
4450 logging .debug (self .index_stats )
4551
4652 @property
47- def index_name (self ) -> str :
53+ def index_name (self ) -> Optional [ str ] :
4854 """index name."""
4955 return self ._index_name
5056
@@ -57,24 +63,30 @@ def index_name(self, value: str) -> None:
5763 self .init_index ()
5864
5965 @property
60- def index (self ) -> Pinecone . Index :
66+ def index (self ) -> Optional [ Index ] :
6167 """pinecone.Index lazy read-only property."""
6268 if self ._index is None :
6369 self .init_index ()
64- self ._index = self .pinecone .Index (name = self .index_name )
70+ if isinstance (self .pinecone , Pinecone ) and isinstance (self .index_name , str ):
71+ self ._index = self .pinecone .Index (name = self .index_name )
72+
6573 return self ._index
6674
6775 @property
68- def index_stats (self ) -> dict :
76+ def index_stats (self ) -> str :
6977 """index stats."""
70- retval = self .index .describe_index_stats ()
71- return json .dumps (retval .to_dict (), indent = 4 )
78+ if self .index is not None :
79+ retval : PineconeIndexDescription = self .index .describe_index_stats ()
80+ return json .dumps (retval .to_dict (), indent = 4 )
81+ return "Index not initialized."
7282
7383 @property
7484 def initialized (self ) -> bool :
7585 """initialized read-only property."""
76- indexes = self .pinecone .list_indexes ()
77- return self .index_name in indexes .names ()
86+ if isinstance (self .pinecone , Pinecone ) and isinstance (self .index_name , str ):
87+ indexes = self .pinecone .list_indexes ()
88+ return self .index_name in indexes .names ()
89+ return False
7890
7991 @property
8092 def vector_store (self ) -> PineconeVectorStore :
@@ -95,19 +107,20 @@ def openai_embeddings(self) -> OpenAIEmbeddings:
95107 if self ._openai_embeddings is None :
96108 # pylint: disable=no-member
97109 self ._openai_embeddings = OpenAIEmbeddings (
98- api_key = settings .openai_api_key . get_secret_value () ,
110+ api_key = settings .openai_api_key ,
99111 organization = settings .openai_api_organization ,
100112 )
101113 return self ._openai_embeddings
102114
103115 @property
104- def pinecone (self ) -> Pinecone :
116+ def pinecone (self ) -> Optional [ Pinecone ] :
105117 """Pinecone lazy read-only property."""
106118 if self ._pinecone is None :
107119 print ("Initializing Pinecone..." )
108- api_key = settings .pinecone_api_key .get_secret_value ()
109- print (f"API Key: { api_key [:12 ]} ****------" )
110- self ._pinecone = Pinecone (api_key = api_key )
120+ if isinstance (settings .pinecone_api_key , SecretStr ):
121+ api_key = settings .pinecone_api_key .get_secret_value ()
122+ print (f"API Key: { api_key [:12 ]} ****------" )
123+ self ._pinecone = Pinecone (api_key = api_key )
111124 return self ._pinecone
112125
113126 @property
@@ -119,11 +132,11 @@ def text_splitter(self) -> RecursiveCharacterTextSplitter:
119132
120133 def init_index (self ):
121134 """Verify that an index named self.index_name exists in Pinecone. If not, create it."""
122- indexes : IndexList = None
123- indexes = self .pinecone .list_indexes ()
124- if self .index_name not in indexes .names ():
125- logging .debug ("Index does not exist." )
126- self .create ()
135+ if isinstance ( self . pinecone , Pinecone ):
136+ indexes : IndexList = self .pinecone .list_indexes ()
137+ if self .index_name not in indexes .names ():
138+ logging .debug ("Index does not exist." )
139+ self .create ()
127140
128141 # pylint: disable=no-member
129142 def init (self ):
@@ -140,24 +153,27 @@ def delete(self):
140153 if not self .initialized :
141154 logging .debug ("Index does not exist. Nothing to delete." )
142155 return
143- print ("Deleting index..." )
144- self .pinecone .delete_index (self .index_name )
156+ if isinstance (self .pinecone , Pinecone ) and isinstance (self .index_name , str ):
157+ print ("Deleting index..." )
158+ self .pinecone .delete_index (self .index_name )
145159
146160 def create (self ):
147161 """Create index."""
148162 print ("Creating index. This may take a few minutes..." )
149163 serverless_spec = ServerlessSpec (
150- cloud = "aws" ,
151- region = "us-east-1" ,
164+ cloud = CloudProvider . AWS ,
165+ region = AwsRegion . US_EAST_1 ,
152166 )
153167 try :
154- self .pinecone .create_index (
155- name = self .index_name ,
156- dimension = settings .pinecone_dimensions ,
157- metric = settings .pinecone_metric ,
158- spec = serverless_spec ,
159- )
160- print ("Index created." )
168+ if isinstance (self .pinecone , Pinecone ) and isinstance (self .index_name , str ):
169+ self .pinecone .create_index (
170+ name = self .index_name ,
171+ dimension = settings .pinecone_dimensions ,
172+ metric = settings .pinecone_metric ,
173+ spec = serverless_spec ,
174+ vector_type = VectorType .DENSE ,
175+ )
176+ print ("Index created." )
161177 except PineconeApiException :
162178 pass
163179
0 commit comments