4cfad5ae0f
- 全新ui - 全面优化websocket逻辑,提高数字人和ui连接的稳定性及资源开销 - 全面优化唤醒逻辑,提供稳定的普通唤醒模式和前置词唤醒模式 - 优化拾音质量,支持多声道麦克风拾音 - 优化自动播放服务器的对接机制,提供稳定和兼容旧版ue工程的对接模式 - 数字人接口输出机器人表情,以适应新fay ui及单片机的数字人表情输出 - 使用更高级的音频时长计算方式,可以更精准控制音频播放完成后的逻辑 - 修复点击关闭按钮会导致程序退出的bug - 修复没有麦克风的设备开启麦克风会出错的问题 - 为服务器主机地址提供配置项,以方便服务器部署
196 lines
6.7 KiB
Python
196 lines
6.7 KiB
Python
from rasa_sdk import Action,Tracker
|
|
from rasa_sdk.executor import CollectingDispatcher
|
|
from datetime import datetime
|
|
from dateutil.parser import parse
|
|
from dateutil.relativedelta import relativedelta
|
|
import json
|
|
import requests
|
|
from rasa_sdk.forms import FormValidationAction
|
|
|
|
# from utils.openai_api.openai_api_request import chat
|
|
|
|
class ValidateScheduleReminderForm(FormValidationAction):
|
|
def name(self) :
|
|
return "validate_schedule_reminder_form"
|
|
|
|
async def validate_date(self, value, dispatcher: CollectingDispatcher, tracker: Tracker, domain) :
|
|
# 在这里添加您的日期验证逻辑
|
|
return {"date": value}
|
|
|
|
async def validate_time(self, value, dispatcher: CollectingDispatcher, tracker: Tracker, domain):
|
|
# 在这里添加您的时间验证逻辑
|
|
return {"time": value}
|
|
|
|
async def validate_event(self, value, dispatcher: CollectingDispatcher, tracker: Tracker, domain):
|
|
# 在这里添加您的事件验证逻辑
|
|
return {"event": value}
|
|
|
|
|
|
class ActionGPTResponse(Action):
|
|
def name(self) -> str:
|
|
return "action_gpt_response"
|
|
|
|
async def run(self, dispatcher: CollectingDispatcher, tracker, domain):
|
|
history = []
|
|
user_messages = []
|
|
bot_messages = []
|
|
|
|
# Separate user messages and bot messages
|
|
for event in tracker.events:
|
|
if event.get("text") == "":
|
|
continue
|
|
answer_info = dict()
|
|
if event.get("event") == "user":
|
|
user_messages.append(event.get("text"))
|
|
answer_info["role"] = "user"
|
|
answer_info["content"] = event.get("text")
|
|
history.append(answer_info)
|
|
elif event.get("event") == "bot":
|
|
bot_messages.append(event.get("text"))
|
|
answer_info["role"] = "assistant"
|
|
answer_info["content"] = event.get("text")
|
|
history.append(answer_info)
|
|
|
|
# Combine user and bot messages
|
|
# for user, bot in zip(user_messages, bot_messages):
|
|
# history.append([user, bot])
|
|
|
|
print("*******************************")
|
|
print(history)
|
|
print("*******************************")
|
|
|
|
current_chat={'role': 'user', 'content': tracker.latest_message.get("text")}
|
|
history.append(current_chat)
|
|
|
|
url = "http://127.0.0.1:8101/v1/chat/completions"
|
|
req = json.dumps({
|
|
"model": "THUDM/chatglm3-6b",
|
|
"messages": history,
|
|
"max_tokens": 768,
|
|
"temperature": 0})
|
|
print(req)
|
|
headers = {'content-type': 'application/json'}
|
|
r = requests.post(url, headers=headers, json={
|
|
"model": "THUDM/chatglm3-6b",
|
|
"messages": history,
|
|
"max_tokens": 768,
|
|
"temperature": 0})
|
|
|
|
a = json.loads(r.text)['choices'][0]['message']['content']
|
|
# history = json.loads(r.text).get('history')
|
|
|
|
dispatcher.utter_message(a)
|
|
|
|
return []
|
|
|
|
|
|
class ActionAskDate(Action):
|
|
def name(self):
|
|
return "action_ask_date"
|
|
|
|
async def run(self, dispatcher, tracker, domain):
|
|
# 获取当前日期
|
|
today = datetime.now().date()
|
|
# 获取星期几的信息
|
|
week_day = today.strftime("%A")
|
|
# 将星期几的英文名称转换为中文
|
|
week_day_zh = {
|
|
"Monday": "星期一",
|
|
"Tuesday": "星期二",
|
|
"Wednesday": "星期三",
|
|
"Thursday": "星期四",
|
|
"Friday": "星期五",
|
|
"Saturday": "星期六",
|
|
"Sunday": "星期日",
|
|
}.get(week_day, "未知")
|
|
# 将日期格式化为字符串
|
|
date_str = today.strftime("%Y年%m月%d日")
|
|
# 将日期和星期信息发送给用户
|
|
dispatcher.utter_message(text=f"今天是 {date_str} {week_day_zh}。")
|
|
|
|
|
|
class ActionAskTime(Action):
|
|
def name(self):
|
|
return "action_ask_time"
|
|
|
|
async def run(self, dispatcher, tracker, domain):
|
|
# 获取当前时间
|
|
now = datetime.now()
|
|
# 将时间格式化为字符串
|
|
time_str = now.strftime("%H:%M")
|
|
# 将时间信息发送给用户
|
|
dispatcher.utter_message(text=f"现在是 {time_str}。")
|
|
|
|
def parse_datetime(datetime_str):
|
|
try:
|
|
datetime_obj = parse(datetime_str, fuzzy=True)
|
|
if "周" in datetime_str: # 处理相对日期,如 "周五"
|
|
today = datetime.now().date()
|
|
weekday_diff = (datetime_obj.weekday() - today.weekday()) % 7
|
|
datetime_obj = today + relativedelta(days=weekday_diff)
|
|
return datetime_obj
|
|
except ValueError:
|
|
print("无法解析日期和时间")
|
|
|
|
class ActionAskProblem(Action):
|
|
def name(self) -> str:
|
|
return "action_ask_problem"
|
|
|
|
async def run(self, dispatcher: CollectingDispatcher, tracker, domain):
|
|
history = []
|
|
user_messages = []
|
|
bot_messages = []
|
|
|
|
# Separate user messages and bot messages
|
|
for event in tracker.events:
|
|
if event.get("text") == "":
|
|
continue
|
|
answer_info = dict()
|
|
if event.get("event") == "user":
|
|
user_messages.append(event.get("text"))
|
|
answer_info["role"] = "user"
|
|
answer_info["content"] = event.get("text")
|
|
history.append(answer_info)
|
|
elif event.get("event") == "bot":
|
|
bot_messages.append(event.get("text"))
|
|
answer_info["role"] = "assistant"
|
|
answer_info["content"] = event.get("text")
|
|
history.append(answer_info)
|
|
|
|
# Combine user and bot messages
|
|
# for user, bot in zip(user_messages, bot_messages):
|
|
# history.append([user, bot])
|
|
|
|
print("*******************************")
|
|
print(history)
|
|
print("*******************************")
|
|
|
|
current_chat={'role': 'user', 'content': tracker.latest_message.get("text")}
|
|
history.append(current_chat)
|
|
|
|
url = "http://127.0.0.1:8101/v1/chat/completions"
|
|
req = json.dumps({
|
|
"model": "THUDM/chatglm3-6b",
|
|
"messages": history,
|
|
"max_tokens": 768,
|
|
"temperature": 0})
|
|
|
|
print(req)
|
|
|
|
headers = {'content-type': 'application/json'}
|
|
r = requests.post(url, headers=headers, json={
|
|
"model": "THUDM/chatglm3-6b",
|
|
"messages": history,
|
|
"max_tokens": 768,
|
|
"temperature": 0})
|
|
# a = json.loads(r.text).get('response')
|
|
#如果是vll推理则用
|
|
a = json.loads(r.text)['choices'][0]['message']['content']
|
|
# history = json.loads(r.text).get('history')
|
|
|
|
|
|
|
|
dispatcher.utter_message(a)
|
|
|
|
return []
|