dream

Форк
0
62 строки · 2.0 Кб
1
import copy
2
import logging
3
import random
4

5
from df_engine.core import Actor, Context
6

7
from common.constants import MUST_CONTINUE
8
from common.dff.integration.context import (
9
    get_last_human_utterance,
10
    get_shared_memory,
11
    save_to_shared_memory,
12
    set_can_continue,
13
    set_confidence,
14
)
15
from common.fact_random import get_fact
16
from common.funfact import FUNFACT_LIST, make_question
17
from common.utils import get_topics
18

19
logger = logging.getLogger(__name__)
20

21
CONF_HIGH = 1.0
22
CONF_ZERO = 0.0
23

24

25
def random_funfact_response(ctx: Context, actor: Actor, *args, **kwargs) -> str:
26
    response = ""
27
    set_confidence(ctx, actor, CONF_HIGH)
28
    set_can_continue(ctx, actor, MUST_CONTINUE)
29
    funfact_list = copy.deepcopy(FUNFACT_LIST)
30
    random.shuffle(funfact_list)
31
    shared_memory = get_shared_memory(ctx, actor)
32
    given_funfacts = []
33
    if shared_memory:
34
        given_funfacts = shared_memory.get("given_funfacts", [])
35
    for funfact, topic in funfact_list:
36
        if funfact not in given_funfacts:
37
            given_funfacts.append(funfact)
38
            save_to_shared_memory(ctx, actor, given_funfacts=given_funfacts)
39
            link_question = make_question(topic)
40
            response = f"{funfact} {link_question}"
41
            break
42
    if not response:
43
        set_confidence(ctx, actor, CONF_ZERO)
44
    return response
45

46

47
def thematic_funfact_response(ctx: Context, actor: Actor, *args, **kwargs) -> str:
48
    response = ""
49
    set_confidence(ctx, actor, CONF_HIGH)
50
    set_can_continue(ctx, actor, MUST_CONTINUE)
51
    entity = ctx.last_request.split("about")
52
    if len(entity) > 1:
53
        entity = entity[1]
54
        human_utter = get_last_human_utterance(ctx, actor)
55
        topic = get_topics(human_utter, which="cobot_topics")[0]
56
        funfact = get_fact(entity, f"fact about {entity}")
57
        if funfact:
58
            link_question = make_question(topic)
59
            response = f"{funfact} {link_question}"
60
    if not response:
61
        set_confidence(ctx, actor, CONF_ZERO)
62
    return response
63

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.