chunks.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import json
  2. from typing import Any, Optional
  3. from uuid import UUID
  4. from shared.api.models import (
  5. WrappedBooleanResponse,
  6. WrappedChunkResponse,
  7. WrappedChunksResponse,
  8. WrappedVectorSearchResponse,
  9. )
  10. from ..models import SearchSettings
  11. class ChunksSDK:
  12. """SDK for interacting with chunks in the v3 API."""
  13. def __init__(self, client):
  14. self.client = client
  15. def update(
  16. self,
  17. chunk: dict[str, str],
  18. ) -> WrappedChunkResponse:
  19. """Update an existing chunk.
  20. Args:
  21. chunk (dict[str, str]): Chunk to update. Should contain:
  22. - id: UUID of the chunk
  23. - metadata: Dictionary of metadata
  24. Returns:
  25. WrappedChunkResponse
  26. """
  27. response_dict = self.client._make_request(
  28. "POST",
  29. f"chunks/{str(chunk['id'])}",
  30. json=chunk,
  31. version="v3",
  32. )
  33. return WrappedChunkResponse(**response_dict)
  34. def retrieve(
  35. self,
  36. id: str | UUID,
  37. ) -> WrappedChunkResponse:
  38. """Get a specific chunk.
  39. Args:
  40. id (str | UUID): Chunk ID to retrieve
  41. Returns:
  42. WrappedChunkResponse
  43. """
  44. response_dict = self.client._make_request(
  45. "GET",
  46. f"chunks/{id}",
  47. version="v3",
  48. )
  49. return WrappedChunkResponse(**response_dict)
  50. # FIXME: Is this the most appropriate name for this method?
  51. def list_by_document(
  52. self,
  53. document_id: str | UUID,
  54. metadata_filter: Optional[dict] = None,
  55. offset: Optional[int] = 0,
  56. limit: Optional[int] = 100,
  57. ) -> WrappedChunksResponse:
  58. """List chunks for a specific document.
  59. Args:
  60. document_id (str | UUID): Document ID to get chunks for
  61. metadata_filter (Optional[dict]): Filter chunks by metadata
  62. offset (int, optional): Specifies the number of objects to skip. Defaults to 0.
  63. limit (int, optional): Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.
  64. Returns:
  65. WrappedChunksResponse
  66. """
  67. params: dict = {
  68. "offset": offset,
  69. "limit": limit,
  70. }
  71. if metadata_filter:
  72. params["metadata_filter"] = json.dumps(metadata_filter)
  73. response_dict = self.client._make_request(
  74. "GET",
  75. f"documents/{str(document_id)}/chunks",
  76. params=params,
  77. version="v3",
  78. )
  79. return WrappedChunksResponse(**response_dict)
  80. def delete(
  81. self,
  82. id: str | UUID,
  83. ) -> WrappedBooleanResponse:
  84. """Delete a specific chunk.
  85. Args:
  86. id (str | UUID): ID of chunk to delete
  87. Returns:
  88. WrappedBooleanResponse
  89. """
  90. response_dict = self.client._make_request(
  91. "DELETE",
  92. f"chunks/{str(id)}",
  93. version="v3",
  94. )
  95. return WrappedBooleanResponse(**response_dict)
  96. def list(
  97. self,
  98. include_vectors: bool = False,
  99. metadata_filter: Optional[dict] = None,
  100. offset: Optional[int] = 0,
  101. limit: Optional[int] = 100,
  102. filters: Optional[dict] = None,
  103. ) -> WrappedChunksResponse:
  104. """List chunks with pagination support.
  105. Args:
  106. include_vectors (bool, optional): Include vector data in response. Defaults to False.
  107. metadata_filter (Optional[dict], optional): Filter by metadata. Defaults to None.
  108. offset (int, optional): Specifies the number of objects to skip. Defaults to 0.
  109. limit (int, optional): Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.
  110. Returns:
  111. WrappedChunksResponse
  112. """
  113. params: dict = {
  114. "offset": offset,
  115. "limit": limit,
  116. "include_vectors": include_vectors,
  117. }
  118. if filters:
  119. params["filters"] = json.dumps(filters)
  120. if metadata_filter:
  121. params["metadata_filter"] = json.dumps(metadata_filter)
  122. response_dict = self.client._make_request(
  123. "GET",
  124. "chunks",
  125. params=params,
  126. version="v3",
  127. )
  128. return WrappedChunksResponse(**response_dict)
  129. def search(
  130. self,
  131. query: str,
  132. search_settings: Optional[dict | SearchSettings] = None,
  133. ) -> WrappedVectorSearchResponse:
  134. """Conduct a vector and/or graph search.
  135. Args:
  136. query (str): The query to search for.
  137. search_settings (Optional[dict, SearchSettings]]): Vector search settings.
  138. Returns:
  139. WrappedVectorSearchResponse
  140. """
  141. if search_settings and not isinstance(search_settings, dict):
  142. search_settings = search_settings.model_dump()
  143. data: dict[str, Any] = {
  144. "query": query,
  145. "search_settings": search_settings,
  146. }
  147. response_dict = self.client._make_request(
  148. "POST",
  149. "chunks/search",
  150. json=data,
  151. version="v3",
  152. )
  153. return WrappedVectorSearchResponse(**response_dict)