indices.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. 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. Returns:
  21. WrappedGenericMessageResponse
  22. """
  23. if not isinstance(config, dict):
  24. config = config.model_dump()
  25. data: dict[str, Any] = {
  26. "config": config,
  27. "run_with_orchestration": run_with_orchestration,
  28. }
  29. response_dict = self.client._make_request(
  30. "POST",
  31. "indices",
  32. json=data,
  33. version="v3",
  34. )
  35. return WrappedGenericMessageResponse(**response_dict)
  36. def list(
  37. self,
  38. filters: Optional[dict] = None,
  39. offset: Optional[int] = 0,
  40. limit: Optional[int] = 10,
  41. ) -> WrappedVectorIndicesResponse:
  42. """List existing vector similarity search indices with pagination
  43. support.
  44. Args:
  45. filters (Optional[dict]): Filter criteria for indices.
  46. offset (int, optional): Specifies the number of objects to skip. Defaults to 0.
  47. limit (int, optional): Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.
  48. Returns:
  49. WrappedVectorIndicesResponse
  50. """
  51. params: dict = {
  52. "offset": offset,
  53. "limit": limit,
  54. }
  55. if filters:
  56. params["filters"] = json.dumps(filters)
  57. response_dict = self.client._make_request(
  58. "GET",
  59. "indices",
  60. params=params,
  61. version="v3",
  62. )
  63. return WrappedVectorIndicesResponse(**response_dict)
  64. def retrieve(
  65. self,
  66. index_name: str,
  67. table_name: str = "vectors",
  68. ) -> WrappedVectorIndexResponse:
  69. """Get detailed information about a specific vector index.
  70. Args:
  71. index_name (str): The name of the index to retrieve.
  72. table_name (str): The name of the table where the index is stored.
  73. Returns:
  74. WrappedGetIndexResponse
  75. """
  76. response_dict = self.client._make_request(
  77. "GET",
  78. f"indices/{table_name}/{index_name}",
  79. version="v3",
  80. )
  81. return WrappedVectorIndexResponse(**response_dict)
  82. def delete(
  83. self,
  84. index_name: str,
  85. table_name: str = "vectors",
  86. ) -> WrappedGenericMessageResponse:
  87. """Delete an existing vector index.
  88. Args:
  89. index_name (str): The name of the index to retrieve.
  90. table_name (str): The name of the table where the index is stored.
  91. Returns:
  92. WrappedGetIndexResponse
  93. """
  94. response_dict = self.client._make_request(
  95. "DELETE",
  96. f"indices/{table_name}/{index_name}",
  97. version="v3",
  98. )
  99. return WrappedGenericMessageResponse(**response_dict)