conversations.py 4.6 KB

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