From 72f0fb324b44e2f976c7de45b456c7b16e2cca52 Mon Sep 17 00:00:00 2001 From: eliranwong Date: Sat, 16 Nov 2024 11:58:42 +0000 Subject: [PATCH] working on web chat --- setup.py | 2 +- uniquebible/__init__.py | 2 +- uniquebible/util/RemoteHttpHandler.py | 18 +++++++++-- uniquebible/util/TextCommandParser.py | 45 +++++++++++++++++++++++---- 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/setup.py b/setup.py index 27e964ef7a..f0a50c227b 100644 --- a/setup.py +++ b/setup.py @@ -47,7 +47,7 @@ # https://packaging.python.org/en/latest/guides/distributing-packages-using-setuptools/ setup( name=package, - version="0.1.84", + version="0.1.85", python_requires=">=3.8, <3.13", description=f"UniqueBible App is a cross-platform & offline bible application, integrated with high-quality resources and unique features. Developers: Eliran Wong and Oliver Tseng", long_description=long_description, diff --git a/uniquebible/__init__.py b/uniquebible/__init__.py index df054d5415..22e1daae87 100644 --- a/uniquebible/__init__.py +++ b/uniquebible/__init__.py @@ -238,7 +238,7 @@ def getChatResponse(backend, chatMessages) -> Optional[str]: textOutput = completion.choices[0].message.content # update the message chain in the config, to work with command `chat` config.chatMessages = chatMessages - config.chatMessages.append({"role": "user", "content": textOutput}) + config.chatMessages.append({"role": "assistant", "content": textOutput}) except: textOutput = "Failed to connect! Please try again later." if hasattr(config, "displayLanguage") and config.displayLanguage == "zh_HANT": diff --git a/uniquebible/util/RemoteHttpHandler.py b/uniquebible/util/RemoteHttpHandler.py index 6704705182..e56b6aa4b6 100644 --- a/uniquebible/util/RemoteHttpHandler.py +++ b/uniquebible/util/RemoteHttpHandler.py @@ -1,7 +1,7 @@ # https://docs.python.org/3/library/http.server.html # https://ironpython-test.readthedocs.io/en/latest/library/simplehttpserver.html import hashlib -import json +import json, markdown import logging import os, re, pprint, glob from uniquebible import config @@ -518,6 +518,7 @@ def loadContent(self): "logout": self.logout, "search": self.searchContent, "qna": self.qnaContent, + "chat": self.chatContent, "maps": self.mapsContent, "days": self.dailyReadingContent, "theme": self.swapTheme, @@ -1048,13 +1049,14 @@ def getSideNavContent(self): (config.thisTranslation["biblePromises"], ".promises"), (config.thisTranslation["bibleHarmonies"], ".parallels"), (config.thisTranslation["bibleAnswers"], ".qna") if config.addBibleQnA else None, + ("AI " + config.thisTranslation["chat"], ".chat"), (config.thisTranslation["readingPlan"], ".days"), ("{0} 🔎︎".format(config.thisTranslation["menu_search"]), ".search"), (config.thisTranslation["download"], ".download"), (config.thisTranslation["ubaCommands"], ".help"), ) for item in sideNavItems: - if item is not None: + if item is not None and not (item[0].startswith("AI ") and not config.webHomePage==f"{config.webPrivateHomePage}.html"): html += """{0}""".format(*item) html += """{0}""".format(config.thisTranslation["userManual"], self.getUserManual()) html += "
" @@ -1936,9 +1938,19 @@ def qnaContent(self): def chatContent(self): content = "

AI {0}

".format(config.thisTranslation["chat"]) - + if config.displayLanguage == "zh_HANT": + content += """

請輸入您的提問,然後按下按鈕「{0}」。

""".format(config.thisTranslation["send"]) + elif config.displayLanguage == "zh_HANS": + content += """

请输入您的提问,然后按下按钮「{0}」。

""".format(config.thisTranslation["send"]) + else: + content += """

Please enter your query and click the button '{0}'.

""".format(config.thisTranslation["send"]) content += "

" content += "

".format(config.thisTranslation["send"]) + + content += "
" + content += "\n\n".join([f'''# {i.get("role", "")}\n\n{i.get("content", "")}'''for i in config.chatMessages]) + textOutput = markdown.markdown(textOutput) + content += """ """.format("{", "}") + # Disclaimer textOutput += getAiFeatureDisclaimer() return ("study", textOutput, {'tab_title': "AI"})