indices.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import json
  2. from typing import Any, 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 (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. 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 delete(
  79. self,
  80. index_name: str,
  81. table_name: str = "vectors",
  82. ) -> WrappedGenericMessageResponse:
  83. """
  84. Delete an existing vector index.
  85. Args:
  86. index_name (str): The name of the index to retrieve.
  87. table_name (str): The name of the table where the index is stored.
  88. Returns:
  89. WrappedGetIndexResponse: The response containing the index details.
  90. """
  91. return await self.client._make_request(
  92. "DELETE",
  93. f"indices/{table_name}/{index_name}",
  94. version="v3",
  95. )