|
| 1 | +import logging |
| 2 | +from typing import Optional, Dict, Any |
| 3 | + |
| 4 | +from pinecone.config import ConfigBuilder |
| 5 | + |
| 6 | + |
| 7 | +from pinecone.core.openapi.ckb_knowledge_data.api.document_operations_api import ( |
| 8 | + DocumentOperationsApi, |
| 9 | +) |
| 10 | +from pinecone.core.openapi.ckb_knowledge_data import API_VERSION |
| 11 | + |
| 12 | +from pinecone.openapi_support import ApiClient |
| 13 | +from pinecone.core.openapi.ckb_knowledge_data.models import ( |
| 14 | + UpsertDocumentResponse, |
| 15 | + GetDocumentResponse, |
| 16 | + ListDocumentsResponse, |
| 17 | +) |
| 18 | + |
| 19 | +from .interfaces import RepositoryInterface |
| 20 | + |
| 21 | +from pinecone.utils import setup_openapi_client |
| 22 | + |
| 23 | +from multiprocessing import cpu_count |
| 24 | + |
| 25 | + |
| 26 | +logger = logging.getLogger(__name__) |
| 27 | +""" :meta private: """ |
| 28 | + |
| 29 | + |
| 30 | +class Repository(RepositoryInterface): |
| 31 | + """ |
| 32 | + A client for interacting with a Pinecone Repository API. |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__( |
| 36 | + self, |
| 37 | + api_key: str, |
| 38 | + host: str, |
| 39 | + pool_threads: Optional[int] = None, |
| 40 | + additional_headers: Optional[Dict[str, str]] = {}, |
| 41 | + openapi_config=None, |
| 42 | + **kwargs, |
| 43 | + ): |
| 44 | + self._config = ConfigBuilder.build( |
| 45 | + api_key=api_key, host=host, additional_headers=additional_headers, **kwargs |
| 46 | + ) |
| 47 | + """ :meta private: """ |
| 48 | + self._openapi_config = ConfigBuilder.build_openapi_config(self._config, openapi_config) |
| 49 | + """ :meta private: """ |
| 50 | + |
| 51 | + if pool_threads is None: |
| 52 | + self._pool_threads = 5 * cpu_count() |
| 53 | + """ :meta private: """ |
| 54 | + else: |
| 55 | + self._pool_threads = pool_threads |
| 56 | + """ :meta private: """ |
| 57 | + |
| 58 | + if kwargs.get("connection_pool_maxsize", None): |
| 59 | + self._openapi_config.connection_pool_maxsize = kwargs.get("connection_pool_maxsize") |
| 60 | + |
| 61 | + self._repository_api = setup_openapi_client( |
| 62 | + api_client_klass=ApiClient, |
| 63 | + api_klass=DocumentOperationsApi, |
| 64 | + config=self._config, |
| 65 | + openapi_config=self._openapi_config, |
| 66 | + pool_threads=self._pool_threads, |
| 67 | + api_version=API_VERSION, |
| 68 | + ) |
| 69 | + |
| 70 | + self._api_client = self._repository_api.api_client |
| 71 | + |
| 72 | + def upsert(self, namespace: str, document: Dict[str, Any], **kwargs) -> UpsertDocumentResponse: |
| 73 | + return self._repository_api.api_client.call_api( |
| 74 | + resource_path=f"/ckb-stub-namespaces/{namespace}/documents/upsert", |
| 75 | + method="POST", |
| 76 | + path_params={"namespace": namespace}, |
| 77 | + body=document, |
| 78 | + header_params={"Content-Type": "application/json", "Accept": "application/json"}, |
| 79 | + response_type=(UpsertDocumentResponse,), |
| 80 | + _return_http_data_only=True, |
| 81 | + ) |
| 82 | + |
| 83 | + def fetch(self, namespace: str, document_id: str, **kwargs) -> GetDocumentResponse: |
| 84 | + return self._repository_api.get_document( |
| 85 | + namespace=namespace, document_id=document_id, **kwargs |
| 86 | + ) |
| 87 | + |
| 88 | + def list(self, namespace: str, **kwargs) -> ListDocumentsResponse: |
| 89 | + return self._repository_api.list_documents(namespace=namespace, **kwargs) |
0 commit comments