From c653cda0ff915c0461cd4907deedfcdf8b49cded Mon Sep 17 00:00:00 2001 From: aki66938 <271443201@qq.com> Date: Thu, 14 Dec 2023 09:46:04 +0800 Subject: [PATCH] Add Google Bard --- base/func_bard.py | 33 +++++++++++++++++++++++++++++++++ config.yaml.template | 8 ++++++++ configuration.py | 1 + constants.py | 3 ++- requirements.txt | 1 + robot.py | 4 ++++ 6 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 base/func_bard.py diff --git a/base/func_bard.py b/base/func_bard.py new file mode 100644 index 0000000..5d0a32c --- /dev/null +++ b/base/func_bard.py @@ -0,0 +1,33 @@ +#! /usr/bin/env python3 +# -*- coding: utf-8 -*- +import bardapi +import requests + +class BardAssistant: + def __init__(self, conf: dict) -> None: + self._token = conf["token"] + self._bard = bardapi.core.Bard(self._token) + + + def __repr__(self): + return 'BardAssistant' + + @staticmethod + def value_check(conf: dict) -> bool: + if conf: + return all(conf.values()) + return False + + def get_answer(self, msg: str, sender: str = None) -> str: + response = self._bard.get_answer(msg) + return response['content'] + +if __name__ == "__main__": + from configuration import Config + config = Config().BardAssistant + if not config: + exit(0) + + bard_assistant = BardAssistant(config) + rsp = BardAssistant.get_answer("who are you?") + print(rsp) \ No newline at end of file diff --git a/config.yaml.template b/config.yaml.template index 7969f18..897ae2c 100644 --- a/config.yaml.template +++ b/config.yaml.template @@ -69,3 +69,11 @@ xinghuo_web: # -----讯飞星火web模式api配置这行不填 抓取方式详 fd: # fd GtToken: # GtToken prompt: 你是智能聊天机器人,你叫wcferry。请用这个角色回答我的问题 # 根据需要对角色进行设定 + +bard: # -----bard配置这行不填----- + token: # token 长这样 eQhXAaCa3c3lUVXl-EvA210yikeNMsXHtDAX7doVyeFtW6ZbS-ENTSZg4-_713z1rWSXDQ. + # 你的bard的token,抓取方式见https://github.com/dsdanielpark/Bard-API,支持gemini(账号设置为美国),cooike值为__Secure-1PSID + # 特殊注意事项: + # 1、只需要登录gmail抓取账号的__Secure-1PSID即可 + # 2、如果网页登录了bard,已配置的机器人token会失效,需要清除浏览器上的google账号的cookie录后,重新抓取cooike值 + # 3、新建的账号可能无法登录bard,需要提前确认是否可以使用bard diff --git a/configuration.py b/configuration.py index e50a2ea..0aa5b45 100644 --- a/configuration.py +++ b/configuration.py @@ -35,3 +35,4 @@ class Config(object): self.TIGERBOT = yconfig.get("tigerbot", {}) self.XINGHUO_WEB = yconfig.get("xinghuo_web", {}) self.CHATGLM = yconfig.get("chatglm", {}) + self.BardAssistant = yconfig.get("bard", {}) diff --git a/constants.py b/constants.py index 2c76914..3c64090 100644 --- a/constants.py +++ b/constants.py @@ -8,11 +8,12 @@ class ChatType(IntEnum): CHATGPT = 2 # ChatGPT XINGHUO_WEB = 3 # 讯飞星火 CHATGLM = 4 # ChatGLM + BardAssistant = 5 # Google Bard @staticmethod def is_in_chat_types(chat_type: int) -> bool: if chat_type in [ChatType.TIGER_BOT.value, ChatType.CHATGPT.value, - ChatType.XINGHUO_WEB.value, ChatType.CHATGLM.value]: + ChatType.XINGHUO_WEB.value, ChatType.CHATGLM.value,ChatType.BardAssistant.value]: return True return False diff --git a/requirements.txt b/requirements.txt index bf63eb9..50b8438 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,3 +13,4 @@ pillow jupyter_client zhdate ipykernel +bardapi diff --git a/robot.py b/robot.py index bb23376..10b0338 100644 --- a/robot.py +++ b/robot.py @@ -41,6 +41,8 @@ class Robot(Job): self.chat = XinghuoWeb(self.config.XINGHUO_WEB) elif chat_type == ChatType.CHATGLM.value and ChatGLM.value_check(self.config.CHATGLM): self.chat = ChatGLM(self.config.CHATGLM) + elif chat_type == ChatType.BardAssistant.value and BardAssistant.value_check(self.config.BardAssistant): + self.chat = BardAssistant(self.config.BardAssistant) else: self.LOG.warning("未配置模型") self.chat = None @@ -53,6 +55,8 @@ class Robot(Job): self.chat = XinghuoWeb(self.config.XINGHUO_WEB) elif ChatGLM.value_check(self.config.CHATGLM): self.chat = ChatGLM(self.config.CHATGLM) + elif BardAssistant.value_check(self.config.BardAssistant): + self.chat = BardAssistant(self.config.BardAssistant) else: self.LOG.warning("未配置模型") self.chat = None