indices.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import json
  2. from typing import Optional
  3. from shared.api.models.base import WrappedGenericMessageResponse
  4. from shared.api.models.ingestion.responses import (
  5. WrappedListVectorIndicesResponse,
  6. )
  7. class IndicesSDK:
  8. def __init__(self, client):
  9. self.client = client
  10. async def create(
  11. self,
  12. config: dict,
  13. run_with_orchestration: Optional[bool] = True,
  14. ) -> WrappedGenericMessageResponse:
  15. """
  16. Create a new vector similarity search index in the database.
  17. Args:
  18. config (Union[dict, IndexConfig]): Configuration for the vector index.
  19. run_with_orchestration (Optional[bool]): Whether to run index creation as an orchestrated task.
  20. """
  21. if not isinstance(config, dict):
  22. config = config.model_dump()
  23. data = {
  24. "config": config,
  25. "run_with_orchestration": run_with_orchestration,
  26. }
  27. return await self.client._make_request(
  28. "POST",
  29. "indices",
  30. json=data,
  31. version="v3",
  32. )
  33. async def list(
  34. self,
  35. filters: Optional[dict] = None,
  36. offset: Optional[int] = 0,
  37. limit: Optional[int] = 10,
  38. ) -> WrappedListVectorIndicesResponse:
  39. """
  40. List existing vector similarity search indices with pagination support.
  41. Args:
  42. filters (Optional[dict]): Filter criteria for indices.
  43. offset (int, optional): Specifies the number of objects to skip. Defaults to 0.
  44. limit (int, optional): Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.
  45. Returns:
  46. WrappedListVectorIndicesResponse: The response containing the list of indices.
  47. """
  48. params: dict = {
  49. "offset": offset,
  50. "limit": limit,
  51. }
  52. if filters:
  53. params["filters"] = json.dumps(filters)
  54. return await self.client._make_request(
  55. "GET",
  56. "indices",
  57. params=params,
  58. version="v3",
  59. )
  60. async def retrieve(
  61. self,
  62. index_name: str,
  63. table_name: str = "vectors",
  64. ) -> dict:
  65. """
  66. Get detailed information about a specific vector index.
  67. Args:
  68. index_name (str): The name of the index to retrieve.
  69. table_name (str): The name of the table where the index is stored.
  70. Returns:
  71. WrappedGetIndexResponse: The response containing the index details.
  72. """
  73. return await self.client._make_request(
  74. "GET",
  75. f"indices/{table_name}/{index_name}",
  76. version="v3",
  77. )
  78. # async def update_index(
  79. # self,
  80. # id: Union[str, UUID],
  81. # config: dict, # Union[dict, IndexConfig],
  82. # run_with_orchestration: Optional[bool] = True,
  83. # ) -> dict:
  84. # """
  85. # Update an existing index's configuration.
  86. # Args:
  87. # id (Union[str, UUID]): The ID of the index to update.
  88. # config (Union[dict, IndexConfig]): The new configuration for the index.
  89. # run_with_orchestration (Optional[bool]): Whether to run the update as an orchestrated task.
  90. # Returns:
  91. # WrappedUpdateIndexResponse: The response containing the updated index details.
  92. # """
  93. # if not isinstance(config, dict):
  94. # config = config.model_dump()
  95. # data = {
  96. # "config": config,
  97. # "run_with_orchestration": run_with_orchestration,
  98. # }
  99. # return await self.client._make_request("POST", f"indices/{id}", json=data) # type: ignore
  100. async def delete(
  101. self,
  102. index_name: str,
  103. table_name: str = "vectors",
  104. ) -> WrappedGenericMessageResponse:
  105. """
  106. Delete an existing vector index.
  107. Args:
  108. index_name (str): The name of the index to retrieve.
  109. table_name (str): The name of the table where the index is stored.
  110. Returns:
  111. WrappedGetIndexResponse: The response containing the index details.
  112. """
  113. return await self.client._make_request(
  114. "DELETE",
  115. f"indices/{table_name}/{index_name}",
  116. version="v3",
  117. )