Skip to content

Commit

Permalink
新增通义千问的接入
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikaros-521 committed Sep 23, 2023
1 parent a088df0 commit 986a8c1
Show file tree
Hide file tree
Showing 11 changed files with 364 additions and 85 deletions.
106 changes: 59 additions & 47 deletions UI_main.py

Large diffs are not rendered by default.

21 changes: 13 additions & 8 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"platform": "bilibili",
"platform": "talk",
"room_display_id": "278333",
"chat_type": "reread",
"need_lang": "none",
Expand Down Expand Up @@ -85,13 +85,13 @@
"lowest_price": 1.0
},
"live2d": {
"enable": true,
"enable": false,
"port": 12345
},
"openai": {
"api": "https://api.openai.com/v1",
"api_key": [
"修改为你的api_key"
"替换为你的api-key"
],
"model": "gpt-3.5-turbo"
},
Expand Down Expand Up @@ -171,12 +171,12 @@
"config_path": "E:\\vits-simple-api\\Model\\ikaros\\config.json",
"api_ip_port": "http://127.0.0.1:23456",
"id": "0",
"format": "wav",
"lang": "自动",
"length": "1",
"noise": "0.33",
"noisew": "0.4",
"max": "50",
"format": "wav",
"sdp_radio": "0.2"
},
"vits_fast": {
Expand Down Expand Up @@ -266,6 +266,10 @@
"type": "web",
"cookie": ""
},
"tongyi": {
"type": "web",
"cookie_path": "cookie/tongyi.json"
},
"local_qa": {
"text": {
"enable": true,
Expand Down Expand Up @@ -436,7 +440,7 @@
},
"gift": {
"enable": true,
"get_integral_proportion": 10,
"get_integral_proportion": 10.0,
"copywriting": [
{
"gift_price_interval": "0-1",
Expand Down Expand Up @@ -465,15 +469,16 @@
"enable": true,
"cmd": [
"签到",
"打卡"
"打卡",
"打卡喵"
],
"get_integral": 1,
"copywriting": [
{
"sign_num_interval": "0-3",
"copywriting": [
"{user_name}打卡成功,获得{get_integral}点积分,您已打卡{sign_num}天",
"{user_name}打卡成功喵,获得{get_integral}点积分,感谢您{sign_num}天的陪伴"
"{user_name}打卡成功喵,获得{get_integral}点积分"
]
},
{
Expand All @@ -484,7 +489,7 @@
]
},
{
"sign_num_interval": "31-66666",
"sign_num_interval": "31-666666",
"copywriting": [
"非常感谢{user_name}的陪伴,有您这样的粉丝,主播生涯也是没有遗憾了",
"{user_name}打卡成功喵,获得{get_integral}点积分,感谢您{sign_num}天的陪伴"
Expand Down
21 changes: 13 additions & 8 deletions config.json.bak
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"platform": "bilibili",
"platform": "talk",
"room_display_id": "278333",
"chat_type": "reread",
"need_lang": "none",
Expand Down Expand Up @@ -85,13 +85,13 @@
"lowest_price": 1.0
},
"live2d": {
"enable": true,
"enable": false,
"port": 12345
},
"openai": {
"api": "https://api.openai.com/v1",
"api_key": [
"修改为你的api_key"
"替换为你的api-key"
],
"model": "gpt-3.5-turbo"
},
Expand Down Expand Up @@ -171,12 +171,12 @@
"config_path": "E:\\vits-simple-api\\Model\\ikaros\\config.json",
"api_ip_port": "http://127.0.0.1:23456",
"id": "0",
"format": "wav",
"lang": "自动",
"length": "1",
"noise": "0.33",
"noisew": "0.4",
"max": "50",
"format": "wav",
"sdp_radio": "0.2"
},
"vits_fast": {
Expand Down Expand Up @@ -266,6 +266,10 @@
"type": "web",
"cookie": ""
},
"tongyi": {
"type": "web",
"cookie_path": "cookie/tongyi.json"
},
"local_qa": {
"text": {
"enable": true,
Expand Down Expand Up @@ -436,7 +440,7 @@
},
"gift": {
"enable": true,
"get_integral_proportion": 10,
"get_integral_proportion": 10.0,
"copywriting": [
{
"gift_price_interval": "0-1",
Expand Down Expand Up @@ -465,15 +469,16 @@
"enable": true,
"cmd": [
"签到",
"打卡"
"打卡",
"打卡喵"
],
"get_integral": 1,
"copywriting": [
{
"sign_num_interval": "0-3",
"copywriting": [
"{user_name}打卡成功,获得{get_integral}点积分,您已打卡{sign_num}天",
"{user_name}打卡成功喵,获得{get_integral}点积分,感谢您{sign_num}天的陪伴"
"{user_name}打卡成功喵,获得{get_integral}点积分"
]
},
{
Expand All @@ -484,7 +489,7 @@
]
},
{
"sign_num_interval": "31-66666",
"sign_num_interval": "31-666666",
"copywriting": [
"非常感谢{user_name}的陪伴,有您这样的粉丝,主播生涯也是没有遗憾了",
"{user_name}打卡成功喵,获得{get_integral}点积分,感谢您{sign_num}天的陪伴"
Expand Down
3 changes: 2 additions & 1 deletion cookie/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
此处存放快手的cookie文件,因为监听服务被噶噶检测,所以每次用完后,第二次使用时需要删除json文件,重新创建。
此处存放cookie文件。
快手的cookie文件,因为监听服务被噶噶检测,所以每次用完后,第二次使用时需要删除json文件,重新创建。
85 changes: 69 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,8 @@ def init_config(self):
"langchain_chatglm",
"智谱AI",
"Bard",
"文心一言"
"文心一言",
"通义千问"
])
chat_type_index = 0
if self.chat_type == "none":
Expand Down Expand Up @@ -720,6 +721,8 @@ def init_config(self):
chat_type_index = 12
elif self.chat_type == "yiyan":
chat_type_index = 13
elif self.chat_type == "tongyi":
chat_type_index = 14
self.ui.comboBox_chat_type.setCurrentIndex(chat_type_index)

self.ui.comboBox_need_lang.clear()
Expand Down Expand Up @@ -1931,6 +1934,43 @@ def yiyan_gui_create():

yiyan_gui_create()

# 通义千问
def tongyi_gui_create():
data_json = []
tongyi_config = config.get("tongyi")

tmp_json = {
"label_text": "类型",
"label_tip": "使用的接口类型,暂时只提供web版",
"widget_type": "combo_box",
"combo_data_list": ['web'],
"data": tongyi_config["type"],
"main_obj_name": "tongyi",
"index": 0
}
data_json.append(tmp_json)

tmp_json = {
"label_text": "cookie",
"label_tip": "通义千问登录后,通过浏览器插件Cookie Editor获取Cookie JSON串,然后将数据保存在这个路径的文件中",
"data": tongyi_config["cookie_path"],
"main_obj_name": "tongyi",
"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_tongyi.addWidget(widgets[i], row, 0)
self.ui.gridLayout_tongyi.addWidget(widgets[i + 1], row, 1)
row += 1

tongyi_gui_create()

# 念弹幕
def read_comment_create():
data_json = []
Expand Down Expand Up @@ -2520,6 +2560,8 @@ def common_textEdit_handle(content):
config_data["chat_type"] = "bard"
elif chat_type == "文心一言":
config_data["chat_type"] = "yiyan"
elif chat_type == "通义千问":
config_data["chat_type"] = "tongyi"


config_data["before_prompt"] = self.ui.lineEdit_before_prompt.text()
Expand Down Expand Up @@ -3013,6 +3055,15 @@ def reorganize_copywriting_config_data(copywriting_config_data):
yiyan_data = self.update_data_from_gridLayout(self.ui.gridLayout_yiyan)
config_data["yiyan"] = reorganize_grid_data(yiyan_data, yiyan_keys_mapping)

# 通义千问
tongyi_keys_mapping = {
"type": 0,
"cookie_path": 1
}

tongyi_data = self.update_data_from_gridLayout(self.ui.gridLayout_tongyi)
config_data["tongyi"] = reorganize_grid_data(tongyi_data, tongyi_keys_mapping)

# 念弹幕
read_comment_keys_mapping = {
"enable": 0,
Expand Down Expand Up @@ -3794,23 +3845,24 @@ 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, 0, 0),
1: (0, 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, 0),
3: (0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
4: (0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0),
5: (0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0),
6: (1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0),
7: (0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0),
8: (0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 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visibility_map.get(index, (0, 0, 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 @@ -3826,6 +3878,7 @@ def oncomboBox_chat_type_IndexChanged(self, index):
self.ui.groupBox_zhipu.setVisible(visibility_values[11])
self.ui.groupBox_bard.setVisible(visibility_values[12])
self.ui.groupBox_yiyan.setVisible(visibility_values[13])
self.ui.groupBox_tongyi.setVisible(visibility_values[14])


# 语音合成类型改变 加载显隐不同groupBox
Expand Down
Empty file added tests/test_tongyi/cookies.json
Empty file.
75 changes: 75 additions & 0 deletions tests/test_tongyi/tongyi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import revTongYi
import json, logging


def convert_cookies(cookies: list) -> dict:
"""转换cookies"""
cookies_dict = {}
for cookie in cookies:
cookies_dict[cookie["name"]] = cookie["value"]
return cookies_dict


class TongYi:
def __init__(self, data):
# self.common = Common()
# 日志文件路径
# file_path = "./log/log-" + self.common.get_bj_time(1) + ".txt"
# Configure_logger(file_path)

self.cookie_path = data["cookie_path"]
self.type = data["type"]

self.cookies_dict = {}

with open(self.cookie_path, "r") as f:
self.cookies_dict = convert_cookies(json.load(f))


def get_resp(self, prompt):
"""请求对应接口,获取返回值
Args:
prompt (str): 你的提问
Returns:
str: 返回的文本回答
"""
try:
if self.type == "web":
session = revTongYi.Session(
cookies=self.cookies_dict,
firstQuery=prompt
)

ret = next(
session.ask( # ask方法实际上是一个迭代器,可以提供参数stream=True并换用for的方式迭代
prompt=prompt
) # ask方法接收的详细参数请查看源码
)

return ret["content"][0]
except Exception as e:
logging.error(e)
return None


if __name__ == '__main__':
# 配置日志输出格式
logging.basicConfig(
level=logging.DEBUG, # 设置日志级别,可以根据需求调整
format="%(asctime)s [%(levelname)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)

data = {
"cookie_path": 'cookies.json',
"type": 'web'
}

tongyi = TongYi(data)


logging.info(tongyi.get_resp("你可以扮演猫娘吗,每句话后面加个喵"))
logging.info(tongyi.get_resp("早上好"))

Loading

0 comments on commit 986a8c1

Please sign in to comment.