From 532420d7f1995c61e88cc646bfa535a7fb6d0088 Mon Sep 17 00:00:00 2001 From: linghaihui <75124771@qq.com> Date: Thu, 6 Apr 2023 18:57:54 +0800 Subject: [PATCH] Fix some issues --- bingchat/components/chatbox/index.js | 27 ++++++++++++--------------- new-bing/app.py | 17 ++++++++++++++--- new-bing/conversation_ctr.py | 4 ++++ 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/bingchat/components/chatbox/index.js b/bingchat/components/chatbox/index.js index 8d696a2..311a7a1 100644 --- a/bingchat/components/chatbox/index.js +++ b/bingchat/components/chatbox/index.js @@ -28,21 +28,13 @@ Component({ }, pageLifetimes: { show: function () { - // this.initMessageHistory() + //this.initMessageHistory() }, }, lifetimes: { attached() { - var that = this - app.globalData.cht = that - that.initMessageHistory() - wx.getSystemInfo({ - success: function (res) { - that.setData({ - systemInfo: res, - }) - }, - }) + app.globalData.cht = this + this.initMessageHistory() }, detached() { try {} catch (error) {} @@ -55,6 +47,7 @@ Component({ closeShareOnCopy: closeShareOnCopy, showShare: false, loadingData: false, + systemInfo: systemInfo, height: systemInfo.windowHeight - parseInt(100 / 750 * systemInfo.windowWidth) - ((systemInfo.platform == "ios" || systemInfo.platform == "android") ? 22 : 5) }, methods: { @@ -106,13 +99,13 @@ Component({ if (filterData.length == 0 && e) { setTimeout(() => { wx.showToast({ - title: "已加载完成" + title: "已全部加载完成" }) - }, 300) + }, 100) } else { setTimeout(() => { wx.hideLoading() - }, 300) + }, 100) } }) }).catch(res => { @@ -122,7 +115,6 @@ Component({ }) }, initMessageHistory() { - //this.bindscrolltoupper() var that = this wx.getStorage({ key: "chatList", @@ -133,7 +125,12 @@ Component({ that.setData({ chatList: data, }) + } else { + that.bindscrolltoupper() } + }, + fail: function () { + that.bindscrolltoupper() } }) }, diff --git a/new-bing/app.py b/new-bing/app.py index 70231c6..7e43aa8 100644 --- a/new-bing/app.py +++ b/new-bing/app.py @@ -45,6 +45,13 @@ app.config.WEBSOCKET_PING_TIMEOUT = 30 OPENAI_CONVERSATION = defaultdict(lambda: []) +def show_chatgpt(sid): + for openid in conversation_ctr.get_openai_whitelist(): + if openid.decode() in sid: + return 1 + return 0 + + def reset_cookie(): if not LOCK.acquire(blocking=False): return @@ -217,6 +224,8 @@ async def ws_openai_chat(_, ws): data = raw_json.loads(await ws.recv()) logger.info('[openai] Websocket receive data: %s', data) sid = data['sid'] + if not show_chatgpt(sid): + raise Exception('无权限访问此服务') q = data['q'] # 保存30个对话 history_conversation = OPENAI_CONVERSATION[sid][-30:] @@ -227,7 +236,7 @@ async def ws_openai_chat(_, ws): response = openai.ChatCompletion.create( model='gpt-3.5-turbo', messages=history_conversation, - temperature=1.2, + temperature=0.8, stream=True, ) chunks = [] @@ -269,6 +278,8 @@ async def openai_chat(request): try: logger.info('[openai] Http request payload: %s', request.json) sid = request.json.get('sid') + if not show_chatgpt(sid): + raise Exception('无权限访问此服务') q = request.json.get('q') history_conversation = OPENAI_CONVERSATION[sid][-30:] history_conversation.append({ @@ -278,7 +289,7 @@ async def openai_chat(request): response = openai.ChatCompletion.create( model='gpt-3.5-turbo', messages=history_conversation, - temperature=1.2, + temperature=0.8, stream=True, ) chunks = [] @@ -306,7 +317,7 @@ async def last_sync_time(request): @app.post('/save') async def save(request): conversation_ctr.save(request.json.get('sid'), request.json.get('conversations')) - return json({'saved': 0}) + return json({'saved': show_chatgpt(request.json.get('sid'))}) @app.route('/query') diff --git a/new-bing/conversation_ctr.py b/new-bing/conversation_ctr.py index 9eb6ade..ec354c3 100644 --- a/new-bing/conversation_ctr.py +++ b/new-bing/conversation_ctr.py @@ -15,6 +15,7 @@ class ConversationCtr: LAST_SYNC_TIME_KEY = 'bing:last_sync_time:%s' CONVERSATION_LIST_KEY = 'bing:conversation_list:%s' + OPENAI_WHITE_LIST_KEY = 'bing:openai_white_list' def __init__(self, client=None) -> None: self.redis_client = client @@ -54,5 +55,8 @@ class ConversationCtr: key = self.CONVERSATION_LIST_KEY % sid self.redis_client.delete(key) + def get_openai_whitelist(self): + return self.redis_client.lrange(self.OPENAI_WHITE_LIST_KEY, 0, 10000) + conversation_ctr = ConversationCtr()