Bing.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. from __future__ import annotations
  2. import random
  3. import json
  4. import uuid
  5. import time
  6. import asyncio
  7. from urllib import parse
  8. from datetime import datetime, date
  9. from ..typing import AsyncResult, Messages, ImageType, Cookies
  10. from ..image import ImageRequest
  11. from ..errors import ResponseError, ResponseStatusError, RateLimitError
  12. from ..requests import DEFAULT_HEADERS
  13. from ..requests.aiohttp import StreamSession
  14. from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
  15. from .helper import get_random_hex
  16. from .bing.upload_image import upload_image
  17. from .bing.conversation import Conversation, create_conversation, delete_conversation
  18. from .BingCreateImages import BingCreateImages
  19. from .. import debug
  20. class Tones:
  21. """
  22. Defines the different tone options for the Bing provider.
  23. """
  24. creative = "Creative"
  25. balanced = "Balanced"
  26. precise = "Precise"
  27. copilot = "Copilot"
  28. class Bing(AsyncGeneratorProvider, ProviderModelMixin):
  29. """
  30. Bing provider for generating responses using the Bing API.
  31. """
  32. label = "Microsoft Copilot in Bing"
  33. url = "https://bing.com/chat"
  34. working = True
  35. supports_message_history = True
  36. supports_gpt_4 = True
  37. default_model = "Balanced"
  38. default_vision_model = "gpt-4-vision"
  39. models = [getattr(Tones, key) for key in Tones.__dict__ if not key.startswith("__")]
  40. @classmethod
  41. def create_async_generator(
  42. cls,
  43. model: str,
  44. messages: Messages,
  45. proxy: str = None,
  46. timeout: int = 900,
  47. api_key: str = None,
  48. cookies: Cookies = None,
  49. tone: str = None,
  50. image: ImageType = None,
  51. web_search: bool = False,
  52. context: str = None,
  53. **kwargs
  54. ) -> AsyncResult:
  55. """
  56. Creates an asynchronous generator for producing responses from Bing.
  57. :param model: The model to use.
  58. :param messages: Messages to process.
  59. :param proxy: Proxy to use for requests.
  60. :param timeout: Timeout for requests.
  61. :param cookies: Cookies for the session.
  62. :param tone: The tone of the response.
  63. :param image: The image type to be used.
  64. :param web_search: Flag to enable or disable web search.
  65. :return: An asynchronous result object.
  66. """
  67. prompt = messages[-1]["content"]
  68. if context is None:
  69. context = create_context(messages[:-1]) if len(messages) > 1 else None
  70. if tone is None:
  71. tone = tone if model.startswith("gpt-4") else model
  72. tone = cls.get_model("" if tone is None else tone)
  73. gpt4_turbo = True if model.startswith("gpt-4-turbo") else False
  74. return stream_generate(
  75. prompt, tone, image, context, cookies, api_key,
  76. proxy, web_search, gpt4_turbo, timeout,
  77. **kwargs
  78. )
  79. def create_context(messages: Messages) -> str:
  80. """
  81. Creates a context string from a list of messages.
  82. :param messages: A list of message dictionaries.
  83. :return: A string representing the context created from the messages.
  84. """
  85. return "".join(
  86. f"[{message['role']}]" + ("(#message)"
  87. if message['role'] != "system"
  88. else "(#additional_instructions)") + f"\n{message['content']}"
  89. for message in messages
  90. ) + "\n\n"
  91. def get_ip_address() -> str:
  92. return f"13.{random.randint(104, 107)}.{random.randint(0, 255)}.{random.randint(0, 255)}"
  93. def get_default_cookies():
  94. #muid = get_random_hex().upper()
  95. sid = get_random_hex().upper()
  96. guid = get_random_hex().upper()
  97. isodate = date.today().isoformat()
  98. timestamp = int(time.time())
  99. zdate = "0001-01-01T00:00:00.0000000"
  100. return {
  101. "_C_Auth": "",
  102. #"MUID": muid,
  103. #"MUIDB": muid,
  104. "_EDGE_S": f"F=1&SID={sid}",
  105. "_EDGE_V": "1",
  106. "SRCHD": "AF=hpcodx",
  107. "SRCHUID": f"V=2&GUID={guid}&dmnchg=1",
  108. "_RwBf": (
  109. f"r=0&ilt=1&ihpd=0&ispd=0&rc=3&rb=0&gb=0&rg=200&pc=0&mtu=0&rbb=0&g=0&cid="
  110. f"&clo=0&v=1&l={isodate}&lft={zdate}&aof=0&ard={zdate}"
  111. f"&rwdbt={zdate}&rwflt={zdate}&o=2&p=&c=&t=0&s={zdate}"
  112. f"&ts={isodate}&rwred=0&wls=&wlb="
  113. "&wle=&ccp=&cpt=&lka=0&lkt=0&aad=0&TH="
  114. ),
  115. '_Rwho': f'u=d&ts={isodate}',
  116. "_SS": f"SID={sid}&R=3&RB=0&GB=0&RG=200&RP=0",
  117. "SRCHUSR": f"DOB={date.today().strftime('%Y%m%d')}&T={timestamp}",
  118. "SRCHHPGUSR": f"HV={int(time.time())}",
  119. "BCP": "AD=1&AL=1&SM=1",
  120. "ipv6": f"hit={timestamp}",
  121. '_C_ETH' : '1',
  122. }
  123. async def create_headers(cookies: Cookies = None, api_key: str = None) -> dict:
  124. if cookies is None:
  125. # import nodriver as uc
  126. # browser = await uc.start(headless=False)
  127. # page = await browser.get(Defaults.home)
  128. # await asyncio.sleep(10)
  129. # cookies = {}
  130. # for c in await page.browser.cookies.get_all():
  131. # if c.domain.endswith(".bing.com"):
  132. # cookies[c.name] = c.value
  133. # user_agent = await page.evaluate("window.navigator.userAgent")
  134. # await page.close()
  135. cookies = get_default_cookies()
  136. if api_key is not None:
  137. cookies["_U"] = api_key
  138. headers = Defaults.headers.copy()
  139. headers["cookie"] = "; ".join(f"{k}={v}" for k, v in cookies.items())
  140. return headers
  141. class Defaults:
  142. """
  143. Default settings and configurations for the Bing provider.
  144. """
  145. delimiter = "\x1e"
  146. # List of allowed message types for Bing responses
  147. allowedMessageTypes = [
  148. "ActionRequest","Chat",
  149. "ConfirmationCard", "Context",
  150. "InternalSearchQuery", #"InternalSearchResult",
  151. #"Disengaged", "InternalLoaderMessage",
  152. "Progress", "RenderCardRequest",
  153. "RenderContentRequest", "AdsQuery",
  154. "SemanticSerp", "GenerateContentQuery",
  155. "SearchQuery", "GeneratedCode",
  156. "InternalTasksMessage"
  157. ]
  158. sliceIds = {
  159. "balanced": [
  160. "supllmnfe","archnewtf",
  161. "stpstream", "stpsig", "vnextvoicecf", "scmcbase", "cmcpupsalltf", "sydtransctrl",
  162. "thdnsrch", "220dcl1s0", "0215wcrwips0", "0305hrthrots0", "0130gpt4t",
  163. "bingfc", "0225unsticky1", "0228scss0",
  164. "defquerycf", "defcontrol", "3022tphpv"
  165. ],
  166. "creative": [
  167. "bgstream", "fltltst2c",
  168. "stpstream", "stpsig", "vnextvoicecf", "cmcpupsalltf", "sydtransctrl",
  169. "0301techgnd", "220dcl1bt15", "0215wcrwip", "0305hrthrot", "0130gpt4t",
  170. "bingfccf", "0225unsticky1", "0228scss0",
  171. "3022tpvs0"
  172. ],
  173. "precise": [
  174. "bgstream", "fltltst2c",
  175. "stpstream", "stpsig", "vnextvoicecf", "cmcpupsalltf", "sydtransctrl",
  176. "0301techgnd", "220dcl1bt15", "0215wcrwip", "0305hrthrot", "0130gpt4t",
  177. "bingfccf", "0225unsticky1", "0228scss0",
  178. "defquerycf", "3022tpvs0"
  179. ],
  180. "copilot": []
  181. }
  182. optionsSets = {
  183. "balanced": {
  184. "default": [
  185. "nlu_direct_response_filter", "deepleo",
  186. "disable_emoji_spoken_text", "responsible_ai_policy_235",
  187. "enablemm", "dv3sugg", "autosave",
  188. "iyxapbing", "iycapbing",
  189. "galileo", "saharagenconv5", "gldcl1p",
  190. "gpt4tmncnp"
  191. ],
  192. "nosearch": [
  193. "nlu_direct_response_filter", "deepleo",
  194. "disable_emoji_spoken_text", "responsible_ai_policy_235",
  195. "enablemm", "dv3sugg", "autosave",
  196. "iyxapbing", "iycapbing",
  197. "galileo", "sunoupsell", "base64filter", "uprv4p1upd",
  198. "hourthrot", "noctprf", "gndlogcf", "nosearchall"
  199. ]
  200. },
  201. "creative": {
  202. "default": [
  203. "nlu_direct_response_filter", "deepleo",
  204. "disable_emoji_spoken_text", "responsible_ai_policy_235",
  205. "enablemm", "dv3sugg",
  206. "iyxapbing", "iycapbing",
  207. "h3imaginative", "techinstgnd", "hourthrot", "clgalileo", "gencontentv3",
  208. "gpt4tmncnp"
  209. ],
  210. "nosearch": [
  211. "nlu_direct_response_filter", "deepleo",
  212. "disable_emoji_spoken_text", "responsible_ai_policy_235",
  213. "enablemm", "dv3sugg", "autosave",
  214. "iyxapbing", "iycapbing",
  215. "h3imaginative", "sunoupsell", "base64filter", "uprv4p1upd",
  216. "hourthrot", "noctprf", "gndlogcf", "nosearchall",
  217. "clgalileo", "nocache", "up4rp14bstcst"
  218. ]
  219. },
  220. "precise": {
  221. "default": [
  222. "nlu_direct_response_filter", "deepleo",
  223. "disable_emoji_spoken_text", "responsible_ai_policy_235",
  224. "enablemm", "dv3sugg",
  225. "iyxapbing", "iycapbing",
  226. "h3precise", "techinstgnd", "hourthrot", "techinstgnd", "hourthrot",
  227. "clgalileo", "gencontentv3"
  228. ],
  229. "nosearch": [
  230. "nlu_direct_response_filter", "deepleo",
  231. "disable_emoji_spoken_text", "responsible_ai_policy_235",
  232. "enablemm", "dv3sugg", "autosave",
  233. "iyxapbing", "iycapbing",
  234. "h3precise", "sunoupsell", "base64filter", "uprv4p1upd",
  235. "hourthrot", "noctprf", "gndlogcf", "nosearchall",
  236. "clgalileo", "nocache", "up4rp14bstcst"
  237. ]
  238. },
  239. "copilot": [
  240. "nlu_direct_response_filter", "deepleo",
  241. "disable_emoji_spoken_text", "responsible_ai_policy_235",
  242. "enablemm", "dv3sugg",
  243. "iyxapbing", "iycapbing",
  244. "h3precise", "clgalileo", "gencontentv3", "prjupy"
  245. ],
  246. }
  247. # Default location settings
  248. location = {
  249. "locale": "en-US", "market": "en-US", "region": "US",
  250. "location":"lat:34.0536909;long:-118.242766;re=1000m;",
  251. "locationHints": [{
  252. "country": "United States", "state": "California", "city": "Los Angeles",
  253. "timezoneoffset": 8, "countryConfidence": 8,
  254. "Center": {"Latitude": 34.0536909, "Longitude": -118.242766},
  255. "RegionType": 2, "SourceType": 1
  256. }],
  257. }
  258. # Default headers for requests
  259. home = "https://www.bing.com/chat?q=Microsoft+Copilot&FORM=hpcodx"
  260. headers = {
  261. **DEFAULT_HEADERS,
  262. "accept": "application/json",
  263. "referer": home,
  264. "x-ms-client-request-id": str(uuid.uuid4()),
  265. "x-ms-useragent": "azsdk-js-api-client-factory/1.0.0-beta.1 core-rest-pipeline/1.15.1 OS/Windows",
  266. }
  267. def format_message(msg: dict) -> str:
  268. """
  269. Formats a message dictionary into a JSON string with a delimiter.
  270. :param msg: The message dictionary to format.
  271. :return: A formatted string representation of the message.
  272. """
  273. return json.dumps(msg, ensure_ascii=False) + Defaults.delimiter
  274. def create_message(
  275. conversation: Conversation,
  276. prompt: str,
  277. tone: str,
  278. context: str = None,
  279. image_request: ImageRequest = None,
  280. web_search: bool = False,
  281. gpt4_turbo: bool = False,
  282. new_conversation: bool = True
  283. ) -> str:
  284. """
  285. Creates a message for the Bing API with specified parameters.
  286. :param conversation: The current conversation object.
  287. :param prompt: The user's input prompt.
  288. :param tone: The desired tone for the response.
  289. :param context: Additional context for the prompt.
  290. :param image_request: The image request with the url.
  291. :param web_search: Flag to enable web search.
  292. :param gpt4_turbo: Flag to enable GPT-4 Turbo.
  293. :return: A formatted string message for the Bing API.
  294. """
  295. options_sets = Defaults.optionsSets[tone.lower()]
  296. if not web_search and "nosearch" in options_sets:
  297. options_sets = options_sets["nosearch"]
  298. elif "default" in options_sets:
  299. options_sets = options_sets["default"]
  300. options_sets = options_sets.copy()
  301. if gpt4_turbo:
  302. options_sets.append("dlgpt4t")
  303. request_id = str(uuid.uuid4())
  304. struct = {
  305. "arguments":[{
  306. "source": "cib",
  307. "optionsSets": options_sets,
  308. "allowedMessageTypes": Defaults.allowedMessageTypes,
  309. "sliceIds": Defaults.sliceIds[tone.lower()],
  310. "verbosity": "verbose",
  311. "scenario": "CopilotMicrosoftCom" if tone == Tones.copilot else "SERP",
  312. "plugins": [{"id": "c310c353-b9f0-4d76-ab0d-1dd5e979cf68", "category": 1}] if web_search else [],
  313. "traceId": get_random_hex(40),
  314. "conversationHistoryOptionsSets": ["autosave","savemem","uprofupd","uprofgen"],
  315. "gptId": "copilot",
  316. "isStartOfSession": new_conversation,
  317. "requestId": request_id,
  318. "message":{
  319. **Defaults.location,
  320. "userIpAddress": get_ip_address(),
  321. "timestamp": datetime.now().isoformat(),
  322. "author": "user",
  323. "inputMethod": "Keyboard",
  324. "text": prompt,
  325. "messageType": "Chat",
  326. "requestId": request_id,
  327. "messageId": request_id
  328. },
  329. "tone": "Balanced" if tone == Tones.copilot else tone,
  330. "spokenTextMode": "None",
  331. "conversationId": conversation.conversationId,
  332. "participant": {"id": conversation.clientId}
  333. }],
  334. "invocationId": "0",
  335. "target": "chat",
  336. "type": 4
  337. }
  338. if image_request and image_request.get('imageUrl') and image_request.get('originalImageUrl'):
  339. struct['arguments'][0]['message']['originalImageUrl'] = image_request.get('originalImageUrl')
  340. struct['arguments'][0]['message']['imageUrl'] = image_request.get('imageUrl')
  341. struct['arguments'][0]['experienceType'] = None
  342. struct['arguments'][0]['attachedFileInfo'] = {"fileName": None, "fileType": None}
  343. if context:
  344. struct['arguments'][0]['previousMessages'] = [{
  345. "author": "user",
  346. "description": context,
  347. "contextType": "ClientApp",
  348. "messageType": "Context",
  349. "messageId": "discover-web--page-ping-mriduna-----"
  350. }]
  351. return format_message(struct)
  352. async def stream_generate(
  353. prompt: str,
  354. tone: str,
  355. image: ImageType = None,
  356. context: str = None,
  357. cookies: dict = None,
  358. api_key: str = None,
  359. proxy: str = None,
  360. web_search: bool = False,
  361. gpt4_turbo: bool = False,
  362. timeout: int = 900,
  363. conversation: Conversation = None,
  364. return_conversation: bool = False,
  365. raise_apology: bool = False,
  366. max_retries: int = None,
  367. sleep_retry: int = 15,
  368. **kwargs
  369. ):
  370. """
  371. Asynchronously streams generated responses from the Bing API.
  372. :param prompt: The user's input prompt.
  373. :param tone: The desired tone for the response.
  374. :param image: The image type involved in the response.
  375. :param context: Additional context for the prompt.
  376. :param cookies: Cookies for the session.
  377. :param web_search: Flag to enable web search.
  378. :param gpt4_turbo: Flag to enable GPT-4 Turbo.
  379. :param timeout: Timeout for the request.
  380. :return: An asynchronous generator yielding responses.
  381. """
  382. headers = await create_headers(cookies, api_key)
  383. new_conversation = conversation is None
  384. max_retries = (5 if new_conversation else 0) if max_retries is None else max_retries
  385. first = True
  386. while first or conversation is None:
  387. async with StreamSession(timeout=timeout, proxy=proxy) as session:
  388. first = False
  389. do_read = True
  390. try:
  391. if conversation is None:
  392. conversation = await create_conversation(session, headers, tone)
  393. if return_conversation:
  394. yield conversation
  395. except (ResponseStatusError, RateLimitError) as e:
  396. max_retries -= 1
  397. if max_retries < 1:
  398. raise e
  399. if debug.logging:
  400. print(f"Bing: Retry: {e}")
  401. headers = await create_headers()
  402. await asyncio.sleep(sleep_retry)
  403. continue
  404. image_request = await upload_image(
  405. session,
  406. image,
  407. "Balanced" if tone == Tones.copilot else tone,
  408. headers
  409. ) if image else None
  410. async with session.ws_connect(
  411. 'wss://s.copilot.microsoft.com/sydney/ChatHub'
  412. if tone == "Copilot" else
  413. 'wss://sydney.bing.com/sydney/ChatHub',
  414. autoping=False,
  415. params={'sec_access_token': conversation.conversationSignature},
  416. headers=headers
  417. ) as wss:
  418. await wss.send_str(format_message({'protocol': 'json', 'version': 1}))
  419. await wss.send_str(format_message({"type": 6}))
  420. await wss.receive_str()
  421. await wss.send_str(create_message(
  422. conversation, prompt, tone,
  423. context if new_conversation else None,
  424. image_request, web_search, gpt4_turbo,
  425. new_conversation
  426. ))
  427. response_txt = ''
  428. returned_text = ''
  429. message_id = None
  430. while do_read:
  431. try:
  432. msg = await wss.receive_str()
  433. except TypeError:
  434. continue
  435. objects = msg.split(Defaults.delimiter)
  436. for obj in objects:
  437. if not obj:
  438. continue
  439. try:
  440. response = json.loads(obj)
  441. except ValueError:
  442. continue
  443. if response and response.get('type') == 1 and response['arguments'][0].get('messages'):
  444. message = response['arguments'][0]['messages'][0]
  445. if message_id is not None and message_id != message["messageId"]:
  446. returned_text = ''
  447. message_id = message["messageId"]
  448. image_response = None
  449. if (raise_apology and message['contentOrigin'] == 'Apology'):
  450. raise ResponseError("Apology Response Error")
  451. if 'adaptiveCards' in message:
  452. card = message['adaptiveCards'][0]['body'][0]
  453. if "text" in card:
  454. response_txt = card.get('text')
  455. if message.get('messageType') and "inlines" in card:
  456. inline_txt = card['inlines'][0].get('text')
  457. response_txt += f"{inline_txt}\n"
  458. elif message.get('contentType') == "IMAGE":
  459. prompt = message.get('text')
  460. try:
  461. image_client = BingCreateImages(cookies, proxy, api_key)
  462. image_response = await image_client.create_async(prompt)
  463. except Exception as e:
  464. if debug.logging:
  465. print(f"Bing: Failed to create images: {e}")
  466. image_response = f"\nhttps://www.bing.com/images/create?q={parse.quote(prompt)}"
  467. if response_txt.startswith(returned_text):
  468. new = response_txt[len(returned_text):]
  469. if new not in ("", "\n"):
  470. yield new
  471. returned_text = response_txt
  472. if image_response is not None:
  473. yield image_response
  474. elif response.get('type') == 2:
  475. result = response['item']['result']
  476. do_read = False
  477. if result.get('error'):
  478. max_retries -= 1
  479. if max_retries < 1:
  480. if result["value"] == "CaptchaChallenge":
  481. raise RateLimitError(f"{result['value']}: Use other cookies or/and ip address")
  482. else:
  483. raise RuntimeError(f"{result['value']}: {result['message']}")
  484. if debug.logging:
  485. print(f"Bing: Retry: {result['value']}: {result['message']}")
  486. headers = await create_headers()
  487. conversation = None
  488. await asyncio.sleep(sleep_retry)
  489. break
  490. elif response.get('type') == 3:
  491. do_read = False
  492. break
  493. if conversation is not None:
  494. await delete_conversation(session, conversation, headers)