dream
155 строк · 5.1 Кб
1import requests2from deeppavlov_kg import TerminusdbKnowledgeGraph3
4
5def formulate_utt_annotations(dog_id=None, park_id=None):6utt_annotations = {7"property_extraction": [8{9"triplets": [10{"subject": "user", "relation": "HAVE PET", "object": "dog"},11{"subject": "user", "relation": "LIKE GOTO", "object": "park"},12]13}14],15"custom_entity_linking": [],16}17
18# if dog is in kg add it to custom_el annotations19if dog_id is not None:20utt_annotations["custom_entity_linking"].append(21{22"entity_substr": "dog",23"entity_ids": [dog_id],24"confidences": [1.0],25"tokens_match_conf": [1.0],26"entity_id_tags": ["Animal"],27},28)29if park_id is not None:30utt_annotations["custom_entity_linking"].append(31{32"entity_substr": "park",33"entity_ids": [park_id],34"confidences": [1.0],35"tokens_match_conf": [1.0],36"entity_id_tags": ["Place"],37},38)39
40return utt_annotations41
42
43def prepare_for_comparison(results):44for result in results:45if uttrs := result["added_to_graph"]:46for utt in uttrs:47for triplet in utt:48triplet[2] = triplet[2].split("/")[0]49if uttrs := result["triplets_already_in_graph"]:50for utt in uttrs:51for triplet in utt:52triplet[2] = triplet[2].split("/")[0]53
54return results55
56
57def compare_results(results, golden_results) -> bool:58def compare(uttrs, golden_result):59for idx, utt in enumerate(uttrs):60for triplet in utt:61if triplet not in golden_result[idx]:62return False63return True64
65is_successfull = []66for result, golden_result in zip(results, golden_results):67is_added = compare(result["added_to_graph"], golden_result["added_to_graph"])68is_in_graph = compare(result["triplets_already_in_graph"], golden_result["triplets_already_in_graph"])69is_successfull.append(is_added)70is_successfull.append(is_in_graph)71return all(is_successfull)72
73
74def main():75TERMINUSDB_SERVER_URL = "http://0.0.0.0:6363"76TERMINUSDB_SERVER_TEAM = "admin"77TERMINUSDB_SERVER_DB = "bot_knowledge_db"78TERMINUSDB_SERVER_PASSWORD = "root"79BOT_KNOWLEDGE_MEMORIZER_PORT = 804480
81BOT_KNOWLEDGE_MEMORIZER_URL = f"http://0.0.0.0:{BOT_KNOWLEDGE_MEMORIZER_PORT}/respond"82
83graph = TerminusdbKnowledgeGraph(84db_name=TERMINUSDB_SERVER_DB,85team=TERMINUSDB_SERVER_TEAM,86server=TERMINUSDB_SERVER_URL,87password=TERMINUSDB_SERVER_PASSWORD,88)89
90BOT_ID = "Bot/514b2c3d-bb73-4294-9486-04f9e099835e"91# get dog_id and park_id from KG92dog_id, park_id = None, None93try:94user_props = graph.get_properties_of_entity(BOT_ID)95entities_info = graph.get_properties_of_entities(96[*user_props["HAVE PET/Animal"], *user_props["LIKE GOTO/Place"]]97)98for entity_info in entities_info:99if entity_info.get("substr") == "dog":100dog_id = entity_info["@id"]101elif entity_info.get("substr") == "park":102park_id = entity_info["@id"]103print(f"Found park_id: '{park_id}' and dog_ig: '{dog_id}'")104added_new_entities = False105except Exception:106print("Adding new entities and rels")107added_new_entities = True108
109request_data = [110{111"utterances": [112{113"text": "i have a dog and a cat",114"user": {"id": BOT_ID.split("/")[1]},115"annotations": formulate_utt_annotations(dog_id, park_id),116},117{118"text": "",119"user": {"id": ""},120"annotations": {121"property_extraction": [{}],122"custom_entity_linking": [],123},124},125],126"human_utterances": [127{128"text": "What's your dog's name?",129},130{131"text": "",132},133],134}135]136
137golden_triplets = [[[BOT_ID, "LIKE GOTO", "Place"], [BOT_ID, "HAVE PET", "Animal"]], []]138if added_new_entities:139golden_results = [[{"added_to_graph": golden_triplets, "triplets_already_in_graph": [[], []]}]]140else:141golden_results = [[{"added_to_graph": [[], []], "triplets_already_in_graph": golden_triplets}]]142
143count = 0144for data, golden_result in zip(request_data, golden_results):145result = requests.post(BOT_KNOWLEDGE_MEMORIZER_URL, json=data).json()146print(result)147result = prepare_for_comparison(result)148if compare_results(result, golden_result):149count += 1150assert count == len(request_data)151print("Success")152
153
154if __name__ == "__main__":155main()156