Bing.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. from __future__ import annotations
  2. import random
  3. import json
  4. import os
  5. import uuid
  6. import time
  7. from urllib import parse
  8. from aiohttp import ClientSession, ClientTimeout
  9. from ..typing import AsyncResult, Messages, ImageType
  10. from ..image import ImageResponse
  11. from .base_provider import AsyncGeneratorProvider
  12. from .bing.upload_image import upload_image
  13. from .bing.create_images import create_images
  14. from .bing.conversation import Conversation, create_conversation, delete_conversation
  15. class Tones:
  16. """
  17. Defines the different tone options for the Bing provider.
  18. """
  19. creative = "Creative"
  20. balanced = "Balanced"
  21. precise = "Precise"
  22. class Bing(AsyncGeneratorProvider):
  23. """
  24. Bing provider for generating responses using the Bing API.
  25. """
  26. url = "https://bing.com/chat"
  27. working = True
  28. supports_message_history = True
  29. supports_gpt_4 = True
  30. @staticmethod
  31. def create_async_generator(
  32. model: str,
  33. messages: Messages,
  34. proxy: str = None,
  35. timeout: int = 900,
  36. cookies: dict = None,
  37. tone: str = Tones.balanced,
  38. image: ImageType = None,
  39. web_search: bool = False,
  40. **kwargs
  41. ) -> AsyncResult:
  42. """
  43. Creates an asynchronous generator for producing responses from Bing.
  44. :param model: The model to use.
  45. :param messages: Messages to process.
  46. :param proxy: Proxy to use for requests.
  47. :param timeout: Timeout for requests.
  48. :param cookies: Cookies for the session.
  49. :param tone: The tone of the response.
  50. :param image: The image type to be used.
  51. :param web_search: Flag to enable or disable web search.
  52. :return: An asynchronous result object.
  53. """
  54. if len(messages) < 2:
  55. prompt = messages[0]["content"]
  56. context = None
  57. else:
  58. prompt = messages[-1]["content"]
  59. context = create_context(messages[:-1])
  60. if not cookies:
  61. cookies = Defaults.cookies
  62. else:
  63. for key, value in Defaults.cookies.items():
  64. if key not in cookies:
  65. cookies[key] = value
  66. gpt4_turbo = True if model.startswith("gpt-4-turbo") else False
  67. return stream_generate(prompt, tone, image, context, proxy, cookies, web_search, gpt4_turbo, timeout)
  68. def create_context(messages: Messages) -> str:
  69. """
  70. Creates a context string from a list of messages.
  71. :param messages: A list of message dictionaries.
  72. :return: A string representing the context created from the messages.
  73. """
  74. return "".join(
  75. f"[{message['role']}]" + ("(#message)" if message['role'] != "system" else "(#additional_instructions)") + f"\n{message['content']}\n\n"
  76. for message in messages
  77. )
  78. class Defaults:
  79. """
  80. Default settings and configurations for the Bing provider.
  81. """
  82. delimiter = "\x1e"
  83. ip_address = f"13.{random.randint(104, 107)}.{random.randint(0, 255)}.{random.randint(0, 255)}"
  84. # List of allowed message types for Bing responses
  85. allowedMessageTypes = [
  86. "ActionRequest", "Chat", "Context", "Progress", "SemanticSerp",
  87. "GenerateContentQuery", "SearchQuery", "RenderCardRequest"
  88. ]
  89. sliceIds = [
  90. 'abv2', 'srdicton', 'convcssclick', 'stylewv2', 'contctxp2tf',
  91. '802fluxv1pc_a', '806log2sphs0', '727savemem', '277teditgnds0', '207hlthgrds0'
  92. ]
  93. # Default location settings
  94. location = {
  95. "locale": "en-US", "market": "en-US", "region": "US",
  96. "locationHints": [{
  97. "country": "United States", "state": "California", "city": "Los Angeles",
  98. "timezoneoffset": 8, "countryConfidence": 8,
  99. "Center": {"Latitude": 34.0536909, "Longitude": -118.242766},
  100. "RegionType": 2, "SourceType": 1
  101. }],
  102. }
  103. # Default headers for requests
  104. headers = {
  105. 'accept': '*/*',
  106. 'accept-language': 'en-US,en;q=0.9',
  107. 'cache-control': 'max-age=0',
  108. 'sec-ch-ua': '"Chromium";v="110", "Not A(Brand";v="24", "Microsoft Edge";v="110"',
  109. 'sec-ch-ua-arch': '"x86"',
  110. 'sec-ch-ua-bitness': '"64"',
  111. 'sec-ch-ua-full-version': '"110.0.1587.69"',
  112. 'sec-ch-ua-full-version-list': '"Chromium";v="110.0.5481.192", "Not A(Brand";v="24.0.0.0", "Microsoft Edge";v="110.0.1587.69"',
  113. 'sec-ch-ua-mobile': '?0',
  114. 'sec-ch-ua-model': '""',
  115. 'sec-ch-ua-platform': '"Windows"',
  116. 'sec-ch-ua-platform-version': '"15.0.0"',
  117. 'sec-fetch-dest': 'document',
  118. 'sec-fetch-mode': 'navigate',
  119. 'sec-fetch-site': 'none',
  120. 'sec-fetch-user': '?1',
  121. 'upgrade-insecure-requests': '1',
  122. 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.69',
  123. 'x-edge-shopping-flag': '1',
  124. 'x-forwarded-for': ip_address,
  125. }
  126. optionsSets = [
  127. 'nlu_direct_response_filter', 'deepleo', 'disable_emoji_spoken_text',
  128. 'responsible_ai_policy_235', 'enablemm', 'iyxapbing', 'iycapbing',
  129. 'gencontentv3', 'fluxsrtrunc', 'fluxtrunc', 'fluxv1', 'rai278',
  130. 'replaceurl', 'eredirecturl', 'nojbfedge'
  131. ]
  132. # Default cookies
  133. cookies = {
  134. 'SRCHD' : 'AF=NOFORM',
  135. 'PPLState' : '1',
  136. 'KievRPSSecAuth': '',
  137. 'SUID' : '',
  138. 'SRCHUSR' : '',
  139. 'SRCHHPGUSR' : f'HV={int(time.time())}',
  140. }
  141. def format_message(msg: dict) -> str:
  142. """
  143. Formats a message dictionary into a JSON string with a delimiter.
  144. :param msg: The message dictionary to format.
  145. :return: A formatted string representation of the message.
  146. """
  147. return json.dumps(msg, ensure_ascii=False) + Defaults.delimiter
  148. def create_message(
  149. conversation: Conversation,
  150. prompt: str,
  151. tone: str,
  152. context: str = None,
  153. image_response: ImageResponse = None,
  154. web_search: bool = False,
  155. gpt4_turbo: bool = False
  156. ) -> str:
  157. """
  158. Creates a message for the Bing API with specified parameters.
  159. :param conversation: The current conversation object.
  160. :param prompt: The user's input prompt.
  161. :param tone: The desired tone for the response.
  162. :param context: Additional context for the prompt.
  163. :param image_response: The response if an image is involved.
  164. :param web_search: Flag to enable web search.
  165. :param gpt4_turbo: Flag to enable GPT-4 Turbo.
  166. :return: A formatted string message for the Bing API.
  167. """
  168. options_sets = Defaults.optionsSets
  169. # Append tone-specific options
  170. if tone == Tones.creative:
  171. options_sets.append("h3imaginative")
  172. elif tone == Tones.precise:
  173. options_sets.append("h3precise")
  174. elif tone == Tones.balanced:
  175. options_sets.append("galileo")
  176. else:
  177. options_sets.append("harmonyv3")
  178. # Additional configurations based on parameters
  179. if not web_search:
  180. options_sets.append("nosearchall")
  181. if gpt4_turbo:
  182. options_sets.append("dlgpt4t")
  183. request_id = str(uuid.uuid4())
  184. struct = {
  185. 'arguments': [{
  186. 'source': 'cib', 'optionsSets': options_sets,
  187. 'allowedMessageTypes': Defaults.allowedMessageTypes,
  188. 'sliceIds': Defaults.sliceIds,
  189. 'traceId': os.urandom(16).hex(), 'isStartOfSession': True,
  190. 'requestId': request_id,
  191. 'message': {
  192. **Defaults.location,
  193. 'author': 'user',
  194. 'inputMethod': 'Keyboard',
  195. 'text': prompt,
  196. 'messageType': 'Chat',
  197. 'requestId': request_id,
  198. 'messageId': request_id
  199. },
  200. "verbosity": "verbose",
  201. "scenario": "SERP",
  202. "plugins": [{"id": "c310c353-b9f0-4d76-ab0d-1dd5e979cf68", "category": 1}] if web_search else [],
  203. 'tone': tone,
  204. 'spokenTextMode': 'None',
  205. 'conversationId': conversation.conversationId,
  206. 'participant': {'id': conversation.clientId},
  207. }],
  208. 'invocationId': '1',
  209. 'target': 'chat',
  210. 'type': 4
  211. }
  212. if image_response and image_response.get('imageUrl') and image_response.get('originalImageUrl'):
  213. struct['arguments'][0]['message']['originalImageUrl'] = image_response.get('originalImageUrl')
  214. struct['arguments'][0]['message']['imageUrl'] = image_response.get('imageUrl')
  215. struct['arguments'][0]['experienceType'] = None
  216. struct['arguments'][0]['attachedFileInfo'] = {"fileName": None, "fileType": None}
  217. if context:
  218. struct['arguments'][0]['previousMessages'] = [{
  219. "author": "user",
  220. "description": context,
  221. "contextType": "WebPage",
  222. "messageType": "Context",
  223. "messageId": "discover-web--page-ping-mriduna-----"
  224. }]
  225. return format_message(struct)
  226. async def stream_generate(
  227. prompt: str,
  228. tone: str,
  229. image: ImageType = None,
  230. context: str = None,
  231. proxy: str = None,
  232. cookies: dict = None,
  233. web_search: bool = False,
  234. gpt4_turbo: bool = False,
  235. timeout: int = 900
  236. ):
  237. """
  238. Asynchronously streams generated responses from the Bing API.
  239. :param prompt: The user's input prompt.
  240. :param tone: The desired tone for the response.
  241. :param image: The image type involved in the response.
  242. :param context: Additional context for the prompt.
  243. :param proxy: Proxy settings for the request.
  244. :param cookies: Cookies for the session.
  245. :param web_search: Flag to enable web search.
  246. :param gpt4_turbo: Flag to enable GPT-4 Turbo.
  247. :param timeout: Timeout for the request.
  248. :return: An asynchronous generator yielding responses.
  249. """
  250. headers = Defaults.headers
  251. if cookies:
  252. headers["Cookie"] = "; ".join(f"{k}={v}" for k, v in cookies.items())
  253. async with ClientSession(
  254. timeout=ClientTimeout(total=timeout), headers=headers
  255. ) as session:
  256. conversation = await create_conversation(session, proxy)
  257. image_response = await upload_image(session, image, tone, proxy) if image else None
  258. if image_response:
  259. yield image_response
  260. try:
  261. async with session.ws_connect(
  262. 'wss://sydney.bing.com/sydney/ChatHub',
  263. autoping=False,
  264. params={'sec_access_token': conversation.conversationSignature},
  265. proxy=proxy
  266. ) as wss:
  267. await wss.send_str(format_message({'protocol': 'json', 'version': 1}))
  268. await wss.receive(timeout=timeout)
  269. await wss.send_str(create_message(conversation, prompt, tone, context, image_response, web_search, gpt4_turbo))
  270. response_txt = ''
  271. returned_text = ''
  272. final = False
  273. while not final:
  274. msg = await wss.receive(timeout=timeout)
  275. if not msg.data:
  276. continue
  277. objects = msg.data.split(Defaults.delimiter)
  278. for obj in objects:
  279. if obj is None or not obj:
  280. continue
  281. response = json.loads(obj)
  282. if response and response.get('type') == 1 and response['arguments'][0].get('messages'):
  283. message = response['arguments'][0]['messages'][0]
  284. image_response = None
  285. if (message['contentOrigin'] != 'Apology'):
  286. if 'adaptiveCards' in message:
  287. card = message['adaptiveCards'][0]['body'][0]
  288. if "text" in card:
  289. response_txt = card.get('text')
  290. if message.get('messageType'):
  291. inline_txt = card['inlines'][0].get('text')
  292. response_txt += inline_txt + '\n'
  293. elif message.get('contentType') == "IMAGE":
  294. prompt = message.get('text')
  295. try:
  296. image_response = ImageResponse(await create_images(session, prompt, proxy), prompt)
  297. except:
  298. response_txt += f"\nhttps://www.bing.com/images/create?q={parse.quote(prompt)}"
  299. final = True
  300. if response_txt.startswith(returned_text):
  301. new = response_txt[len(returned_text):]
  302. if new != "\n":
  303. yield new
  304. returned_text = response_txt
  305. if image_response:
  306. yield image_response
  307. elif response.get('type') == 2:
  308. result = response['item']['result']
  309. if result.get('error'):
  310. if result["value"] == "CaptchaChallenge":
  311. raise Exception(f"{result['value']}: Use other cookies or/and ip address")
  312. else:
  313. raise Exception(f"{result['value']}: {result['message']}")
  314. return
  315. finally:
  316. await delete_conversation(session, conversation, proxy)