9
9
import json
10
10
import logging
11
11
import os
12
+ from typing import Optional
12
13
13
14
from langchain .text_splitter import RecursiveCharacterTextSplitter
14
15
from langchain_community .document_loaders .pdf import PyPDFLoader
15
16
from langchain_openai import OpenAIEmbeddings
16
17
from langchain_pinecone import PineconeVectorStore
17
18
18
19
# 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
20
26
from pinecone .exceptions import PineconeApiException
21
- from pinecone . models import IndexList
27
+ from pydantic import SecretStr
22
28
23
29
# this project
24
30
from models .conf import settings
@@ -31,20 +37,20 @@ class PineconeIndex:
31
37
"""Pinecone helper class."""
32
38
33
39
_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
39
45
40
- def __init__ (self , index_name : str = None ):
46
+ def __init__ (self , index_name : Optional [ str ] = None ):
41
47
self .init ()
42
48
self .index_name = index_name or settings .pinecone_index_name
43
49
logging .debug ("PineconeIndex initialized with index_name: %s" , self .index_name )
44
50
logging .debug (self .index_stats )
45
51
46
52
@property
47
- def index_name (self ) -> str :
53
+ def index_name (self ) -> Optional [ str ] :
48
54
"""index name."""
49
55
return self ._index_name
50
56
@@ -57,24 +63,30 @@ def index_name(self, value: str) -> None:
57
63
self .init_index ()
58
64
59
65
@property
60
- def index (self ) -> Pinecone . Index :
66
+ def index (self ) -> Optional [ Index ] :
61
67
"""pinecone.Index lazy read-only property."""
62
68
if self ._index is None :
63
69
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
+
65
73
return self ._index
66
74
67
75
@property
68
- def index_stats (self ) -> dict :
76
+ def index_stats (self ) -> str :
69
77
"""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."
72
82
73
83
@property
74
84
def initialized (self ) -> bool :
75
85
"""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
78
90
79
91
@property
80
92
def vector_store (self ) -> PineconeVectorStore :
@@ -95,19 +107,20 @@ def openai_embeddings(self) -> OpenAIEmbeddings:
95
107
if self ._openai_embeddings is None :
96
108
# pylint: disable=no-member
97
109
self ._openai_embeddings = OpenAIEmbeddings (
98
- api_key = settings .openai_api_key . get_secret_value () ,
110
+ api_key = settings .openai_api_key ,
99
111
organization = settings .openai_api_organization ,
100
112
)
101
113
return self ._openai_embeddings
102
114
103
115
@property
104
- def pinecone (self ) -> Pinecone :
116
+ def pinecone (self ) -> Optional [ Pinecone ] :
105
117
"""Pinecone lazy read-only property."""
106
118
if self ._pinecone is None :
107
119
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 )
111
124
return self ._pinecone
112
125
113
126
@property
@@ -119,11 +132,11 @@ def text_splitter(self) -> RecursiveCharacterTextSplitter:
119
132
120
133
def init_index (self ):
121
134
"""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 ()
127
140
128
141
# pylint: disable=no-member
129
142
def init (self ):
@@ -140,24 +153,27 @@ def delete(self):
140
153
if not self .initialized :
141
154
logging .debug ("Index does not exist. Nothing to delete." )
142
155
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 )
145
159
146
160
def create (self ):
147
161
"""Create index."""
148
162
print ("Creating index. This may take a few minutes..." )
149
163
serverless_spec = ServerlessSpec (
150
- cloud = "aws" ,
151
- region = "us-east-1" ,
164
+ cloud = CloudProvider . AWS ,
165
+ region = AwsRegion . US_EAST_1 ,
152
166
)
153
167
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." )
161
177
except PineconeApiException :
162
178
pass
163
179
0 commit comments