diff --git a/README.md b/README.md index 175e94ee..fe6104eb 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ AI Vtuber是一个由 ChatterBot/GPT/Claude/langchain本地or云端/chatglm/text - [讯飞星火](#%E8%AE%AF%E9%A3%9E%E6%98%9F%E7%81%AB) - [闻达](#%E9%97%BB%E8%BE%BE) - [ChatterBot](#chatterbot) + - [Bard](#bard) - [TTS](#tts) - [elevenlabs](#elevenlabs) - [genshinvoice.top](#genshinvoicetop) @@ -1004,7 +1005,12 @@ ChatterBot 的核心思想是:基于历史对话数据,使用机器学习和 总的来说,ChatterBot 是一个非常强大、灵活、易用的聊天机器人框架,帮助开发者快速搭建出个性化、定制化的聊天机器人,从而提升用户体验和服务质量。 - +#### Bard +官方:[https://bard.google.com/](https://bard.google.com/) +token获取方式: +访问 https://bard.google.com/,登录账号 +键盘F12 打开开发者工具 +应用程序 → Cookie → 复制 Cookie 中 __Secure-1PSID 对应的值。 ### TTS @@ -1579,6 +1585,9 @@ cmd运行`npm i docsify-cli -g` - 更新b站监听库到16.0.0版本,新增b站登录板块,获取cookie等信息解决弹幕监听丢失问题(强烈建议使用小号登录,有风险) - 修复GUI b站部分配置项描述错误 +- 2023-09-03 + - 新增 谷歌bard的接入,支持上下文记忆。 + diff --git a/UI_main.py b/UI_main.py index 56107b4c..2bb8a043 100644 --- a/UI_main.py +++ b/UI_main.py @@ -86,7 +86,7 @@ def setupUi(self, MainWindow): self.scrollArea.setWidgetResizable(True) self.scrollArea.setObjectName("scrollArea") self.scrollAreaWidgetContents = QtWidgets.QWidget() - self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 984, 9957)) + self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, -5167, 984, 10033)) self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents") self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents) self.verticalLayout.setContentsMargins(35, 20, 35, 20) @@ -919,6 +919,14 @@ def setupUi(self, MainWindow): self.gridLayout_54.addWidget(self.lineEdit_sparkdesk_api_key, 6, 1, 1, 1) self.gridLayout_55.addLayout(self.gridLayout_54, 0, 0, 1, 1) self.verticalLayout.addWidget(self.groupBox_sparkdesk) + self.groupBox_bard = QtWidgets.QGroupBox(self.scrollAreaWidgetContents) + self.groupBox_bard.setObjectName("groupBox_bard") + self.gridLayout_74 = QtWidgets.QGridLayout(self.groupBox_bard) + self.gridLayout_74.setObjectName("gridLayout_74") + self.gridLayout_bard = QtWidgets.QGridLayout() + self.gridLayout_bard.setObjectName("gridLayout_bard") + self.gridLayout_74.addLayout(self.gridLayout_bard, 0, 0, 1, 1) + self.verticalLayout.addWidget(self.groupBox_bard) self.groupBox_chatterbot = QtWidgets.QGroupBox(self.scrollAreaWidgetContents) self.groupBox_chatterbot.setObjectName("groupBox_chatterbot") self.gridLayout_12 = QtWidgets.QGridLayout(self.groupBox_chatterbot) @@ -2077,6 +2085,7 @@ def retranslateUi(self, MainWindow): self.label_sparkdesk_fd.setText(_translate("MainWindow", "fd")) self.label_sparkdesk_type.setText(_translate("MainWindow", "类型")) self.label_sparkdesk_api_key.setText(_translate("MainWindow", "api_key")) + self.groupBox_bard.setTitle(_translate("MainWindow", "Bard")) self.groupBox_chatterbot.setTitle(_translate("MainWindow", "Chatterbot")) self.label_chatterbot_name.setText(_translate("MainWindow", "bot名称")) self.label_chatterbot_db_path.setText(_translate("MainWindow", "数据库路径")) diff --git a/config.json b/config.json index ed2ea7f3..43b8ff59 100644 --- a/config.json +++ b/config.json @@ -245,6 +245,9 @@ "instruction_template": "Vicuna-v1.1", "your_name": "你" }, + "bard": { + "token": "" + }, "local_qa": { "text": { "enable": true, diff --git a/config.json.bak b/config.json.bak index ed2ea7f3..43b8ff59 100644 --- a/config.json.bak +++ b/config.json.bak @@ -245,6 +245,9 @@ "instruction_template": "Vicuna-v1.1", "your_name": "你" }, + "bard": { + "token": "" + }, "local_qa": { "text": { "enable": true, diff --git a/docs/AI Vtuber.xmind b/docs/AI Vtuber.xmind index 3d4958c6..49ac6cfb 100644 Binary files a/docs/AI Vtuber.xmind and b/docs/AI Vtuber.xmind differ diff --git a/docs/xmind.png b/docs/xmind.png index 17770342..3a81729c 100644 Binary files a/docs/xmind.png and b/docs/xmind.png differ diff --git a/main.py b/main.py index bdb59e18..175d8e01 100644 --- a/main.py +++ b/main.py @@ -682,7 +682,8 @@ def init_config(self): "text_generation_webui", "讯飞星火", "langchain_chatglm", - "智谱AI" + "智谱AI", + "Bard" ]) chat_type_index = 0 if self.chat_type == "none": @@ -709,6 +710,8 @@ def init_config(self): chat_type_index = 10 elif self.chat_type == "zhipu": chat_type_index = 11 + elif self.chat_type == "bard": + chat_type_index = 12 self.ui.comboBox_chat_type.setCurrentIndex(chat_type_index) self.ui.comboBox_need_lang.clear() @@ -1809,6 +1812,31 @@ def bilibili_gui_create(): bilibili_gui_create() + # bard + def bard_gui_create(): + data_json = [] + bard_config = config.get("bard") + + tmp_json = { + "label_text": "token", + "label_tip": "登录bard,打开F12,在cookie中获取 __Secure-1PSID 对应的值", + "data": bard_config["token"], + "main_obj_name": "bard", + "index": 1 + } + data_json.append(tmp_json) + + widgets = self.create_widgets_from_json(data_json) + + # 动态添加widget到对应的gridLayout + row = 0 + # 分2列,左边就是label说明,右边就是输入框等 + for i in range(0, len(widgets), 2): + self.ui.gridLayout_bard.addWidget(widgets[i], row, 0) + self.ui.gridLayout_bard.addWidget(widgets[i + 1], row, 1) + row += 1 + + bard_gui_create() """ ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ @@ -2057,6 +2085,8 @@ def common_textEdit_handle(content): config_data["chat_type"] = "langchain_chatglm" elif chat_type == "智谱AI": config_data["chat_type"] = "zhipu" + elif chat_type == "Bard": + config_data["chat_type"] = "bard" config_data["before_prompt"] = self.ui.lineEdit_before_prompt.text() @@ -2585,6 +2615,22 @@ def reorganize_bilibili_data(bilibili_data): # 写回json config_data["bilibili"] = reorganize_bilibili_data(bilibili_data) + # bard + def reorganize_bard_data(bard_data): + keys = list(bard_data.keys()) + + tmp_json = { + "token": bard_data[keys[0]] + } + + logging.debug(f"tmp_json={tmp_json}") + + return tmp_json + + bard_data = self.update_data_from_gridLayout(self.ui.gridLayout_bard) + # 写回json + config_data["bard"] = reorganize_bard_data(bard_data) + """ ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ ------------------------------------------------------------------------------------------------------------- @@ -3234,21 +3280,22 @@ def show_box_clicked(self, status, text): def oncomboBox_chat_type_IndexChanged(self, index): # 各index对应的groupbox的显隐值 visibility_map = { - 0: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), - 1: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), - 2: (1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0), - 3: (0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0), - 4: (0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0), - 5: (0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0), - 6: (1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0), - 7: (0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0), - 8: (0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0), - 9: (0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0), - 10: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0), - 11: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), + 0: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), + 1: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), + 2: (1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), + 3: (0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0), + 4: (0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0), + 5: (0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0), + 6: (1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0), + 7: (0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0), + 8: (0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0), + 9: (0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0), + 10: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0), + 11: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0), + 12: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1), } - visibility_values = visibility_map.get(index, (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) + visibility_values = visibility_map.get(index, (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) self.ui.groupBox_openai.setVisible(visibility_values[0]) self.ui.groupBox_chatgpt.setVisible(visibility_values[1]) @@ -3262,6 +3309,7 @@ def oncomboBox_chat_type_IndexChanged(self, index): self.ui.groupBox_sparkdesk.setVisible(visibility_values[9]) self.ui.groupBox_langchain_chatglm.setVisible(visibility_values[10]) self.ui.groupBox_zhipu.setVisible(visibility_values[11]) + self.ui.groupBox_bard.setVisible(visibility_values[12]) # 语音合成类型改变 加载显隐不同groupBox @@ -3651,4 +3699,5 @@ def emit(self, record): e = AI_VTB() - sys.exit(e.app.exec()) \ No newline at end of file + sys.exit(e.app.exec()) + \ No newline at end of file diff --git a/tests/test_bard/qa.py b/tests/test_bard/qa.py new file mode 100644 index 00000000..944d93ed --- /dev/null +++ b/tests/test_bard/qa.py @@ -0,0 +1,20 @@ +import bardapi + +""" +访问 https://bard.google.com/ +F12 for console 用于控制台的 F12 +会话:应用程序→ Cookie → 复制 Cookie 的值 __Secure-1PSID 。 +""" +token = '' +proxies = { + 'http': 'http://127.0.0.1:10809', + 'https': 'http://127.0.0.1:10809' +} +#proxies = None + +input_text = "你好" +response = bardapi.core.Bard(token, proxies=proxies, timeout=30).get_answer(input_text) +print(response) +print(response["content"]) +# bard = Bard(token=token, proxies=proxies, timeout=30) +# bard.get_answer("你好")['content'] \ No newline at end of file diff --git a/tests/test_bard/qa2.py b/tests/test_bard/qa2.py new file mode 100644 index 00000000..e54f1285 --- /dev/null +++ b/tests/test_bard/qa2.py @@ -0,0 +1,26 @@ +from bardapi import Bard +import requests + +""" +访问 https://bard.google.com/ +F12 for console 用于控制台的 F12 +会话:应用程序→ Cookie → 复制 Cookie 的值 __Secure-1PSID 。 +""" +token='' + +session = requests.Session() +session.headers = { + "Host": "bard.google.com", + "X-Same-Domain": "1", + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36", + "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", + "Origin": "https://bard.google.com", + "Referer": "https://bard.google.com/", + } +session.cookies.set("__Secure-1PSID", token) + +bard = Bard(token=token, session=session, timeout=30) +print(bard.get_answer("你可以扮演猫娘吗,每句话后面加个喵")['content']) + +# Continued conversation without set new session +print(bard.get_answer("早上好")['content']) \ No newline at end of file diff --git a/tests/test_bard/tts.py b/tests/test_bard/tts.py new file mode 100644 index 00000000..ad5634d4 --- /dev/null +++ b/tests/test_bard/tts.py @@ -0,0 +1,15 @@ +from bardapi import Bard + +""" +访问 https://bard.google.com/ +F12 for console 用于控制台的 F12 +会话:应用程序→ Cookie → 复制 Cookie 的值 __Secure-1PSID 。 +""" +token = '' + +bard = Bard(token=token) +content = 'Hello, I am Bard! How can I help you today?' +content = '你好' +audio = bard.speech(content) +with open("speech.ogg", "wb") as f: + f.write(bytes(audio['audio'])) \ No newline at end of file diff --git a/tests/test_yiyan/web.py b/tests/test_yiyan/web.py index 421a4dff..a8302a98 100644 --- a/tests/test_yiyan/web.py +++ b/tests/test_yiyan/web.py @@ -72,7 +72,7 @@ def get_resp(self, prompt): data = { "api_ip_port": "http://localhost:3000", - "cookie": '', + "cookie": 'BIDUPSID=A668F884A60F8775B4F5319BB5AD816B; PSTM=1686378956; BAIDUID=8051FCC40FE4D6347C3AABB45F813283:FG=1; BAIDUID_BFESS=8051FCC40FE4D6347C3AABB45F813283:FG=1; ZFY=IlqzqtFg1rRR9ek9oeO1cOPn8S2zytJST79xA9r3EFI:C; __bid_n=188cd9d38714368c1980bd; ET_WHITELIST=etwhitelistintwodays; BA_HECTOR=a1al0100a0200ga0a1ag0gaj1if5e8s1o; H_WISE_SIDS=216853_213352_214792_110085_244720_254831_261710_236312_256419_265881_266360_265615_267074_259031_268478_268593_266187_259642_269401_269778_269832_269904_267066_256739_270460_270535_270516_270547_270922_271036_271020_271170_271174_269771_263618_267659_271321_265034_271272_266028_270102_271560_271726_270443_271869_270157_271674_269858_271812_269878_271954_268758_267804_269665_271255_234296_234207_271187_272223_272284_272364_272008_272458_253022_269729_272741_272822_272841_272802_260335_269297_271285_267596_273061_267560_273090_273161_273118_273136_273240_273301_273400_271158_270055_271146_273671_273704_264170_270186_270142_274080_273932_273965_274141_274177_269610_274207_273917_274233_273786_273043_273598_263750_272805_272319_272560_274425_274422_272332_197096; BDRCVFR[GXdE_1q0qSn]=aeXf-1x8UdYcs; PSINO=5; H_PS_PSSID=; delPer=0; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; Hm_lvt_01e907653ac089993ee83ed00ef9c2f3=1692425469,1692541101,1693483352,1693709742; XFT=u5oY8QNaQLR3vAsRd1CEydt/e9bf8brrvvynvl4G0y4=; XFI=6fe9cfb0-4a05-11ee-9d92-b7e0d2a9a0cb; XFCS=3DF06024E37B44A58BEF2BE93646805739B8C4AD6A31852915AAE1659CA635D7; RT="z=1&dm=baidu.com&si=5bc0fc22-8447-49aa-a3f1-81573f266a04&ss=lm2v1sa2&sl=2&tt=2cl&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf"; Hm_lpvt_01e907653ac089993ee83ed00ef9c2f3=1693709771', "type": 'web' } yiyan = Yiyan(data) diff --git a/ui/main.ui b/ui/main.ui index ce2eeba0..b0260a98 100644 --- a/ui/main.ui +++ b/ui/main.ui @@ -136,9 +136,9 @@ background-color: rgba(255, 255, 255, 50); 0 - 0 + -5167 984 - 9957 + 10033 @@ -1749,6 +1749,18 @@ background-color: rgba(255, 255, 255, 50); + + + + Bard + + + + + + + + diff --git a/utils/gpt_model/bard.py b/utils/gpt_model/bard.py new file mode 100644 index 00000000..1a094df4 --- /dev/null +++ b/utils/gpt_model/bard.py @@ -0,0 +1,44 @@ +from bardapi import Bard +import requests +import logging +import traceback + +from utils.common import Common +from utils.logger import Configure_logger + +class Bard_api(Common): + def __init__(self, data): + self.common = Common() + # 日志文件路径 + file_path = "./log/log-" + self.common.get_bj_time(1) + ".txt" + Configure_logger(file_path) + + """ + 访问 https://bard.google.com/ + F12 打开开发者工具 + 会话:应用程序 → Cookie → 复制 Cookie 中 __Secure-1PSID 对应的值。 + """ + self.token = data["token"] + + self.session = requests.Session() + self.session.headers = { + "Host": "bard.google.com", + "X-Same-Domain": "1", + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36", + "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8", + "Origin": "https://bard.google.com", + "Referer": "https://bard.google.com/", + } + self.session.cookies.set("__Secure-1PSID", self.token) + + + # 调用接口,获取返回内容 + def get_resp(self, prompt): + try: + bard = Bard(token=self.token, session=self.session, timeout=30) + resp_content = bard.get_answer(prompt)['content'].replace("\\n", "") + + return resp_content + except Exception as e: + logging.error(traceback.format_exc()) + return None diff --git a/utils/gpt_model/gpt.py b/utils/gpt_model/gpt.py index d55012de..524772fa 100644 --- a/utils/gpt_model/gpt.py +++ b/utils/gpt_model/gpt.py @@ -17,6 +17,7 @@ from utils.gpt_model.sparkdesk import SPARKDESK from utils.gpt_model.langchain_chatglm import Langchain_ChatGLM from utils.gpt_model.zhipu import Zhipu +from utils.gpt_model.bard import Bard_api class GPT_Model: @@ -30,6 +31,7 @@ class GPT_Model: sparkdesk = None langchain_chatglm = None zhipu = None + bard_api = None def set_model_config(self, model_name, config): if model_name == "openai": @@ -53,6 +55,8 @@ def set_model_config(self, model_name, config): self.langchain_chatglm = Langchain_ChatGLM(config) elif model_name == "zhipu": self.zhipu = Zhipu(config) + elif model_name == "bard": + self.bard_api = Bard_api(config) def get(self, name): logging.info("GPT_MODEL: 进入get方法") @@ -75,6 +79,8 @@ def get(self, name): return self.langchain_chatglm case "zhipu": return self.zhipu + case "bard": + return self.bard_api case _: logging.error(f"{name} 该模型不支持") return diff --git a/utils/my_handle.py b/utils/my_handle.py index edf61d4c..f6a28a18 100644 --- a/utils/my_handle.py +++ b/utils/my_handle.py @@ -75,30 +75,7 @@ def __init__(self, config_path): # 优先本地问答 self.local_qa = My_handle.config.get("local_qa") self.local_qa_audio_list = None - - """ - 新增LLM时,需要追加新的配置,其实这块可以优化掉 - """ - # openai - self.openai_config = My_handle.config.get("openai") - # chatgpt - self.chatgpt_config = My_handle.config.get("chatgpt") - # claude - self.claude_config = My_handle.config.get("claude") - # claude2 - self.claude2_config = My_handle.config.get("claude2") - # chatterbot - self.chatterbot_config = My_handle.config.get("chatterbot") - # chatglm - self.chatglm_config = My_handle.config.get("chatglm") - # chat_with_file - self.chat_with_file_config = My_handle.config.get("chat_with_file") - self.text_generation_webui_config = My_handle.config.get("text_generation_webui") - self.sparkdesk_config = My_handle.config.get("sparkdesk") - self.langchain_chatglm_config = My_handle.config.get("langchain_chatglm") - self.zhipu_config = My_handle.config.get("zhipu") - - + # 音频合成使用技术 My_handle.audio_synthesis_type = My_handle.config.get("audio_synthesis_type") @@ -114,9 +91,9 @@ def __init__(self, config_path): logging.error(traceback.format_exc()) # 设置GPT_Model全局模型列表 - GPT_MODEL.set_model_config("openai", self.openai_config) - GPT_MODEL.set_model_config("chatgpt", self.chatgpt_config) - GPT_MODEL.set_model_config("claude", self.claude_config) + GPT_MODEL.set_model_config("openai", My_handle.config.get("openai")) + GPT_MODEL.set_model_config("chatgpt", My_handle.config.get("chatgpt")) + GPT_MODEL.set_model_config("claude", My_handle.config.get("claude")) """ 新增LLM后,这边先定义下各个变量,下面会用到 @@ -130,6 +107,7 @@ def __init__(self, config_path): self.sparkdesk = None self.langchain_chatglm = None self.zhipu = None + self.bard_api = None # 聊天相关类实例化 @@ -143,7 +121,7 @@ def __init__(self, config_path): if not self.claude.reset_claude(): logging.error("重置Claude会话失败喵~") elif self.chat_type == "claude2": - GPT_MODEL.set_model_config("claude2", self.claude2_config) + GPT_MODEL.set_model_config("claude2", My_handle.config.get("claude2")) self.claude2 = GPT_MODEL.get(self.chat_type) @@ -152,6 +130,9 @@ def __init__(self, config_path): logging.error("重置Claude2会话失败喵~") elif self.chat_type == "chatterbot": from chatterbot import ChatBot # 导入聊天机器人库 + + self.chatterbot_config = My_handle.config.get("chatterbot") + try: self.bot = ChatBot( self.chatterbot_config["name"], # 聊天机器人名字 @@ -161,28 +142,32 @@ def __init__(self, config_path): logging.info(e) exit(0) elif self.chat_type == "chatglm": - GPT_MODEL.set_model_config("chatglm", self.chatglm_config) + GPT_MODEL.set_model_config("chatglm", My_handle.config.get("chatglm")) self.chatglm = GPT_MODEL.get(self.chat_type) elif self.chat_type == "chat_with_file": from utils.chat_with_file.chat_with_file import Chat_with_file - self.chat_with_file = Chat_with_file(self.chat_with_file_config) + self.chat_with_file = Chat_with_file(My_handle.config.get("chat_with_file")) elif self.chat_type == "text_generation_webui": - GPT_MODEL.set_model_config("text_generation_webui", self.text_generation_webui_config) + GPT_MODEL.set_model_config("text_generation_webui", My_handle.config.get("text_generation_webui")) self.text_generation_webui = GPT_MODEL.get(self.chat_type) elif self.chat_type == "sparkdesk": - GPT_MODEL.set_model_config("sparkdesk", self.sparkdesk_config) + GPT_MODEL.set_model_config("sparkdesk", My_handle.config.get("sparkdesk")) self.sparkdesk = GPT_MODEL.get(self.chat_type) elif self.chat_type == "langchain_chatglm": - GPT_MODEL.set_model_config("langchain_chatglm", self.langchain_chatglm_config) + GPT_MODEL.set_model_config("langchain_chatglm", My_handle.config.get("langchain_chatglm")) self.langchain_chatglm = GPT_MODEL.get(self.chat_type) elif self.chat_type == "zhipu": - GPT_MODEL.set_model_config("zhipu", self.zhipu_config) + GPT_MODEL.set_model_config("zhipu", My_handle.config.get("zhipu")) self.zhipu = GPT_MODEL.get(self.chat_type) + elif self.chat_type == "bard": + GPT_MODEL.set_model_config("bard", My_handle.config.get("bard")) + + self.bard_api = GPT_MODEL.get(self.chat_type) elif self.chat_type == "game": # from game.game import Game @@ -262,6 +247,8 @@ def __init__(self, config_path): def get_room_id(self): return self.room_id + + # 从本地问答库中搜索问题的答案 def find_answer(self, question, qa_file_path, similarity=1): """从本地问答库中搜索问题的答案 @@ -292,6 +279,7 @@ def find_answer(self, question, qa_file_path, similarity=1): return None + # 本地问答库 文本模式 根据相似度查找答案 def find_similar_answer(self, input_str, qa_file_path, min_similarity=0.8): """本地问答库 文本模式 根据相似度查找答案 @@ -814,6 +802,9 @@ def llm_handle(self, chat_type, data): elif chat_type == "zhipu": # 生成回复 resp_content = self.zhipu.get_resp(data["content"]) + elif chat_type == "bard": + # 生成回复 + resp_content = self.bard_api.get_resp(data["content"]) elif chat_type == "reread": # 复读机 resp_content = data["content"] @@ -986,6 +977,17 @@ def comment_handle(self, data): else: resp_content = "" logging.warning("警告:智谱AI无返回") + elif self.chat_type == "bard": + data_json["content"] = self.before_prompt + content + self.after_prompt + + # 调用LLM统一接口,获取返回内容 + resp_content = self.llm_handle(self.chat_type, data_json) + if resp_content is not None: + # 输出 返回的回复消息 + logging.info(f"[AI回复{user_name}]:{resp_content}") + else: + resp_content = "" + logging.warning("警告:Bard无返回,请检查配置、网络是否正确,也可能是token过期,需要清空cookie重新登录获取") elif self.chat_type == "game": return g1 = game1()