2023-10-10 20:47:32 +08:00
|
|
|
import py_trees as ptree
|
2023-11-08 15:28:01 +08:00
|
|
|
from robowaiter.behavior_lib._base.Act import Act
|
2023-11-09 16:07:02 +08:00
|
|
|
|
2023-11-19 14:21:58 +08:00
|
|
|
from robowaiter.llm_client.multi_rounds import ask_llm, new_history
|
|
|
|
import random
|
|
|
|
|
|
|
|
import spacy
|
|
|
|
|
|
|
|
nlp = spacy.load('en_core_web_lg')
|
|
|
|
|
2023-11-09 21:52:13 +08:00
|
|
|
|
2023-11-08 15:28:01 +08:00
|
|
|
class DealChat(Act):
|
2023-11-08 10:03:40 +08:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2023-11-15 14:30:57 +08:00
|
|
|
self.chat_history = ""
|
2023-11-18 17:56:48 +08:00
|
|
|
self.function_success = False
|
2023-11-19 14:21:58 +08:00
|
|
|
self.func_map = {"create_sub_task": self.create_sub_task, "get_object_info": self.get_object_info, "find_location": self.find_location}
|
2023-10-17 16:28:36 +08:00
|
|
|
|
2023-10-25 22:12:15 +08:00
|
|
|
def _update(self) -> ptree.common.Status:
|
2023-10-25 10:34:24 +08:00
|
|
|
# if self.scene.status?
|
2023-11-19 14:21:58 +08:00
|
|
|
name, sentence = self.scene.state['chat_list'].pop(0)
|
2023-11-18 12:07:30 +08:00
|
|
|
|
2023-11-18 14:13:07 +08:00
|
|
|
if name == "Goal":
|
2023-11-18 17:56:48 +08:00
|
|
|
self.create_sub_task(goal=sentence)
|
2023-11-16 20:48:01 +08:00
|
|
|
return ptree.common.Status.RUNNING
|
|
|
|
|
2023-11-18 17:56:48 +08:00
|
|
|
if name not in self.scene.state["chat_history"]:
|
|
|
|
self.scene.state["chat_history"][name] = new_history()
|
|
|
|
|
|
|
|
history = self.scene.state["chat_history"][name]
|
2023-11-18 14:51:17 +08:00
|
|
|
self.scene.state["attention"]["customer"] = name
|
2023-11-19 14:21:58 +08:00
|
|
|
self.scene.state["serve_state"] = {"last_chat_time": self.scene.time, }
|
2023-11-16 20:48:01 +08:00
|
|
|
|
2023-11-19 14:21:58 +08:00
|
|
|
function_call, response = ask_llm(sentence, history, func_map=self.func_map)
|
2023-11-15 14:30:57 +08:00
|
|
|
|
2023-11-19 14:21:58 +08:00
|
|
|
self.scene.chat_bubble(response) # 机器人输出对话
|
2023-11-12 14:36:41 +08:00
|
|
|
|
2023-11-18 17:56:48 +08:00
|
|
|
return ptree.common.Status.RUNNING
|
2023-11-09 21:52:13 +08:00
|
|
|
|
2023-11-19 14:21:58 +08:00
|
|
|
def create_sub_task(self, **args):
|
2023-11-18 17:56:48 +08:00
|
|
|
try:
|
|
|
|
goal = args['goal']
|
2023-11-08 17:37:49 +08:00
|
|
|
|
2023-11-18 17:56:48 +08:00
|
|
|
w = goal.split(")")
|
|
|
|
goal_set = set()
|
|
|
|
goal_set.add(w[0] + ")")
|
2023-11-19 14:21:58 +08:00
|
|
|
if len(w) > 1:
|
2023-11-18 17:56:48 +08:00
|
|
|
for x in w[1:]:
|
|
|
|
if x != "":
|
2023-11-18 21:09:14 +08:00
|
|
|
goal_set.add(x[1:] + ")")
|
2023-11-18 17:56:48 +08:00
|
|
|
self.function_success = True
|
|
|
|
except:
|
|
|
|
print("参数解析错误")
|
2023-11-08 17:37:49 +08:00
|
|
|
|
2023-11-18 17:56:48 +08:00
|
|
|
self.scene.robot.expand_sub_task_tree(goal_set)
|
2023-11-18 22:30:14 +08:00
|
|
|
|
2023-11-19 14:21:58 +08:00
|
|
|
def get_object_info(self, **args):
|
2023-11-18 22:30:14 +08:00
|
|
|
try:
|
|
|
|
obj = args['obj']
|
|
|
|
|
|
|
|
self.function_success = True
|
|
|
|
except:
|
|
|
|
obj = None
|
|
|
|
print("参数解析错误")
|
|
|
|
|
|
|
|
near_object = "None"
|
2023-11-19 14:21:58 +08:00
|
|
|
|
|
|
|
# 场景中现有物品
|
|
|
|
cur_things = set()
|
|
|
|
for item in self.status.objects:
|
|
|
|
cur_things.add(item.name)
|
|
|
|
# obj与现有物品进行相似度匹配
|
|
|
|
query_token = nlp(obj)
|
|
|
|
for w in self.all_loc_en:
|
|
|
|
word_token = nlp(w)
|
|
|
|
similarity = query_token.similarity(word_token)
|
|
|
|
if similarity > max_similarity:
|
|
|
|
max_similarity = similarity
|
|
|
|
similar_word = w
|
|
|
|
print("max_similarity:",max_similarity,"similar_word:",similar_word)
|
|
|
|
|
|
|
|
if similar_word: # 存在同义词说明场景中存在该物品
|
|
|
|
near_object = random.choices(list(cur_things), k=5) # 返回场景中的5个物品
|
|
|
|
|
2023-11-18 22:30:14 +08:00
|
|
|
if obj == "洗手间":
|
|
|
|
near_object = "大门"
|
|
|
|
|
2023-11-19 14:21:58 +08:00
|
|
|
return near_object
|
|
|
|
|
|
|
|
def find_location(self, **args):
|
|
|
|
try:
|
|
|
|
location = args['obj']
|
|
|
|
self.function_success = True
|
|
|
|
except:
|
|
|
|
obj = None
|
|
|
|
print("参数解析错误")
|
|
|
|
|
|
|
|
near_location = None
|
|
|
|
# 用户咨询的地点
|
|
|
|
query_token = nlp(location)
|
|
|
|
max_similarity = 0
|
|
|
|
similar_word = None
|
|
|
|
# 到自己维护的地点列表中找同义词
|
|
|
|
for w in self.all_loc_en:
|
|
|
|
word_token = nlp(w)
|
|
|
|
similarity = query_token.similarity(word_token)
|
|
|
|
if similarity > max_similarity:
|
|
|
|
max_similarity = similarity
|
|
|
|
similar_word = w
|
|
|
|
print("similarity:", max_similarity, "similar_word:", similar_word)
|
|
|
|
# 存在同义词说明客户咨询的地点有效
|
|
|
|
if similar_word:
|
|
|
|
mp = list(self.loc_map_en[similar_word])
|
|
|
|
near_location = random.choice(mp)
|
|
|
|
return near_location
|