Skip to content

Commit

Permalink
Merge pull request #282 from Ikaros-521/owner
Browse files Browse the repository at this point in the history
新增 谷歌bard的接入,支持上下文记忆。
  • Loading branch information
Ikaros-521 authored Sep 3, 2023
2 parents b317b7c + b24b51c commit ba657d9
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 54 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -1579,6 +1585,9 @@ cmd运行`npm i docsify-cli -g`
- 更新b站监听库到16.0.0版本,新增b站登录板块,获取cookie等信息解决弹幕监听丢失问题(强烈建议使用小号登录,有风险)
- 修复GUI b站部分配置项描述错误

- 2023-09-03
- 新增 谷歌bard的接入,支持上下文记忆。

</details>


Expand Down
11 changes: 10 additions & 1 deletion UI_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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", "数据库路径"))
Expand Down
3 changes: 3 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@
"instruction_template": "Vicuna-v1.1",
"your_name": ""
},
"bard": {
"token": ""
},
"local_qa": {
"text": {
"enable": true,
Expand Down
3 changes: 3 additions & 0 deletions config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@
"instruction_template": "Vicuna-v1.1",
"your_name": "你"
},
"bard": {
"token": ""
},
"local_qa": {
"text": {
"enable": true,
Expand Down
Binary file modified docs/AI Vtuber.xmind
Binary file not shown.
Binary file modified docs/xmind.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 64 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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()
Expand Down Expand Up @@ -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()

"""
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

"""
↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
-------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -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])
Expand All @@ -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
Expand Down Expand Up @@ -3651,4 +3699,5 @@ def emit(self, record):

e = AI_VTB()

sys.exit(e.app.exec())
sys.exit(e.app.exec())

20 changes: 20 additions & 0 deletions tests/test_bard/qa.py
Original file line number Diff line number Diff line change
@@ -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']
26 changes: 26 additions & 0 deletions tests/test_bard/qa2.py
Original file line number Diff line number Diff line change
@@ -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'])
15 changes: 15 additions & 0 deletions tests/test_bard/tts.py
Original file line number Diff line number Diff line change
@@ -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']))
2 changes: 1 addition & 1 deletion tests/test_yiyan/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 14 additions & 2 deletions ui/main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ background-color: rgba(255, 255, 255, 50);
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<y>-5167</y>
<width>984</width>
<height>9957</height>
<height>10033</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -1749,6 +1749,18 @@ background-color: rgba(255, 255, 255, 50);
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_bard">
<property name="title">
<string>Bard</string>
</property>
<layout class="QGridLayout" name="gridLayout_74">
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout_bard"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox_chatterbot">
<property name="title">
Expand Down
44 changes: 44 additions & 0 deletions utils/gpt_model/bard.py
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit ba657d9

Please sign in to comment.