Skip to content

Commit 8a2afef

Browse files
authored
Add alternative constructor for AsyncioIndex (#451)
## Problem If you only care about doing async index oeprations, it's cumbersome to have to manage nested async contexts by going through the `PineconeAsyncio` class. ## Solution This is a very simple change that enables people to create an async index client. ## Usage You can now do index operations via asyncio like this: ```python import asyncio from pinecone import Pinecone async def main(): async with Pinecone().IndexAsyncio(host='myhost') as idx: await idx.uspert(...) # do async things asyncio.run(main()) ``` ## Type of Change - [x] New feature (non-breaking change which adds functionality)
1 parent b510e21 commit 8a2afef

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

pinecone/control/pinecone.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from .langchain_import_warnings import _build_langchain_attribute_error_message
2626
from pinecone.utils import docslinks
2727

28-
from pinecone.data import _Index, _Inference
28+
from pinecone.data import _Index, _Inference, _AsyncioIndex
2929
from pinecone.enums import (
3030
Metric,
3131
VectorType,
@@ -314,6 +314,24 @@ def Index(self, name: str = "", host: str = "", **kwargs):
314314
**kwargs,
315315
)
316316

317+
def IndexAsyncio(self, host: str, **kwargs):
318+
api_key = self.config.api_key
319+
openapi_config = self.openapi_config
320+
321+
if host is None or host == "":
322+
raise ValueError("A host must be specified")
323+
324+
check_realistic_host(host)
325+
index_host = normalize_host(host)
326+
327+
return _AsyncioIndex(
328+
host=index_host,
329+
api_key=api_key,
330+
openapi_config=openapi_config,
331+
source_tag=self.config.source_tag,
332+
**kwargs,
333+
)
334+
317335

318336
def check_realistic_host(host: str) -> None:
319337
"""@private
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pytest
2+
from pinecone import Pinecone
3+
from ..helpers import random_string, embedding_values
4+
5+
6+
@pytest.mark.asyncio
7+
async def test_instantiation_through_non_async_client(index_host, dimension):
8+
asyncio_idx = Pinecone().IndexAsyncio(host=index_host)
9+
10+
def emb():
11+
return embedding_values(dimension)
12+
13+
# Upsert with tuples
14+
await asyncio_idx.upsert(
15+
vectors=[("1", emb()), ("2", emb()), ("3", emb())], namespace=random_string(10)
16+
)
17+
18+
await asyncio_idx.close()

0 commit comments

Comments
 (0)