@@ -202,6 +202,20 @@ def _initialize_client(self, api_key: Optional[str] = None):
202
202
203
203
return Pinecone (** pinecone_args )
204
204
205
+ def _calculate_index_host (self ):
206
+ if self .index_host and self .base_url :
207
+ if "api.pinecone.io" in self .base_url :
208
+ if not self .index_host .startswith ("http" ):
209
+ self .index_host = f"https://{ self .index_host } "
210
+ else :
211
+ if "http" not in self .index_host :
212
+ self .index_host = f"http://{ self .base_url .split (':' )[- 2 ].strip ('/' )} :{ self .index_host .split (':' )[- 1 ]} "
213
+ elif not self .index_host .startswith ("http://" ):
214
+ if "localhost" in self .index_host :
215
+ self .index_host = f"http://{ self .base_url .split (':' )[- 2 ].strip ('/' )} :{ self .index_host .split (':' )[- 1 ]} "
216
+ else :
217
+ self .index_host = f"http://{ self .index_host } "
218
+
205
219
def _init_index (self , force_create : bool = False ) -> Union [Any , None ]:
206
220
"""Initializing the index can be done after the object has been created
207
221
to allow for the user to set the dimensions and other parameters.
@@ -215,58 +229,59 @@ def _init_index(self, force_create: bool = False) -> Union[Any, None]:
215
229
dimensions are not given (which will raise an error).
216
230
:type force_create: bool, optional
217
231
"""
218
- index_exists = self .index_name in self .client .list_indexes ().names ()
219
232
dimensions_given = self .dimensions is not None
220
- if dimensions_given and not index_exists :
221
- # if the index doesn't exist and we have dimension value
222
- # we create the index
223
- self .client .create_index (
224
- name = self .index_name ,
225
- dimension = self .dimensions ,
226
- metric = self .metric ,
227
- spec = self .ServerlessSpec (cloud = self .cloud , region = self .region ),
228
- )
229
- # wait for index to be created
230
- while not self .client .describe_index (self .index_name ).status ["ready" ]:
231
- time .sleep (1 )
232
- index = self .client .Index (self .index_name )
233
- time .sleep (0.5 )
234
- elif index_exists :
233
+ if self .index is None :
234
+ index_exists = self .index_name in self .client .list_indexes ().names ()
235
+ if dimensions_given and not index_exists :
236
+ # if the index doesn't exist and we have dimension value
237
+ # we create the index
238
+ self .client .create_index (
239
+ name = self .index_name ,
240
+ dimension = self .dimensions ,
241
+ metric = self .metric ,
242
+ spec = self .ServerlessSpec (cloud = self .cloud , region = self .region ),
243
+ )
244
+ # wait for index to be created
245
+ while not self .client .describe_index (self .index_name ).status ["ready" ]:
246
+ time .sleep (0.2 )
247
+ index = self .client .Index (self .index_name )
248
+ self .index = index
249
+ time .sleep (0.2 )
250
+ elif index_exists :
251
+ # if the index exists we just return it
252
+ # index = self.client.Index(self.index_name)
253
+
254
+ self .index_host = self .client .describe_index (self .index_name ).host
255
+ self ._calculate_index_host ()
256
+ index = self .client .Index (self .index_name , host = self .index_host )
257
+ self .index = index
258
+
259
+ # grab the dimensions from the index
260
+ self .dimensions = index .describe_index_stats ()["dimension" ]
261
+ elif force_create and not dimensions_given :
262
+ raise ValueError (
263
+ "Cannot create an index without specifying the dimensions."
264
+ )
265
+ else :
266
+ # if the index doesn't exist and we don't have the dimensions
267
+ # we return None
268
+ logger .warning (
269
+ "Index could not be initialized. Init parameters: "
270
+ f"{ self .index_name = } , { self .dimensions = } , { self .metric = } , "
271
+ f"{ self .cloud = } , { self .region = } , { self .host = } , { self .namespace = } , "
272
+ f"{ force_create = } "
273
+ )
274
+ index = None
275
+ else :
276
+ index = self .index
277
+ if self .index is not None and self .host == "" :
235
278
# if the index exists we just return it
236
279
self .index_host = self .client .describe_index (self .index_name ).host
237
280
238
281
if self .index_host and self .base_url :
239
- if "api.pinecone.io" in self .base_url :
240
- if not self .index_host .startswith ("http" ):
241
- self .index_host = f"https://{ self .index_host } "
242
- else :
243
- if "http" not in self .index_host :
244
- self .index_host = f"http://{ self .base_url .split (':' )[- 2 ].strip ('/' )} :{ self .index_host .split (':' )[- 1 ]} "
245
- elif not self .index_host .startswith ("http://" ):
246
- if "localhost" in self .index_host :
247
- self .index_host = f"http://{ self .base_url .split (':' )[- 2 ].strip ('/' )} :{ self .index_host .split (':' )[- 1 ]} "
248
- else :
249
- self .index_host = f"http://{ self .index_host } "
282
+ self ._calculate_index_host ()
250
283
index = self .client .Index (self .index_name , host = self .index_host )
251
284
self .host = self .index_host
252
- # grab the dimensions from the index
253
- self .dimensions = index .describe_index_stats ()["dimension" ]
254
- elif force_create and not dimensions_given :
255
- raise ValueError (
256
- "Cannot create an index without specifying the dimensions."
257
- )
258
- else :
259
- # if the index doesn't exist and we don't have the dimensions
260
- # we return None
261
- logger .warning (
262
- "Index could not be initialized. Init parameters: "
263
- f"{ self .index_name = } , { self .dimensions = } , { self .metric = } , "
264
- f"{ self .cloud = } , { self .region = } , { self .host = } , { self .namespace = } , "
265
- f"{ force_create = } "
266
- )
267
- index = None
268
- if index is not None :
269
- self .host = self .client .describe_index (self .index_name )["host" ]
270
285
return index
271
286
272
287
async def _init_async_index (self , force_create : bool = False ):
0 commit comments