Add Google Bard
This commit is contained in:
33
base/func_bard.py
Normal file
33
base/func_bard.py
Normal file
@@ -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)
|
||||
@@ -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
|
||||
|
||||
@@ -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", {})
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -13,3 +13,4 @@ pillow
|
||||
jupyter_client
|
||||
zhdate
|
||||
ipykernel
|
||||
bardapi
|
||||
|
||||
4
robot.py
4
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
|
||||
|
||||
Reference in New Issue
Block a user