Skip to content

Commit

Permalink
Merge pull request #114 from Integration-Automation/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
JE-Chen committed Feb 6, 2024
2 parents a558aa0 + 180eb84 commit fbf25f6
Show file tree
Hide file tree
Showing 14 changed files with 943 additions and 103 deletions.
4 changes: 2 additions & 2 deletions dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ build-backend = "setuptools.build_meta"

[project]
name = "re_edge_gpt_dev"
version = "0.0.31"
version = "0.0.33"
authors = [
{ name = "JE-Chen", email = "[email protected]" },
]
dependencies = [
"aiohttp", "certifi", "httpx", "prompt_toolkit", "requests", "rich", "regex"
"aiohttp", "certifi", "httpx", "prompt_toolkit", "requests", "rich", "regex", "Brotli"
]
description = "Microsoft's Bing Chat AI"
requires-python = ">=3.9"
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ build-backend = "setuptools.build_meta"

[project]
name = "re_edge_gpt"
version = "0.0.28"
version = "0.0.30"
authors = [
{ name = "JE-Chen", email = "[email protected]" },
]
dependencies = [
"aiohttp", "certifi", "httpx", "prompt_toolkit", "requests", "rich", "regex"
"aiohttp", "certifi", "httpx", "prompt_toolkit", "requests", "rich", "regex", "Brotli"
]
description = "Microsoft's Bing Chat AI"
requires-python = ">=3.9"
Expand Down
18 changes: 13 additions & 5 deletions re_edge_gpt/chat/chathub.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ async def ask_stream(
locale: str = guess_locale(),
# Use for attachment
attachment: dict = None,
autosave: bool = True
remove_options: list = None,
add_options: list = None
) -> Generator[bool, Union[dict, str], None]:
""" """
if self.encrypted_conversation_signature is not None:
Expand All @@ -107,14 +108,21 @@ async def ask_stream(
image_url = None
if attachment is not None:
if attachment.get("image_url") is not None:
response = await upload_image_url(**attachment, conversation_id=self.conversation_id)
response = await upload_image_url(
**attachment, conversation_id=self.conversation_id, cookies=cookies)
else:
response = await upload_image(**attachment)
response = await upload_image(
**attachment, conversation_id=self.conversation_id, cookies=cookies)
if response:
image_url = f"https://www.bing.com/images/blob?bcid={response}"
# Construct a ChatHub request
if autosave is False and "autosave" in conversation_style.value:
conversation_style.value.remove("autosave")
if remove_options is not None:
for option in remove_options:
if option in remove_options:
conversation_style.value.remove(option)
if add_options is not None:
for option in add_options:
conversation_style.value.append(option)
self.request.update(
prompt=prompt,
conversation_style=conversation_style,
Expand Down
76 changes: 44 additions & 32 deletions re_edge_gpt/chat/re_edge_gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from .chathub import *
from .conversation import *
from .request import *
from re_edge_gpt.utils.utilities import *


class Chatbot:
Expand Down Expand Up @@ -53,7 +52,8 @@ async def ask(
locale: str = guess_locale(),
simplify_response: bool = False,
attachment: dict[str, str] = None,
autosave: bool = True
remove_options: list = None,
add_options: list = None
):
"""
Ask a question to the bot
Expand All @@ -72,7 +72,8 @@ async def ask(
attachment={"filename": r"<file_path>"})
For base64 image using
attachment={"base64_image": r"<base64_image_str>"})
:param autosave: add autosave on request
:param remove_options remove options from Style
:param add_options add options to Style
"""
async for final, response in self.chat_hub.ask_stream(
prompt=prompt,
Expand All @@ -82,47 +83,56 @@ async def ask(
search_result=search_result,
locale=locale,
attachment=attachment,
autosave=autosave
remove_options=remove_options,
add_options=add_options
):
if final:
if not simplify_response:
return response
messages_left = response["item"]["throttling"][
"maxNumUserMessagesInConversation"
] - response["item"]["throttling"].get(
messages_left = (response.get("item").get("throttling").get("maxNumUserMessagesInConversation")
- response.get("item").get("throttling").get(
"numUserMessagesInConversation",
0,
)
))
if messages_left == 0:
raise Exception("Max messages reached")
message = ""
for msg in reversed(response["item"]["messages"]):
if msg.get("adaptiveCards") and msg["adaptiveCards"][0]["body"][
0
].get("text"):
message = msg
break
message = {}
for msg in reversed(response.get("item").get("messages")):
if msg.get("author") == "bot":
old_message = message.get("text")
if old_message:
old_message = old_message + " \n "
else:
old_message = ""
message.update({
"author": "bot",
"text": old_message + msg.get("text")
})
if not message:
raise Exception("No message found")
suggestions = [
suggestion["text"]
for suggestion in message.get("suggestedResponses", [])
]
adaptive_cards = message.get("adaptiveCards", [])
sources = (
adaptive_cards[0]["body"][0].get("text") if adaptive_cards else None
)
sources_link = (
adaptive_cards[0]["body"][-1].get("text")
if adaptive_cards
else None
)
image_create_text = ""
suggestions = []
source_texts = []
source_links = []
for detail in reversed(response.get("item").get("messages")):
suggestion_responses = detail.get("suggestedResponses", {})
source_attr = detail.get("sourceAttributions", {})
if suggestion_responses:
for suggestion in suggestion_responses:
suggestions.append(suggestion.get("text"))
if source_attr:
for source in source_attr:
source_texts.append(source.get("providerDisplayName"))
source_links.append(source.get("seeMoreUrl"))
if detail.get("contentType") == "IMAGE" and detail.get("messageType") == "GenerateContentQuery":
image_create_text = detail.get("text")
return {
"text": message["text"],
"author": message["author"],
"sources": sources,
"sources_link": sources_link,
"source_texts": source_texts,
"source_links": source_links,
"suggestions": suggestions,
"image_create_text": image_create_text,
"messages_left": messages_left,
"max_messages": response["item"]["throttling"][
"maxNumUserMessagesInConversation"
Expand All @@ -139,7 +149,8 @@ async def ask_stream(
webpage_context: str | None = None,
search_result: bool = False,
locale: str = guess_locale(),
autosave: bool = True
remove_options: list = None,
add_options: list = None
) -> Generator[bool, dict | str, None]:
"""
Ask a question to the bot
Expand All @@ -152,7 +163,8 @@ async def ask_stream(
webpage_context=webpage_context,
search_result=search_result,
locale=locale,
autosave=autosave
remove_options=remove_options,
add_options=add_options
):
yield response

Expand Down
11 changes: 5 additions & 6 deletions re_edge_gpt/chat/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,14 @@ def update(
"allowedMessageTypes": [
"ActionRequest",
"Chat",
"ConfirmationCard",
"Context",
"Disengaged",
"InternalSearchQuery",
"InternalSearchResult",
"InternalLoaderMessage",
"Progress",
"RenderContentRequest",
"AdsQuery",
"SemanticSerp",
"GenerateContentQuery",
"SearchQuery"
"SearchQuery",
"GeneratedCode",
],
"sliceIds": [
"schurmsg",
Expand Down
21 changes: 15 additions & 6 deletions re_edge_gpt/image/upload_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import aiohttp

from re_edge_gpt.utils.constants import IMAGE_HEADER

payload = {
"imageInfo": {},
"knowledgeRequest": {
Expand All @@ -17,9 +19,10 @@
}


async def upload_image_url(image_url: str, conversation_id: str, proxy: str = None, face_blur: bool = True):
async def upload_image_url(image_url: str, conversation_id: str, cookies: dict,
proxy: str = None, face_blur: bool = True):
async with aiohttp.ClientSession(
headers={"Referer": "https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx"},
headers=IMAGE_HEADER, cookies=cookies
) as session:
url = "https://www.bing.com/images/kblob"

Expand All @@ -31,17 +34,22 @@ async def upload_image_url(image_url: str, conversation_id: str, proxy: str = No
data = aiohttp.FormData()
data.add_field('knowledgeRequest', json.dumps(new_payload), content_type="application/json")
async with session.post(url, data=data, proxy=proxy) as resp:
if not resp.status == 200:
raise Exception("Upload image failed")
return (await resp.json())["blobId"]


async def upload_image(filename: str = None, base64_image: str = None, proxy: str = None, face_blur: bool = True):
async def upload_image(conversation_id: str, cookies: dict, filename: str = None,
base64_image: str = None, proxy: str = None, face_blur: bool = True):
async with aiohttp.ClientSession(
headers={"Referer": "https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx"},
headers=IMAGE_HEADER, cookies=cookies
) as session:
url = "https://www.bing.com/images/kblob"

new_payload = payload.get("knowledgeRequest").update(
new_payload = payload
new_payload.get("knowledgeRequest").update(
{"invokedSkillsRequestData": {"enableFaceBlur": face_blur}})
new_payload.get("knowledgeRequest").get("convoData").update({"convoid": conversation_id})

if filename is not None:
with open(filename, 'rb') as f:
Expand All @@ -55,6 +63,7 @@ async def upload_image(filename: str = None, base64_image: str = None, proxy: st
data = aiohttp.FormData()
data.add_field('knowledgeRequest', json.dumps(new_payload), content_type="application/json")
data.add_field('imageBase64', image_base64, content_type="application/octet-stream")

async with session.post(url, data=data, proxy=proxy) as resp:
if not resp.status == 200:
raise Exception("Upload image failed")
return (await resp.json())["blobId"]
52 changes: 40 additions & 12 deletions re_edge_gpt/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"accept-language": "en;q=0.9,en-US;q=0.8",
"accept-encoding": "gzip, deflate, br, zsdch",
"content-type": "application/json",
"sec-ch-ua": '"Microsoft Edge";v="120", '
'"Chromium";v="120", '
'"Not?A_Brand";v="8"',
"sec-ch-ua": '"Not A(Brand";v="99", '
'"Microsoft Edge";v="121", '
'"Chromium";v="121"',
"sec-ch-ua-arch": '"x86"',
"sec-ch-ua-bitness": '"64"',
"sec-ch-ua-full-version": '"1-120.0.2210.133"',
"sec-ch-ua-full-version-list": '"Not_A Brand";v="8.0.0.0", '
'"Chromium";v="120.0.6099.217", '
'"Microsoft Edge";v="120.0.2210.133',
"sec-ch-ua-full-version": '"121.0.2277.83"',
"sec-ch-ua-full-version-list": '"Not A(Brand";v="99.0.0.0", '
'"Microsoft Edge";v="121.0.2277.83", '
'"Chromium";v="121.0.6167.85"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-model": "",
"sec-ch-ua-platform": '"Windows"',
Expand All @@ -39,13 +39,15 @@
"accept": "application/json",
"accept-language": "en;q=0.9,en-US;q=0.8",
"cache-control": "max-age=0",
"sec-ch-ua": '"Not_A Brand";v="8.0.0.0", "Chromium";v="120.0.6099.217", "Microsoft Edge";v="120.0.2210.133',
"sec-ch-ua": '"Not A(Brand";v="99", '
'"Microsoft Edge";v="121", '
'"Chromium";v="121"',
"sec-ch-ua-arch": '"x86"',
"sec-ch-ua-bitness": '"64"',
"sec-ch-ua-full-version": '"1-120.0.2210.133"',
"sec-ch-ua-full-version-list": '"Not_A Brand";v="8.0.0.0", '
'"Chromium";v="120.0.6099.217", '
'"Microsoft Edge";v="120.0.2210.133',
"sec-ch-ua-full-version": '"121.0.2277.83"',
"sec-ch-ua-full-version-list": '"Not A(Brand";v="99.0.0.0", '
'"Microsoft Edge";v="121.0.2277.83", '
'"Chromium";v="121.0.6167.85"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-model": '""',
"sec-ch-ua-platform": '"Windows"',
Expand All @@ -61,6 +63,32 @@
"x-forwarded-for": FORWARDED_IP,
}

IMAGE_HEADER = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "multipart/form-data",
"Referer": "https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx",
"sec-ch-ua": '"Not A(Brand";v="99", '
'"Microsoft Edge";v="121", '
'"Chromium";v="121"',
"sec-ch-ua-arch": '"x86"',
"sec-ch-ua-bitness": '"64"',
"sec-ch-ua-full-version": '"121.0.2277.83"',
"sec-ch-ua-full-version-list": '"Not A(Brand";v="99.0.0.0", '
'"Microsoft Edge";v="121.0.2277.83", '
'"Chromium";v="121.0.6167.85"',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": "Windows",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/120.0.0.0 "
"Safari/537.36 "
"Edg/120.0.0.0",
}

BUNDLE_VERSION = "1.1498.1"

SYDNEY_INIT_HEADER = HEADERS_INIT_CONVER.update(
Expand Down
Loading

0 comments on commit fbf25f6

Please sign in to comment.