|
|
|
@ -20,7 +20,7 @@ from py_mini_racer import MiniRacer
|
|
|
|
|
|
|
|
|
|
from protobuf.douyin import *
|
|
|
|
|
from message_processor import *
|
|
|
|
|
from helper import resource_path, PromptQueue
|
|
|
|
|
from helper import resource_path, PromptQueue, MessageType
|
|
|
|
|
from settings import live_talking_host, Backend
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -358,6 +358,13 @@ class DouyinLiveWebReply:
|
|
|
|
|
response = json.loads(response)
|
|
|
|
|
return response['message']['content']
|
|
|
|
|
|
|
|
|
|
def reply_message_postprocess(self, text):
|
|
|
|
|
prohibited_words = self.live_chat_config.prohibited_words
|
|
|
|
|
for prohibited_word, substitutes in prohibited_words.items():
|
|
|
|
|
if prohibited_word in text:
|
|
|
|
|
text = text.replace(prohibited_word, substitutes if substitutes is not None else '')
|
|
|
|
|
return text
|
|
|
|
|
|
|
|
|
|
async def post_to_human(self, text: str):
|
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
|
await client.post(
|
|
|
|
@ -385,15 +392,31 @@ class DouyinLiveWebReply:
|
|
|
|
|
prompt_data = self.queue.get(False)
|
|
|
|
|
if prompt_data is not None:
|
|
|
|
|
# live_chat: 弹幕
|
|
|
|
|
prompt, live_chat = prompt_data
|
|
|
|
|
# logger.info(f'处理提示词: {prompt}')
|
|
|
|
|
message_type, prompt, live_chat = prompt_data
|
|
|
|
|
if message_type == MessageType.ENTER_LIVE_ROOM.value and \
|
|
|
|
|
random.random() >= self.live_chat_config.enter_live_room_prob / 100:
|
|
|
|
|
continue
|
|
|
|
|
elif message_type == MessageType.FOLLOW.value and \
|
|
|
|
|
random.random() >= self.live_chat_config.follow_prob / 100:
|
|
|
|
|
continue
|
|
|
|
|
elif message_type == MessageType.LIKE.value and \
|
|
|
|
|
random.random() >= self.live_chat_config.like_prob / 100:
|
|
|
|
|
continue
|
|
|
|
|
elif message_type == MessageType.GIFT.value and \
|
|
|
|
|
random.random() >= self.live_chat_config.gift_prob / 100:
|
|
|
|
|
continue
|
|
|
|
|
elif message_type == MessageType.CHAT.value and \
|
|
|
|
|
random.random() >= self.live_chat_config.chat_prob / 100:
|
|
|
|
|
continue
|
|
|
|
|
if live_chat is not None:
|
|
|
|
|
logger.info(f'弹幕: {live_chat}')
|
|
|
|
|
llm_output = self._llm(self.live_chat_config.product_related_prompt.format(content=live_chat))
|
|
|
|
|
logger.info(f'判断弹幕是否和商品有关: {llm_output}')
|
|
|
|
|
if llm_output != '是':
|
|
|
|
|
logger.info(f'判断弹幕是否违反中国大陆法律和政策: {llm_output}')
|
|
|
|
|
if llm_output != '否':
|
|
|
|
|
continue
|
|
|
|
|
reply_messages = self._llm(prompt, True)
|
|
|
|
|
for reply_message in reply_messages:
|
|
|
|
|
reply_message = self.reply_message_postprocess(reply_message)
|
|
|
|
|
asyncio.run(self.post_to_human(reply_message))
|
|
|
|
|
logger.info(f'输出回复: {reply_message}')
|
|
|
|
|
# is_speaking此时是False,需要等一段时间再查询
|
|
|
|
@ -403,8 +426,12 @@ class DouyinLiveWebReply:
|
|
|
|
|
# time.sleep(1)
|
|
|
|
|
system_messages = self.live_chat_config.system_messages
|
|
|
|
|
reply_message = system_messages[random.randint(0, len(system_messages) - 1)]
|
|
|
|
|
asyncio.run(self.post_to_human(reply_message))
|
|
|
|
|
logger.info(f'输出系统文案: {reply_message}')
|
|
|
|
|
llm_prompt = self.live_chat_config.refine_system_message_prompt.format(content=reply_message)
|
|
|
|
|
reply_messages = self._llm(llm_prompt, True)
|
|
|
|
|
for reply_message in reply_messages:
|
|
|
|
|
reply_message = self.reply_message_postprocess(reply_message)
|
|
|
|
|
asyncio.run(self.post_to_human(reply_message))
|
|
|
|
|
logger.info(f'输出系统文案: {reply_message}')
|
|
|
|
|
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
except Exception:
|
|
|
|
@ -413,4 +440,8 @@ class DouyinLiveWebReply:
|
|
|
|
|
time.sleep(5)
|
|
|
|
|
system_messages = self.live_chat_config.system_messages
|
|
|
|
|
reply_message = system_messages[random.randint(0, len(system_messages) - 1)]
|
|
|
|
|
asyncio.run(self.post_to_human(reply_message))
|
|
|
|
|
llm_prompt = self.live_chat_config.refine_system_message_prompt.format(content=reply_message)
|
|
|
|
|
reply_messages = self._llm(llm_prompt, True)
|
|
|
|
|
for reply_message in reply_messages:
|
|
|
|
|
reply_message = self.reply_message_postprocess(reply_message)
|
|
|
|
|
asyncio.run(self.post_to_human(reply_message))
|
|
|
|
|