{"id":96,"date":"2023-04-19T14:47:25","date_gmt":"2023-04-19T06:47:25","guid":{"rendered":"https:\/\/oneai.eu.org\/?p=96"},"modified":"2023-04-20T20:34:21","modified_gmt":"2023-04-20T12:34:21","slug":"bingbot-for-localhost","status":"publish","type":"page","link":"https:\/\/oneai.eu.org\/?page_id=96","title":{"rendered":"python\u5b9e\u73b0\u672c\u5730\u5316bingBot"},"content":{"rendered":"\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column has-medium-font-size is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"python\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\"># --* encoding=utf8 *--\n'''\n\u547d\u4ee4\u6a21\u5f0f\u57fa\u672c\u5b8c\u6210\n'''\nimport asyncio\nimport json\nimport re\n\nfrom prompt_toolkit import PromptSession\nfrom prompt_toolkit.auto_suggest import AutoSuggestFromHistory\nfrom prompt_toolkit.completion import WordCompleter\nfrom prompt_toolkit.history import InMemoryHistory\n\nfrom EdgeGPT import Chatbot, ConversationStyle\n\ntry:\n    from typing import Literal\nexcept ImportError:\n    from typing_extensions import Literal\n\nbingcookiefile = '.\/bingcookies.json'\nwith open(bingcookiefile, 'r') as f:\n    cookies = json.load(f)\nbot = Chatbot(cookies=cookies)\n\n\nasync def main(query):\n    bot = Chatbot(cookies=cookies)\n    response_dict = await bot.ask(prompt=query, conversation_style=ConversationStyle.creative)\n    # json_str = json.dumps(response_dict)\n    # json_str = json.loads(json_str)\n    #\n    # print(\"JSON: \\n\" + str(json_str))\n    suggestedResponses = []  # \u63d0\u793a\u518d\u6b21\u53d1\u9001\u7684\u8bed\u53e5\n    messageText = query\n    response = ''\n    spokenText = ''\n    try:\n        tempresponsedict = response_dict['item']['messages']\n        if len(tempresponsedict) == 1:\n            if 'text' in response_dict['item']['messages'][1]:\n                response = re.sub(r'\\[\\^\\d\\^\\]', '',\n                                  response_dict['item']['messages'][1]['text'])\n            else:\n                response = \"Something wrong. Please reset chat\"\n                # \u89e3\u91ca\u8fd4\u56de\u7684bing\u5efa\u8bae\n            if 'suggestedResponses' in response_dict['item']['messages'][1]:\n                suggestedResponses0 = re.sub(\n                    r'\\[\\^\\d\\^\\]', '', response_dict['item']['messages'][1]['suggestedResponses'][0]['text'])\n                response = response + \"\\n ---------------------------------------------\"\n                response = response + \"\\n 1.[%s]\" % (suggestedResponses0)\n                suggestedResponses1 = re.sub(\n                    r'\\[\\^\\d\\^\\]', '', response_dict['item']['messages'][1]['suggestedResponses'][1]['text'])\n                response = response + \"2.[%s]\\n\" % (suggestedResponses1)\n                suggestedResponses2 = re.sub(\n                    r'\\[\\^\\d\\^\\]', '', response_dict['item']['messages'][1]['suggestedResponses'][2]['text'])\n                response = response + \"3.[%s]\\n\" % (suggestedResponses2)\n            else:\n                suggestedResponses = ''\n            if 'spokenText' in response_dict['item']['messages'][1]:\n                spokenText = response_dict['item']['messages'][1]['spokenText']\n                response = response + \"\\n ---------------------------------------------\"\n                response = response + \"\\n\u53c2\u8003\uff1a[%s]\" % (spokenText)\n        else:\n            for tmpdata in tempresponsedict:\n                if 'text' in tmpdata:\n                    response = response + re.sub(r'\\[\\^\\d\\^\\]', '', tmpdata['text'])\n                if 'suggestedResponses' in tmpdata:\n                    suggestedResponses0 = re.sub(r'\\[\\^\\d\\^\\]', '', tmpdata['suggestedResponses'][0]['text'])\n                    suggestedResponses1 = re.sub(r'\\[\\^\\d\\^\\]', '', tmpdata['suggestedResponses'][1]['text'])\n                    suggestedResponses2 = re.sub(r'\\[\\^\\d\\^\\]', '', tmpdata['suggestedResponses'][2]['text'])\n                else:\n                    suggestedResponses = ''\n                if 'spokenText' in tmpdata:\n                    spokenText = tmpdata['spokenText']\n            response = response + \"\\n ---------------------------------------------\"\n            response = response + \"\\n1.[%s]\" % (suggestedResponses0)\n            response = response + \"\\n2.[%s]\" % (suggestedResponses1)\n            response = response + \"\\n3.[%s]\" % (suggestedResponses2)\n            response = response + \"\\n ---------------------------------------------\"\n            response = response + \"\\n\\n\u53c2\u8003:\\t[%s]\\n\" % (spokenText)\n\n        if 'maxNumUserMessagesInConversation' in response_dict['item'][\n            'throttling'] and 'numUserMessagesInConversation' in \\\n                response_dict['item']['throttling']:\n            maxNumUserMessagesInConversation = response_dict['item'][\n                'throttling']['maxNumUserMessagesInConversation']\n            numUserMessagesInConversation = response_dict['item']['throttling']['numUserMessagesInConversation']\n            response = response + \"\\n---------------------------------------------\\n\"\n            response = response + \"Messages In Conversation : %d \/ %d\" % (\n                numUserMessagesInConversation, maxNumUserMessagesInConversation)\n            if int(numUserMessagesInConversation) >= maxNumUserMessagesInConversation:\n                await bot.reset()  #\n                response = response + \"\\n  Automatic reset succeeded?\"\n\n        if (len(response_dict['item']['messages'][1]['sourceAttributions']) >= 3):\n            providerDisplayName0 = re.sub(\n                r'\\[\\^\\d\\^\\]', '', response_dict['item']['messages'][1]['sourceAttributions'][0]['providerDisplayName'])\n            seeMoreUrl0 = re.sub(\n                r'\\[\\^\\d\\^\\]', '', response_dict['item']['messages'][1]['sourceAttributions'][0]['seeMoreUrl'])\n            providerDisplayName1 = re.sub(\n                r'\\[\\^\\d\\^\\]', '', response_dict['item']['messages'][1]['sourceAttributions'][1]['providerDisplayName'])\n            seeMoreUrl1 = re.sub(\n                r'\\[\\^\\d\\^\\]', '', response_dict['item']['messages'][1]['sourceAttributions'][1]['seeMoreUrl'])\n            providerDisplayName2 = re.sub(\n                r'\\[\\^\\d\\^\\]', '', response_dict['item']['messages'][1]['sourceAttributions'][2]['providerDisplayName'])\n            seeMoreUrl2 = re.sub(\n                r'\\[\\^\\d\\^\\]', '', response_dict['item']['messages'][1]['sourceAttributions'][2]['seeMoreUrl'])\n            response = response + \"\\n\\n--------------------\\nReference:\\n\"\n            response = response + \\\n                       \"1.[%s](%s)\\n\" % (providerDisplayName0, seeMoreUrl0)\n            response = response + \\\n                       \"2.[%s](%s)\\n\" % (providerDisplayName1, seeMoreUrl1)\n            response = response + \\\n                       \"3.[%s](%s)\\n\" % (providerDisplayName2, seeMoreUrl2)\n    except BaseException as B:\n        print(B)\n    print(\"\u95ee\u9898->\uff1a\" + messageText + \"\\n\" + \"\u56de\u7b54->\uff1a\" + response)\n    print('\\n')\n    return response\n\n\nasync def get_input_async(\n        session: PromptSession = None,\n        completer: WordCompleter = None,\n) -> str:\n    \"\"\"\n    Multiline input function.\n    \"\"\"\n    return await session.prompt_async(\n        completer=completer,\n        multiline=True,\n        auto_suggest=AutoSuggestFromHistory(),\n    )\n\n\ndef create_session() -> PromptSession:\n    return PromptSession(history=InMemoryHistory())\n\n\nif __name__ == '__main__':\n    session = create_session()\n    while True:\n        question = input('\u8bf7\u63d0\u95ee:\\t')\n        # query='\u7ed9\u6211\u7528python\u5199\u4e00\u6bb5\u4ee3\u7801\uff0c\u5b9e\u73b0\u4e0ewebUI\u4ea4\u4e92\u5f0f\u804a\u5929,\u8981\u6c42\u6709\u6bd4\u8f83\u6f02\u4eae\u754c\u9762\u8bbe\u8ba1'\n        if question == \"!exit\":\n            break\n        elif question == \"!help\":\n            print(\n                \"\"\"\n            !help - Show this help message\n            !exit - Exit the program\n            !reset - Reset the conversation\n            \"\"\",\n            )\n            continue\n        elif question == \"!reset\":\n            bot.reset()\n            continue\n\n        asyncio.run(main(question))\n<\/code><\/pre>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<p><\/p>\n<\/div>\n<\/div>\n\n\n\n<p>\u8fd0\u884c\u524d\u5b89\u88c5\u597d\u4f9d\u8d56\u5305\uff0c\u7136\u540e\u5c06bing\u7684cookies \u7c98\u8d34\u5230bingcookies.json\u91cc<\/p>\n\n\n\n<p>\u4f7f\u7528\u8bf4\u660e\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b89\u88c5\u5305<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">python3 -m pip install EdgeGPT --upgrade\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github-com.translate.goog\/acheong08\/EdgeGPT?_x_tr_sl=auto&amp;_x_tr_tl=zh-CN&amp;_x_tr_hl=zh-CN#requirements\"><\/a>\u8981\u6c42<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>python 3.8+<\/li>\n\n\n\n<li><a href=\"https:\/\/translate.google.com\/website?sl=auto&amp;tl=zh-CN&amp;hl=zh-CN&amp;u=https:\/\/bing.com\/chat\">\u53ef\u4ee5\u63d0\u524d\u8bbf\u95eehttps:\/\/bing.com\/chat<\/a>\u7684 Microsoft \u5e10\u6237\uff08\u5fc5\u9700\uff09<\/li>\n\n\n\n<li>\u5728\u53d7\u652f\u6301\u7684\u56fd\u5bb6\u9700\u8981 New Bing\uff08\u9700\u8981 VPN\uff09<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github-com.translate.goog\/acheong08\/EdgeGPT?_x_tr_sl=auto&amp;_x_tr_tl=zh-CN&amp;_x_tr_hl=zh-CN#checking-access-required\"><\/a>\u68c0\u67e5\u8bbf\u95ee\u6743\u9650\uff08\u5fc5\u586b\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684 Microsoft Edge<\/li>\n\n\n\n<li>\u6216\u8005\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4efb\u4f55\u6d4f\u89c8\u5668\u5e76\u5c06\u7528\u6237\u4ee3\u7406\u8bbe\u7f6e\u4e3a\u770b\u8d77\u6765\u50cf\u60a8\u6b63\u5728\u4f7f\u7528 Edge\uff08\u4f8b\u5982\uff0c<code>Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/111.0.0.0 Safari\/537.36 Edg\/111.0.1661.51<\/code>\uff09\u3002<a href=\"https:\/\/translate.google.com\/website?sl=auto&amp;tl=zh-CN&amp;hl=zh-CN&amp;u=https:\/\/chrome.google.com\/webstore\/detail\/user-agent-switcher-and-m\/bhchdcejhohfmigjafbampogmaanbfkg\">\u60a8\u53ef\u4ee5\u4f7f\u7528Chrome<\/a>\u548c<a href=\"https:\/\/translate.google.com\/website?sl=auto&amp;tl=zh-CN&amp;hl=zh-CN&amp;u=https:\/\/addons.mozilla.org\/en-US\/firefox\/addon\/user-agent-string-switcher\/\">Firefox<\/a>\u7684\u201cUser-Agent Switcher and Manager\u201d\u7b49\u6269\u5c55\u8f7b\u677e\u5730\u505a\u5230\u8fd9\u4e00\u70b9\u3002<\/li>\n\n\n\n<li>\u6253\u5f00<a href=\"https:\/\/translate.google.com\/website?sl=auto&amp;tl=zh-CN&amp;hl=zh-CN&amp;u=https:\/\/bing.com\/chat\">bing.com\/chat<\/a><\/li>\n\n\n\n<li>\u5982\u679c\u60a8\u770b\u5230\u804a\u5929\u529f\u80fd\uff0c\u90a3\u60a8\u5c31\u53ef\u4ee5\u5f00\u59cb\u4e86<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/github-com.translate.goog\/acheong08\/EdgeGPT?_x_tr_sl=auto&amp;_x_tr_tl=zh-CN&amp;_x_tr_hl=zh-CN#getting-authentication-required\"><\/a>\u83b7\u53d6\u8eab\u4efd\u9a8c\u8bc1\uff08\u5fc5\u586b\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/translate.google.com\/website?sl=auto&amp;tl=zh-CN&amp;hl=zh-CN&amp;u=https:\/\/chrome.google.com\/webstore\/detail\/cookie-editor\/hlkenndednhfkekhgcdicdfddnkalmdm\">\u4e3aChrome<\/a>\u6216<a href=\"https:\/\/translate.google.com\/website?sl=auto&amp;tl=zh-CN&amp;hl=zh-CN&amp;u=https:\/\/addons.mozilla.org\/en-US\/firefox\/addon\/cookie-editor\/\">Firefox<\/a>\u5b89\u88c5 cookie \u7f16\u8f91\u5668\u6269\u5c55<a href=\"https:\/\/translate.google.com\/website?sl=auto&amp;tl=zh-CN&amp;hl=zh-CN&amp;u=https:\/\/addons.mozilla.org\/en-US\/firefox\/addon\/cookie-editor\/\"><\/a><\/li>\n\n\n\n<li>\u53bb<code>bing.com<\/code><\/li>\n\n\n\n<li>\u6253\u5f00\u6269\u5c55<\/li>\n\n\n\n<li>\u5355\u51fb\u53f3\u4e0b\u89d2\u7684\u201c\u5bfc\u51fa\u201d\uff0c\u7136\u540e\u5355\u51fb\u201c\u5bfc\u51fa\u4e3a JSON\u201d\uff08\u8fd9\u4f1a\u5c06\u60a8\u7684 cookie \u4fdd\u5b58\u5230\u526a\u8d34\u677f\uff09<\/li>\n\n\n\n<li>\u5c06\u60a8\u7684 cookie \u7c98\u8d34\u5230\u6587\u4ef6\u4e2dbingcookies.json<\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/github-com.translate.goog\/acheong08\/EdgeGPT?_x_tr_sl=auto&amp;_x_tr_tl=zh-CN&amp;_x_tr_hl=zh-CN#chatbot\"><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd0\u884c\u524d\u5b89\u88c5\u597d\u4f9d\u8d56\u5305\uff0c\u7136\u540e\u5c06bing\u7684cookies \u7c98\u8d34\u5230bingcookies.json\u91cc \u4f7f\u7528\u8bf4\u660e\uff1a \u5b89\u88c5\u5305 python3 -m pip install EdgeGPT &#8211;upgrade \u8981\u6c42 \u68c0\u67e5\u8bbf\u95ee\u6743\u9650\uff08\u5fc5\u586b\uff09 \u83b7\u53d6\u8eab\u4efd\u9a8c\u8bc1\uff08\u5fc5\u586b\uff09<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_eb_attr":"","footnotes":""},"class_list":["post-96","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/pages\/96","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=96"}],"version-history":[{"count":0,"href":"https:\/\/oneai.eu.org\/index.php?rest_route=\/wp\/v2\/pages\/96\/revisions"}],"wp:attachment":[{"href":"https:\/\/oneai.eu.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=96"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}