Skip to content

Commit 9a1af74

Browse files
committed
Update docstrings and type hints
1 parent e872037 commit 9a1af74

File tree

2 files changed

+94
-10
lines changed

2 files changed

+94
-10
lines changed

pinecone/db_data/index.py

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,12 @@ def __init__(
9595
api_key: str,
9696
host: str,
9797
pool_threads: Optional[int] = None,
98-
additional_headers: Optional[Dict[str, str]] = {},
98+
additional_headers: Optional[Dict[str, str]] = None,
9999
openapi_config=None,
100100
**kwargs,
101101
):
102+
if additional_headers is None:
103+
additional_headers = {}
102104
self._config = ConfigBuilder.build(
103105
api_key=api_key, host=host, additional_headers=additional_headers, **kwargs
104106
)
@@ -133,8 +135,8 @@ def __init__(
133135
self._namespace_resource = None
134136
""" :meta private: """
135137

136-
# Pass the same api_client to the ImportFeatureMixin
137-
super().__init__(api_client=self._api_client)
138+
# Initialize PluginAware parent class
139+
super().__init__()
138140

139141
@property
140142
def config(self) -> "Config":
@@ -281,7 +283,47 @@ def upsert_from_dataframe(
281283

282284
return UpsertResponse(upserted_count=upserted_count)
283285

284-
def upsert_records(self, namespace: str, records: List[Dict]):
286+
@validate_and_convert_errors
287+
def upsert_records(self, namespace: str, records: List[Dict[str, Any]]):
288+
"""Upsert records to a namespace.
289+
290+
Upsert records to a namespace. A record is a dictionary that contains either an ``id`` or ``_id``
291+
field along with other fields that will be stored as metadata. The ``id`` or ``_id`` field is used
292+
as the unique identifier for the record. At least one field in the record should correspond to
293+
a field mapping in the index's embed configuration.
294+
295+
When records are upserted, Pinecone converts mapped fields into embeddings and upserts them into
296+
the specified namespace of the index.
297+
298+
Args:
299+
namespace (str): The namespace of the index to upsert records to.
300+
records (List[Dict[str, Any]]): The records to upsert into the index.
301+
Each record should contain either an ``id`` or ``_id`` field.
302+
303+
Examples:
304+
305+
.. code-block:: python
306+
307+
>>> from pinecone import Pinecone, CloudProvider, AwsRegion, EmbedModel, IndexEmbed
308+
>>> pc = Pinecone(api_key="<<PINECONE_API_KEY>>")
309+
>>> index_model = pc.create_index_for_model(
310+
... name="my-model-index",
311+
... cloud=CloudProvider.AWS,
312+
... region=AwsRegion.US_WEST_2,
313+
... embed=IndexEmbed(
314+
... model=EmbedModel.Multilingual_E5_Large,
315+
... field_map={"text": "my_text_field"}
316+
... )
317+
... )
318+
>>> idx = pc.Index(host=index_model.host)
319+
>>> idx.upsert_records(
320+
... namespace="my-namespace",
321+
... records=[
322+
... {"_id": "test1", "my_text_field": "Apple is a popular fruit."},
323+
... {"_id": "test2", "my_text_field": "The tech company Apple is innovative."},
324+
... ],
325+
... )
326+
"""
285327
args = IndexRequestFactory.upsert_records_args(namespace=namespace, records=records)
286328
self._vector_api.upsert_records_namespace(**args)
287329

@@ -293,9 +335,6 @@ def search(
293335
rerank: Optional[Union[SearchRerankTypedDict, SearchRerank]] = None,
294336
fields: Optional[List[str]] = ["*"], # Default to returning all fields
295337
) -> SearchRecordsResponse:
296-
if namespace is None:
297-
raise Exception("Namespace is required when searching records")
298-
299338
request = IndexRequestFactory.search_request(query=query, rerank=rerank, fields=fields)
300339

301340
return self._vector_api.search_records_namespace(namespace, request)

pinecone/db_data/index_asyncio.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,12 @@ def __init__(
159159
self,
160160
api_key: str,
161161
host: str,
162-
additional_headers: Optional[Dict[str, str]] = {},
162+
additional_headers: Optional[Dict[str, str]] = None,
163163
openapi_config=None,
164164
**kwargs,
165165
):
166+
if additional_headers is None:
167+
additional_headers = {}
166168
self.config = ConfigBuilder.build(
167169
api_key=api_key, host=host, additional_headers=additional_headers, **kwargs
168170
)
@@ -613,7 +615,50 @@ async def list(self, **kwargs):
613615
else:
614616
done = True
615617

616-
async def upsert_records(self, namespace: str, records: List[Dict]):
618+
@validate_and_convert_errors
619+
async def upsert_records(self, namespace: str, records: List[Dict[str, Any]]):
620+
"""Upsert records to a namespace.
621+
622+
Upsert records to a namespace. A record is a dictionary that contains either an ``id`` or ``_id``
623+
field along with other fields that will be stored as metadata. The ``id`` or ``_id`` field is used
624+
as the unique identifier for the record. At least one field in the record should correspond to
625+
a field mapping in the index's embed configuration.
626+
627+
When records are upserted, Pinecone converts mapped fields into embeddings and upserts them into
628+
the specified namespace of the index.
629+
630+
Args:
631+
namespace (str): The namespace of the index to upsert records to.
632+
records (List[Dict[str, Any]]): The records to upsert into the index.
633+
Each record should contain either an ``id`` or ``_id`` field.
634+
635+
Examples:
636+
637+
.. code-block:: python
638+
639+
>>> import asyncio
640+
>>> from pinecone import Pinecone, CloudProvider, AwsRegion, EmbedModel, IndexEmbed
641+
>>> async def main():
642+
... pc = Pinecone(api_key="<<PINECONE_API_KEY>>")
643+
... index_model = pc.create_index_for_model(
644+
... name="my-model-index",
645+
... cloud=CloudProvider.AWS,
646+
... region=AwsRegion.US_WEST_2,
647+
... embed=IndexEmbed(
648+
... model=EmbedModel.Multilingual_E5_Large,
649+
... field_map={"text": "my_text_field"}
650+
... )
651+
... )
652+
... async with pc.IndexAsyncio(host=index_model.host) as idx:
653+
... await idx.upsert_records(
654+
... namespace="my-namespace",
655+
... records=[
656+
... {"_id": "test1", "my_text_field": "Apple is a popular fruit."},
657+
... {"_id": "test2", "my_text_field": "The tech company Apple is innovative."},
658+
... ],
659+
... )
660+
>>> asyncio.run(main())
661+
"""
617662
args = IndexRequestFactory.upsert_records_args(namespace=namespace, records=records)
618663
await self._vector_api.upsert_records_namespace(**args)
619664

@@ -625,7 +670,7 @@ async def search(
625670
fields: Optional[List[str]] = ["*"], # Default to returning all fields
626671
) -> SearchRecordsResponse:
627672
if namespace is None:
628-
raise Exception("Namespace is required when searching records")
673+
raise ValueError("Namespace is required when searching records")
629674

630675
request = IndexRequestFactory.search_request(query=query, rerank=rerank, fields=fields)
631676

0 commit comments

Comments
 (0)