conversations.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. from typing import Any, Optional
  2. from uuid import UUID
  3. from shared.api.models.base import WrappedBooleanResponse
  4. from shared.api.models.management.responses import (
  5. WrappedConversationMessagesResponse,
  6. WrappedConversationResponse,
  7. WrappedConversationsResponse,
  8. WrappedMessageResponse,
  9. )
  10. class ConversationsSDK:
  11. def __init__(self, client):
  12. self.client = client
  13. async def create(
  14. self,
  15. name: Optional[str] = None,
  16. ) -> WrappedConversationResponse:
  17. """
  18. Create a new conversation.
  19. Returns:
  20. dict: Created conversation information
  21. """
  22. data = {"name": name} if name else None
  23. return await self.client._make_request(
  24. "POST",
  25. "conversations",
  26. data=data,
  27. version="v3",
  28. )
  29. async def list(
  30. self,
  31. ids: Optional[list[str | UUID]] = None,
  32. offset: Optional[int] = 0,
  33. limit: Optional[int] = 100,
  34. ) -> WrappedConversationsResponse:
  35. """
  36. List conversations with pagination and sorting options.
  37. Args:
  38. ids (Optional[list[Union[str, UUID]]]): List of conversation IDs to retrieve
  39. offset (int, optional): Specifies the number of objects to skip. Defaults to 0.
  40. limit (int, optional): Specifies a limit on the number of objects to return, ranging between 1 and 100. Defaults to 100.
  41. Returns:
  42. dict: List of conversations and pagination information
  43. """
  44. params: dict = {
  45. "offset": offset,
  46. "limit": limit,
  47. }
  48. if ids:
  49. params["ids"] = ids
  50. return await self.client._make_request(
  51. "GET",
  52. "conversations",
  53. params=params,
  54. version="v3",
  55. )
  56. async def retrieve(
  57. self,
  58. id: str | UUID,
  59. ) -> WrappedConversationMessagesResponse:
  60. """
  61. Get detailed information about a specific conversation.
  62. Args:
  63. id (Union[str, UUID]): The ID of the conversation to retrieve
  64. Returns:
  65. dict: Detailed conversation information
  66. """
  67. return await self.client._make_request(
  68. "GET",
  69. f"conversations/{str(id)}",
  70. version="v3",
  71. )
  72. async def update(
  73. self,
  74. id: str | UUID,
  75. name: str,
  76. ) -> WrappedConversationResponse:
  77. """
  78. Update an existing conversation.
  79. Args:
  80. id (Union[str, UUID]): The ID of the conversation to update
  81. name (str): The new name of the conversation
  82. Returns:
  83. dict: The updated conversation
  84. """
  85. data: dict[str, Any] = {
  86. "name": name,
  87. }
  88. return await self.client._make_request(
  89. "POST",
  90. f"conversations/{str(id)}",
  91. json=data,
  92. version="v3",
  93. )
  94. async def delete(
  95. self,
  96. id: str | UUID,
  97. ) -> WrappedBooleanResponse:
  98. """
  99. Delete a conversation.
  100. Args:
  101. id (Union[str, UUID]): The ID of the conversation to delete
  102. Returns:
  103. bool: True if deletion was successful
  104. """
  105. return await self.client._make_request(
  106. "DELETE",
  107. f"conversations/{str(id)}",
  108. version="v3",
  109. )
  110. async def add_message(
  111. self,
  112. id: str | UUID,
  113. content: str,
  114. role: str,
  115. metadata: Optional[dict] = None,
  116. parent_id: Optional[str] = None,
  117. ) -> WrappedMessageResponse:
  118. """
  119. Add a new message to a conversation.
  120. Args:
  121. id (Union[str, UUID]): The ID of the conversation to add the message to
  122. content (str): The content of the message
  123. role (str): The role of the message (e.g., "user" or "assistant")
  124. parent_id (Optional[str]): The ID of the parent message
  125. metadata (Optional[dict]): Additional metadata to attach to the message
  126. Returns:
  127. dict: Result of the operation, including the new message ID
  128. """
  129. data: dict[str, Any] = {
  130. "content": content,
  131. "role": role,
  132. }
  133. if parent_id:
  134. data["parent_id"] = parent_id
  135. if metadata:
  136. data["metadata"] = metadata
  137. return await self.client._make_request(
  138. "POST",
  139. f"conversations/{str(id)}/messages",
  140. json=data,
  141. version="v3",
  142. )
  143. async def update_message(
  144. self,
  145. id: str | UUID,
  146. message_id: str,
  147. content: Optional[str] = None,
  148. metadata: Optional[dict] = None,
  149. ) -> dict:
  150. """
  151. Update an existing message in a conversation.
  152. Args:
  153. id (str | UUID): The ID of the conversation containing the message
  154. message_id (str): The ID of the message to update
  155. content (str): The new content of the message
  156. metadata (dict): Additional metadata to attach to the message
  157. Returns:
  158. dict: Result of the operation, including the new message ID and branch ID
  159. """
  160. data: dict[str, Any] = {"content": content}
  161. if metadata:
  162. data["metadata"] = metadata
  163. return await self.client._make_request(
  164. "POST",
  165. f"conversations/{str(id)}/messages/{message_id}",
  166. json=data,
  167. version="v3",
  168. )