dream
62 строки · 2.0 Кб
1import copy2import logging3import random4
5from df_engine.core import Actor, Context6
7from common.constants import MUST_CONTINUE8from common.dff.integration.context import (9get_last_human_utterance,10get_shared_memory,11save_to_shared_memory,12set_can_continue,13set_confidence,14)
15from common.fact_random import get_fact16from common.funfact import FUNFACT_LIST, make_question17from common.utils import get_topics18
19logger = logging.getLogger(__name__)20
21CONF_HIGH = 1.022CONF_ZERO = 0.023
24
25def random_funfact_response(ctx: Context, actor: Actor, *args, **kwargs) -> str:26response = ""27set_confidence(ctx, actor, CONF_HIGH)28set_can_continue(ctx, actor, MUST_CONTINUE)29funfact_list = copy.deepcopy(FUNFACT_LIST)30random.shuffle(funfact_list)31shared_memory = get_shared_memory(ctx, actor)32given_funfacts = []33if shared_memory:34given_funfacts = shared_memory.get("given_funfacts", [])35for funfact, topic in funfact_list:36if funfact not in given_funfacts:37given_funfacts.append(funfact)38save_to_shared_memory(ctx, actor, given_funfacts=given_funfacts)39link_question = make_question(topic)40response = f"{funfact} {link_question}"41break42if not response:43set_confidence(ctx, actor, CONF_ZERO)44return response45
46
47def thematic_funfact_response(ctx: Context, actor: Actor, *args, **kwargs) -> str:48response = ""49set_confidence(ctx, actor, CONF_HIGH)50set_can_continue(ctx, actor, MUST_CONTINUE)51entity = ctx.last_request.split("about")52if len(entity) > 1:53entity = entity[1]54human_utter = get_last_human_utterance(ctx, actor)55topic = get_topics(human_utter, which="cobot_topics")[0]56funfact = get_fact(entity, f"fact about {entity}")57if funfact:58link_question = make_question(topic)59response = f"{funfact} {link_question}"60if not response:61set_confidence(ctx, actor, CONF_ZERO)62return response63