File size: 4,261 Bytes
00cccb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from typing import Optional
from bielik import llm
from state import ChatState, Message
from pydantic_restrictions import Summary, introductionChapter, UnderstandDistortionClassifier, ThoughtChecker
from neo4j_driver import driver

def introduction_talk(chat_history, systemPrompt):
    if chat_history is None:
        chat_history = []
    else:
        chat_history = [msg for msg in chat_history if msg.get("role") != "system"]
    chat_history.insert(0, {"role": "system", "content": systemPrompt})
    talk_llm = llm.with_structured_output(introductionChapter)
    result = talk_llm.invoke(chat_history)
    return result

def check_situation(message):
    classifier_llm = llm.with_structured_output(UnderstandDistortionClassifier)
    result = classifier_llm.invoke([
        {
            "role": "system",
            "content": """Sklasyfikuj input użytkownika:
            - 'understand': Jeśli jego wiadomość świadczy o tym, że rozumie on o czym mówimy i nie ma pytań co do tego
            - 'no understand': Jeśli jego wiadomość świadczy o tym, że nie rozumie on tego zniekształcenia albo chce więcej informacji o nim, bądź prosi o dokładniejsze wytłumaczenie.
            - 'low expression': jeśli jego wiadomość jest mało wylewna i użytkownik nie przedstawił ani chęci działania ani prośby o wytłumaczenie
            """
        },
        {"role": "user", "content": message}
    ])
    return result.message_type

def create_interview(message, old_data):
    summarizer_llm = llm.with_structured_output(Summary)
    result = summarizer_llm.invoke([
        {
            "role": "system",
            "content": f"""
            [ROLA]
            Jesteś modułem SCALAJĄCYM. Twoim jedynym zadaniem jest stworzyć JEDEN krótki opis,
            który łączy wcześniejszy tekst (PREV) z nowym tekstem (NEW).

            [WEJŚCIE]
            PREV: {old_data if old_data else "<puste>"}
            NEW: {message if message else "<puste>"}

            [ZASADY]
            - Jeśli PREV jest puste → zwróć tylko NEW.
            - Jeśli NEW jest puste → zwróć tylko PREV.
            - Jeśli oba są → połącz w logiczną całość.
            - Usuń powtórzenia (także oczywiste parafrazy).
            - ZERO halucynacji: nie dodawaj nic spoza PREV/NEW.
            - Styl: bardzo prosty, neutralny.
            - Długość: maksymalnie 1–2 krótkie zdania.
            - Język: polski.
            - Zwróć wyłącznie JSON ze podanym schematem
            """
        }
    ])
    return result

def getQuestions(intent):
    query = """
        MATCH (q:Question)<-[:HAS_EXAMPLE_QUESTION]-(i:Intent {name:$intencja})
        RETURN q.content AS nazwa ORDER BY nazwa;
            """
    records, _, _ = driver.execute_query(
        query,
        parameters_={"intencja": intent},
        )
    result = []
    for record in records:
        result.append(record["nazwa"])
    return result

def get_last_user_message(state: ChatState) -> Optional[Message]:
    for m in reversed(state.get("messages", [])):
        if m.get("role") == "user":
            return m
    return None

def beliefs_check_function(message):
    beliefs_llm = llm.with_structured_output(ThoughtChecker)
    result = beliefs_llm.invoke([
        {
            "role": "system",
            "content":
        """Twoje zadanie: oceń, czy wypowiedź zawiera MYŚL lub PRZEKONANIE,
        czyli subiektywną interpretację, ocenę lub wniosek o sobie, innych lub świecie.
        Jeśli to jedynie OPIS SYTUACJI lub EMOCJI, zwróć False.
        Definicje:
        - MYŚL/PRZEKONANIE → interpretacja, ocena, wniosek, uogólnienie lub przewidywanie (np. 'Na pewno mnie nie lubią', 'Zawsze wszystko psuję').
        - SYTUACJA → fakt, kontekst, zdarzenie (np. 'Rozmawiałem z szefem', 'Byłem w pracy').
        - EMOCJA → stan uczuciowy, bez oceny poznawczej (np. 'Czuję złość', 'Jest mi smutno', 'Boje się').
        Zasada: Jeśli brak interpretacji, zwróć False, nawet jeśli pojawia się emocja.
        Zwróć wyłącznie JSON zgodny z modelem: {'decision_beliefs': true/false}."""
        },
        {"role": "user", "content": message}
    ])
    return result.decision_beliefs