-
Notifications
You must be signed in to change notification settings - Fork 921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add more functions #364
Open
sastpg
wants to merge
6
commits into
zhayujie:master
Choose a base branch
from
sastpg:dev-newbing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add more functions #364
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8993093
防止消息撤回,增加对话风格切换
sastpg 64b0781
适配EdgeGPT 0.3.5; 增加防止微软撤回AI消息; 增加对话风格切换
sastpg 6f6a950
update requirements
sastpg c591df2
Update reference, fix bug
sastpg a79e134
refactor variable name
sastpg 5f62d13
fix reference bug; add server disconnected response
sastpg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,25 +6,23 @@ | |
from EdgeGPT import Chatbot, ConversationStyle | ||
from ImageGen import ImageGen | ||
from common import functions | ||
from model.bing.jailbroken_sydney import SydneyBot | ||
import random | ||
import json | ||
|
||
user_session = dict() | ||
user_chathistory = dict() | ||
suggestion_session = dict() | ||
# newBing对话模型逆向网页gitAPI | ||
|
||
|
||
class BingModel(Model): | ||
|
||
style = ConversationStyle.creative | ||
bot: Chatbot = None | ||
cookies: list = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cookies->cookies_list |
||
cookies = None | ||
|
||
def __init__(self): | ||
try: | ||
self.cookies = model_conf_val("bing", "cookies") | ||
self.jailbreak = model_conf_val("bing", "jailbreak") | ||
self.bot = SydneyBot(cookies=self.cookies, options={}) if ( | ||
self.jailbreak) else Chatbot(cookies=self.cookies) | ||
except Exception as e: | ||
log.warn(e) | ||
|
||
|
@@ -34,79 +32,151 @@ async def handle_answer(final, answer): | |
try: | ||
reply = self.build_source_attributions(answer, context) | ||
log.info("[NewBing] reply:{}", reply) | ||
await bot.close() | ||
yield True, reply | ||
except Exception as e: | ||
log.warn(answer) | ||
log.warn(e) | ||
await user_session.get(context['from_user_id'], None).reset() | ||
await user_chathistory.get(context['from_user_id'], None).reset() | ||
yield True, answer | ||
else: | ||
try: | ||
yield False, answer | ||
except Exception as e: | ||
log.warn(answer) | ||
log.warn(e) | ||
await user_session.get(context['from_user_id'], None).reset() | ||
await user_chathistory.get(context['from_user_id'], None).reset() | ||
yield True, answer | ||
|
||
if not context or not context.get('type') or context.get('type') == 'TEXT': | ||
clear_memory_commands = common_conf_val( | ||
'clear_memory_commands', ['#清除记忆']) | ||
if query in clear_memory_commands: | ||
user_session[context['from_user_id']] = None | ||
user_chathistory[context['from_user_id']] = None | ||
yield True, '记忆已清除' | ||
bot = user_session.get(context['from_user_id'], None) | ||
if not bot: | ||
bot = self.bot | ||
|
||
chat_style = "" | ||
chat_history = "" | ||
if user_chathistory.get(context['from_user_id'], None) == None: | ||
if (self.jailbreak): | ||
chars = model_conf_val("bing", "jailbreak_prompt") | ||
chars = chars + "\n\n" | ||
chat_history = ''.join(chars) | ||
user_chathistory[context['from_user_id']] = ['creative', chat_history] | ||
else: | ||
query = self.get_quick_ask_query(query, context) | ||
user_session[context['from_user_id']] = bot | ||
if not chat_history.endswith("\n\n"): | ||
if chat_history.endswith("\n"): | ||
chat_history += "\n" | ||
else: | ||
chat_history += "\n\n" | ||
chat_style = user_chathistory[context['from_user_id']][0] | ||
chat_history = user_chathistory[context['from_user_id']][1] | ||
|
||
query = self.get_quick_ask_query(query, context) | ||
bot = await Chatbot.create(cookies=self.cookies) | ||
user_chathistory[context['from_user_id']][1] += f"[user](#message)\n{query}\n\n" | ||
log.info("[NewBing] query={}".format(query)) | ||
if self.jailbreak: | ||
async for final, answer in bot.ask_stream(query, conversation_style=self.style, message_id=bot.user_message_id): | ||
async for result in handle_answer(final, answer): | ||
yield result | ||
else: | ||
async for final, answer in bot.ask_stream(query, conversation_style=self.style): | ||
async for result in handle_answer(final, answer): | ||
yield result | ||
|
||
def reply(self, query: str, context=None) -> tuple[str, dict]: | ||
if not context or not context.get('type') or context.get('type') == 'TEXT': | ||
clear_memory_commands = common_conf_val( | ||
'clear_memory_commands', ['#清除记忆']) | ||
if query in clear_memory_commands: | ||
user_session[context['from_user_id']] = None | ||
return '记忆已清除' | ||
bot = user_session.get(context['from_user_id'], None) | ||
if (bot == None): | ||
bot = self.bot | ||
else: | ||
query = self.get_quick_ask_query(query, context) | ||
async for final, answer in bot.ask_stream(prompt=query, raw=True, webpage_context=chat_history, conversation_style=chat_style, search_result=True): | ||
async for result in handle_answer(final, answer): | ||
yield result | ||
|
||
user_session[context['from_user_id']] = bot | ||
log.info("[NewBing] query={}".format(query)) | ||
if (self.jailbreak): | ||
task = bot.ask(query, conversation_style=self.style, | ||
message_id=bot.user_message_id) | ||
else: | ||
task = bot.ask(query, conversation_style=self.style) | ||
|
||
answer = asyncio.run(task) | ||
if isinstance(answer, str): | ||
return answer | ||
try: | ||
reply = answer["item"]["messages"][-1] | ||
except Exception as e: | ||
user_session.get(context['from_user_id'], None).reset() | ||
log.warn(answer) | ||
return "本轮对话已超时,已开启新的一轮对话,请重新提问。" | ||
return self.build_source_attributions(answer, context) | ||
def reply(self, query: str, context=None): | ||
if not context or not context.get('type') or context.get('type') == 'TEXT': | ||
return asyncio.run(self.__reply(query, context)) | ||
elif context.get('type', None) == 'IMAGE_CREATE': | ||
if functions.contain_chinese(query): | ||
return "ImageGen目前仅支持使用英文关键词生成图片" | ||
return self.create_img(query) | ||
|
||
async def __reply(self, query: str, context): | ||
clear_memory_commands = common_conf_val( | ||
'clear_memory_commands', ['#清除记忆']) | ||
if query in clear_memory_commands: | ||
user_chathistory[context['from_user_id']] = None | ||
return '记忆已清除' | ||
|
||
# deal chat_history | ||
chat_style = "" | ||
chat_history = "" | ||
if user_chathistory.get(context['from_user_id'], None) == None: | ||
if (self.jailbreak): | ||
chars = model_conf_val("bing", "jailbreak_prompt") | ||
chars = chars + "\n\n" | ||
chat_history = ''.join(chars) | ||
user_chathistory[context['from_user_id']] = ['creative', chat_history] | ||
else: | ||
if not chat_history.endswith("\n\n"): | ||
if chat_history.endswith("\n"): | ||
chat_history += "\n" | ||
else: | ||
chat_history += "\n\n" | ||
chat_style = user_chathistory[context['from_user_id']][0] | ||
chat_history = user_chathistory[context['from_user_id']][1] | ||
|
||
query = self.get_quick_ask_query(query, context) | ||
if query == "输入的序号不在建议列表范围中": | ||
return "对不起,您输入的序号不在建议列表范围中(数字1-9均会被认为是建议列表),请重新输入。" | ||
if "[style]已切换至" in query: | ||
return query | ||
|
||
log.info("[NewBing] query={}".format(query)) | ||
bot = await Chatbot.create(cookies=self.cookies) | ||
reply_text = "" | ||
reference = "" | ||
suggestion = "" | ||
async def stream_output(): | ||
nonlocal chat_history | ||
nonlocal chat_style | ||
nonlocal reply_text | ||
nonlocal reference | ||
nonlocal suggestion | ||
chat_history += f"[user](#message)\n{query}\n\n" | ||
wrote = 0 | ||
async for final, response in bot.ask_stream( | ||
prompt=query, | ||
raw=True, | ||
webpage_context=chat_history, | ||
conversation_style=chat_style, | ||
search_result=True | ||
): | ||
if not final and response["type"] == 1 and "messages" in response["arguments"][0]: | ||
message = response["arguments"][0]["messages"][0] | ||
if message.get("messageType") == "InternalSearchQuery": | ||
#chat_history += f"[assistant](#search_query)\n{message['hiddenText']}\n\n" | ||
pass | ||
elif message.get("messageType") == "InternalSearchResult": | ||
#chat_history += f"[assistant](#search_results)\n{message['hiddenText']}\n\n" | ||
reference += f"[assistant](#search_results)\n{message['hiddenText']}" | ||
elif message.get("messageType") == None: | ||
if "cursor" in response["arguments"][0]: | ||
chat_history += "[assistant](#message)\n" | ||
wrote = 0 | ||
if message.get("contentOrigin") == "Apology": | ||
log.info("检测到AI生成内容被撤回...已阻止") | ||
break | ||
else: | ||
chat_history += message["text"][wrote:] | ||
reply_text += message["text"][wrote:] | ||
wrote = len(message["text"]) | ||
if "suggestedResponses" in message: | ||
suggestion = list(map(lambda x: x["text"], message["suggestedResponses"])) | ||
chat_history += f"""\n[assistant](#suggestions) | ||
```json | ||
{{"suggestedUserResponses": {suggestion}}} | ||
```\n\n""" | ||
break | ||
if final and not response["item"]["messages"][-1].get("text"): | ||
raise Exception("发送的消息被过滤或者对话超时") | ||
|
||
try: | ||
await stream_output() | ||
except Exception as e: | ||
log.info(e) | ||
|
||
# 更新历史对话 | ||
user_chathistory[context['from_user_id']][1] = chat_history | ||
await bot.close() | ||
return self.build_source_text(reply_text, reference, suggestion, context) | ||
|
||
def create_img(self, query): | ||
try: | ||
log.info("[NewBing] image_query={}".format(query)) | ||
|
@@ -123,17 +193,30 @@ def get_quick_ask_query(self, query, context): | |
if (len(query) == 1 and query.isdigit() and query != "0"): | ||
suggestion_dict = suggestion_session[context['from_user_id']] | ||
if (suggestion_dict != None): | ||
query = suggestion_dict[int(query)-1] | ||
if (query == None): | ||
try: | ||
query = suggestion_dict[int(query)-1] | ||
if (query == None): | ||
return "输入的序号不在建议列表范围中" | ||
else: | ||
query = "在上面的基础上,"+query | ||
except: | ||
return "输入的序号不在建议列表范围中" | ||
else: | ||
query = "在上面的基础上,"+query | ||
elif(query == "/creative"): | ||
user_chathistory[context['from_user_id']][0] = query[1:] | ||
return "[style]已切换至创造模式" | ||
elif(query == "/balanced"): | ||
user_chathistory[context['from_user_id']][0] = query[1:] | ||
return "[style]已切换至平衡模式" | ||
elif(query == "/precise"): | ||
user_chathistory[context['from_user_id']][0] = query[1:] | ||
return "[style]已切换至精确模式" | ||
return query | ||
|
||
def build_source_attributions(self, answer, context): | ||
reference = "" | ||
reply = answer["item"]["messages"][-1] | ||
reply_text = reply["text"] | ||
user_chathistory[context['from_user_id']][1] += f"[assistant](#message)\n{reply_text}\n" | ||
if "sourceAttributions" in reply: | ||
for i, attribution in enumerate(reply["sourceAttributions"]): | ||
display_name = attribution["providerDisplayName"] | ||
|
@@ -159,7 +242,7 @@ def build_source_attributions(self, answer, context): | |
throttling_str = "" | ||
|
||
if throttling["numUserMessagesInConversation"] == throttling["maxNumUserMessagesInConversation"]: | ||
user_session.get(context['from_user_id'], None).reset() | ||
user_chathistory.get(context['from_user_id'], None).reset() | ||
throttling_str = "(对话轮次已达上限,本次聊天已结束,将开启新的对话)" | ||
else: | ||
throttling_str = f"对话轮次: {throttling['numUserMessagesInConversation']}/{throttling['maxNumUserMessagesInConversation']}\n" | ||
|
@@ -168,6 +251,35 @@ def build_source_attributions(self, answer, context): | |
log.info("[NewBing] reply={}", response) | ||
return response | ||
else: | ||
user_session.get(context['from_user_id'], None).reset() | ||
user_chathistory.get(context['from_user_id'], None).reset() | ||
log.warn("[NewBing] reply={}", answer) | ||
return "对话被接口拒绝,已开启新的一轮对话。" | ||
|
||
def build_source_text(self, reply_text, reference, suggestion, context): | ||
if not reply_text.endswith("\n\n"): | ||
if reply_text.endswith("\n"): | ||
reply_text += "\n" | ||
else: | ||
reply_text += "\n\n" | ||
|
||
references = "" | ||
if 'json' in reference: | ||
reference_dict = json.loads(reference[36:-3]) | ||
for i in range(len(reference_dict['web_search_results'])): | ||
r = reference_dict['web_search_results'][i] | ||
title = r['title'] | ||
url = r['url'] | ||
references += f"{i+1}、[{title}]({url})\n\n" | ||
|
||
suggestions = "" | ||
suggestion_dict = dict() | ||
if len(suggestion) > 0: | ||
for i in range(len(suggestion)): | ||
suggestion_dict[i] = suggestion[i] | ||
suggestions += f">{i+1}、{suggestion[i]}\n\n" | ||
suggestions = "=====\n💡你可能想问(输入序号):\n\n" + suggestions | ||
suggestion_session[context['from_user_id']] = suggestion_dict | ||
|
||
response = f"{reply_text}******\n{references}{suggestions}******\n" | ||
log.info("[NewBing] reply={}", response) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
user_chathistory->user_chat_history