conversation.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from __future__ import annotations
  2. from ...requests import StreamSession, raise_for_status
  3. from ...errors import RateLimitError
  4. from ...providers.response import BaseConversation
  5. class Conversation(BaseConversation):
  6. """
  7. Represents a conversation with specific attributes.
  8. """
  9. def __init__(self, conversationId: str, clientId: str, conversationSignature: str) -> None:
  10. """
  11. Initialize a new conversation instance.
  12. Args:
  13. conversationId (str): Unique identifier for the conversation.
  14. clientId (str): Client identifier.
  15. conversationSignature (str): Signature for the conversation.
  16. """
  17. self.conversationId = conversationId
  18. self.clientId = clientId
  19. self.conversationSignature = conversationSignature
  20. async def create_conversation(session: StreamSession, headers: dict, tone: str) -> Conversation:
  21. """
  22. Create a new conversation asynchronously.
  23. Args:
  24. session (ClientSession): An instance of aiohttp's ClientSession.
  25. proxy (str, optional): Proxy URL. Defaults to None.
  26. Returns:
  27. Conversation: An instance representing the created conversation.
  28. """
  29. if tone == "Copilot":
  30. url = "https://copilot.microsoft.com/turing/conversation/create?bundleVersion=1.1809.0"
  31. else:
  32. url = "https://www.bing.com/turing/conversation/create?bundleVersion=1.1809.0"
  33. async with session.get(url, headers=headers) as response:
  34. if response.status == 404:
  35. raise RateLimitError("Response 404: Do less requests and reuse conversations")
  36. await raise_for_status(response, "Failed to create conversation")
  37. data = await response.json()
  38. if not data:
  39. raise RuntimeError('Empty response: Failed to create conversation')
  40. conversationId = data.get('conversationId')
  41. clientId = data.get('clientId')
  42. conversationSignature = response.headers.get('X-Sydney-Encryptedconversationsignature')
  43. if not conversationId or not clientId or not conversationSignature:
  44. raise RuntimeError('Empty fields: Failed to create conversation')
  45. return Conversation(conversationId, clientId, conversationSignature)
  46. async def list_conversations(session: StreamSession) -> list:
  47. """
  48. List all conversations asynchronously.
  49. Args:
  50. session (ClientSession): An instance of aiohttp's ClientSession.
  51. Returns:
  52. list: A list of conversations.
  53. """
  54. url = "https://www.bing.com/turing/conversation/chats"
  55. async with session.get(url) as response:
  56. response = await response.json()
  57. return response["chats"]
  58. async def delete_conversation(session: StreamSession, conversation: Conversation, headers: dict) -> bool:
  59. """
  60. Delete a conversation asynchronously.
  61. Args:
  62. session (ClientSession): An instance of aiohttp's ClientSession.
  63. conversation (Conversation): The conversation to delete.
  64. proxy (str, optional): Proxy URL. Defaults to None.
  65. Returns:
  66. bool: True if deletion was successful, False otherwise.
  67. """
  68. url = "https://sydney.bing.com/sydney/DeleteSingleConversation"
  69. json = {
  70. "conversationId": conversation.conversationId,
  71. "conversationSignature": conversation.conversationSignature,
  72. "participant": {"id": conversation.clientId},
  73. "source": "cib",
  74. "optionsSets": ["autosave"]
  75. }
  76. try:
  77. async with session.post(url, json=json, headers=headers) as response:
  78. response = await response.json()
  79. return response["result"]["value"] == "Success"
  80. except:
  81. return False