indices.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import json
  2. from typing import Any, Optional
  3. from shared.api.models import (
  4. WrappedGenericMessageResponse,
  5. WrappedVectorIndexResponse,
  6. WrappedVectorIndicesResponse,
  7. )
  8. class IndicesSDK:
  9. def __init__(self, client):
  10. self.client = client
  11. async def create(
  12. self,
  13. config: dict,
  14. run_with_orchestration: Optional[bool] = True,
  15. ) -> WrappedGenericMessageResponse:
  16. """Create a new vector similarity search index in the database.
  17. Args:
  18. config (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: dict[str, Any] = {
  24. "config": config,
  25. "run_with_orchestration": run_with_orchestration,
  26. }
  27. response_dict = await self.client._make_request(
  28. "POST",
  29. "indices",
  30. json=data,
  31. version="v3",
  32. )
  33. return WrappedGenericMessageResponse(**response_dict)
  34. async def list(
  35. self,
  36. filters: Optional[dict] = None,
  37. offset: Optional[int] = 0,
  38. limit: Optional[int] = 10,
  39. ) -> WrappedVectorIndicesResponse:
  40. """List existing vector similarity search indices with pagination
  41. support.
  42. Args:
  43. filters (Optional[dict]): Filter criteria for indices.
  44. offset (int, optional): Specifies the number of objects to skip. Defaults to 0.
  45. limit (int, optional): Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.
  46. Returns:
  47. WrappedVectorIndicesResponse
  48. """
  49. params: dict = {
  50. "offset": offset,
  51. "limit": limit,
  52. }
  53. if filters:
  54. params["filters"] = json.dumps(filters)
  55. response_dict = await self.client._make_request(
  56. "GET",
  57. "indices",
  58. params=params,
  59. version="v3",
  60. )
  61. return WrappedVectorIndicesResponse(**response_dict)
  62. async def retrieve(
  63. self,
  64. index_name: str,
  65. table_name: str = "vectors",
  66. ) -> WrappedVectorIndexResponse:
  67. """Get detailed information about a specific vector index.
  68. Args:
  69. index_name (str): The name of the index to retrieve.
  70. table_name (str): The name of the table where the index is stored.
  71. Returns:
  72. WrappedGetIndexResponse: The response containing the index details.
  73. """
  74. response_dict = await self.client._make_request(
  75. "GET",
  76. f"indices/{table_name}/{index_name}",
  77. version="v3",
  78. )
  79. return WrappedVectorIndexResponse(**response_dict)
  80. async def delete(
  81. self,
  82. index_name: str,
  83. table_name: str = "vectors",
  84. ) -> WrappedGenericMessageResponse:
  85. """Delete an existing vector index.
  86. Args:
  87. index_name (str): The name of the index to retrieve.
  88. table_name (str): The name of the table where the index is stored.
  89. Returns:
  90. WrappedGetIndexResponse: The response containing the index details.
  91. """
  92. response_dict = await self.client._make_request(
  93. "DELETE",
  94. f"indices/{table_name}/{index_name}",
  95. version="v3",
  96. )
  97. return WrappedGenericMessageResponse(**response_dict)