Skip to content

Commit

Permalink
Merge pull request #652 from Ikaros-521/owner
Browse files Browse the repository at this point in the history
新增配置项 弹幕模板,可以在发送给LLM前对弹幕内容进行包装,如携带用户名等
  • Loading branch information
Ikaros-521 committed Feb 24, 2024
2 parents d650052 + 9e62213 commit 1872178
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 22 deletions.
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"need_lang": "none",
"before_prompt": "请简要回复:",
"after_prompt": "",
"comment_template": {
"enable": false,
"copywriting": "{username}说:{comment}"
},
"comment_log_type": "回答",
"visual_body": "其他",
"xuniren": {
Expand Down
4 changes: 4 additions & 0 deletions config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"need_lang": "none",
"before_prompt": "请简要回复:",
"after_prompt": "",
"comment_template": {
"enable": false,
"copywriting": "{username}说:{comment}"
},
"comment_log_type": "回答",
"visual_body": "其他",
"xuniren": {
Expand Down
67 changes: 47 additions & 20 deletions utils/my_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,15 @@ def audio_synthesis_handle(self, data_json):
if tmp != None:
logging.info(f'触发本地问答库-文本 [{My_handle.config.get("assistant_anchor", "username")}]: {data_json["content"]}')
# 将问答库中设定的参数替换为指定内容,开发者可以自定义替换内容
if "{cur_time}" in tmp:
tmp = tmp.format(cur_time=My_handle.common.get_bj_time(5))
if "{username}" in tmp:
tmp = tmp.format(username=My_handle.config.get("assistant_anchor", "username"))
else:
tmp = tmp
# 假设有多个未知变量,用户可以在此处定义动态变量
variables = {
'cur_time': My_handle.common.get_bj_time(5),
'username': My_handle.config.get("assistant_anchor", "username")
}

# 使用字典进行字符串替换
if any(var in tmp for var in variables):
tmp = tmp.format(**{var: value for var, value in variables.items() if var in tmp})

logging.info(f"助播 本地问答库-文本回答为: {tmp}")

Expand Down Expand Up @@ -595,12 +598,15 @@ def local_qa_handle(self, data):
if tmp != None:
logging.info(f"触发本地问答库-文本 [{user_name}]: {content}")
# 将问答库中设定的参数替换为指定内容,开发者可以自定义替换内容
if "{cur_time}" in tmp:
tmp = tmp.format(cur_time=My_handle.common.get_bj_time(5))
if "{username}" in tmp:
tmp = tmp.format(username=user_name)
else:
tmp = tmp
# 假设有多个未知变量,用户可以在此处定义动态变量
variables = {
'cur_time': My_handle.common.get_bj_time(5),
'username': My_handle.config.get("assistant_anchor", "username")
}

# 使用字典进行字符串替换
if any(var in tmp for var in variables):
tmp = tmp.format(**{var: value for var, value in variables.items() if var in tmp})

logging.info(f"本地问答库-文本回答为: {tmp}")

Expand Down Expand Up @@ -1541,13 +1547,15 @@ def get_a_copywriting_and_audio_synthesis(key_mapping_config, data):
# 随机获取一个文案
tmp = random.choice(key_mapping_config["copywriting"])

# 变量替换
if "{user_name}" in tmp:
tmp = tmp.format(user_name=data["username"])
if "{gift_name}" in tmp:
# 数据中可能不具备这个变量
if "gift_name" in data:
tmp = tmp.format(gift_name=data["gift_name"])
# 假设有多个未知变量,用户可以在此处定义动态变量
variables = {
'user_name': data["username"],
'gift_name': data["gift_name"] if "gift_name" in data else ""
}

# 使用字典进行字符串替换
if any(var in tmp for var in variables):
tmp = tmp.format(**{var: value for var, value in variables.items() if var in tmp})

# 音频合成时需要用到的重要数据
message = {
Expand Down Expand Up @@ -1810,7 +1818,26 @@ def comment_handle(self, data):
if chat_type in ["chatgpt", "claude", "claude2", "chatglm", "alice", "chat_with_file", "text_generation_webui", \
"sparkdesk", "langchain_chatglm", "langchain_chatchat", "zhipu", "bard", "yiyan", "tongyi", \
"tongyixingchen", "my_qianfan", "my_wenxinworkshop", "gemini", "qanything"]:
data_json["content"] = My_handle.config.get("before_prompt") + content + My_handle.config.get("after_prompt")


data_json["content"] = My_handle.config.get("before_prompt")
# 是否启用弹幕模板
if self.config.get("comment_template", "enable"):
# 假设有多个未知变量,用户可以在此处定义动态变量
variables = {
'username': user_name,
'comment': content
}

comment_template_copywriting = self.config.get("comment_template", "copywriting")
# 使用字典进行字符串替换
if any(var in comment_template_copywriting for var in variables):
content = comment_template_copywriting.format(**{var: value for var, value in variables.items() if var in comment_template_copywriting})

data_json["content"] += content + My_handle.config.get("after_prompt")

logging.info(f"data_json={data_json}")

resp_content = self.llm_handle(chat_type, data_json)
if resp_content is not None:
logging.info(f"[AI回复{user_name}]:{resp_content}")
Expand Down
7 changes: 5 additions & 2 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,8 @@ def common_textarea_handle(content):
config_data["need_lang"] = select_need_lang.value
config_data["before_prompt"] = input_before_prompt.value
config_data["after_prompt"] = input_after_prompt.value
config_data["comment_template"]["enable"] = switch_comment_template_enable.value
config_data["comment_template"]["copywriting"] = input_comment_template_copywriting.value
config_data["audio_synthesis_type"] = select_audio_synthesis_type.value

# 哔哩哔哩
Expand Down Expand Up @@ -1743,9 +1745,10 @@ def common_textarea_handle(content):
).style("width:200px;")

input_before_prompt = ui.input(label='提示词前缀', placeholder='此配置会追加在弹幕前,再发送给LLM处理', value=config.get("before_prompt")).style("width:200px;")

input_after_prompt = ui.input(label='提示词后缀', placeholder='此配置会追加在弹幕后,再发送给LLM处理', value=config.get("after_prompt")).style("width:200px;")

switch_comment_template_enable = ui.switch('启用弹幕模板', value=config.get("comment_template", "enable")).style(switch_internal_css)
input_comment_template_copywriting = ui.input(label='弹幕模板', value=config.get("comment_template", "copywriting"), placeholder='此配置会对弹幕内容进行修改,{}内为变量,会被替换为指定内容,请勿随意删除变量').style("width:200px;")

with ui.card().style(card_css):
ui.label('平台相关')
with ui.card().style(card_css):
Expand Down

0 comments on commit 1872178

Please sign in to comment.