File size: 4,847 Bytes
1c3ae40
 
4fe3d9c
b0e72b5
 
 
 
c7a90d5
b0e72b5
42d1f1b
 
1c3ae40
e7ea6a7
b0e72b5
c7a90d5
1c3ae40
c7a90d5
b0e72b5
c7a90d5
 
 
 
1c3ae40
 
 
b0e72b5
1c3ae40
b0e72b5
 
 
1c3ae40
c7a90d5
 
 
 
b0e72b5
 
 
 
 
 
 
 
 
 
 
 
 
4fe3d9c
 
a054b11
 
1c3ae40
a054b11
 
 
 
 
 
1c3ae40
 
 
a054b11
1c3ae40
a054b11
 
 
1c3ae40
a054b11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4fe3d9c
 
 
bd049e0
4fe3d9c
 
 
 
 
c7a90d5
4fe3d9c
 
 
 
 
c7a90d5
e7ea6a7
c7a90d5
 
e7ea6a7
c7a90d5
 
 
 
 
 
 
 
a054b11
 
 
4975afb
 
 
29acba1
a054b11
29acba1
fddb083
29acba1
 
 
5a8160a
29acba1
 
a054b11
29acba1
 
 
a054b11
29acba1
 
a054b11
29acba1
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
from openai import OpenAI
import decider_questions

YES = True
NO = False

g_decider_utils_dbg_printing = False

openai_client = OpenAI(organization=os.environ.get("OPENAI_ORGANIZATION"),
    api_key=os.environ.get("OPENAI_KEY"))


def yesno(question, text, default):
    global g_decider_utils_dbg_printing
    global openai_client

    prompt = text + "\n\n" + question

    if g_decider_utils_dbg_printing:
        print(prompt)

    hopefully_word_yes_or_no = openai_client.chat.completions.create(
        model="gpt-4-turbo",
        messages=[{"role": "system", "content": prompt}],
        temperature=0,
        max_tokens=20,  # A while ago when testing with gpt-4-turbo, at first I tried max_tokens = 1 or 2,  but the davinci-002 model produced zero output (immediate stop) unless I increased max_token to around 20
        frequency_penalty=0,
        presence_penalty=0,
        n=1,
    ).choices[0].message.content

    if g_decider_utils_dbg_printing:
        print(hopefully_word_yes_or_no)

    hopefully_word_yes_or_no = hopefully_word_yes_or_no.upper().strip()

    result = default

    if default == YES:
        if hopefully_word_yes_or_no.startswith("N"):
            result = NO

    if default == NO:
        if hopefully_word_yes_or_no.startswith("Y"):
            result = YES

    return result


def number(question, text, default=-1, maximum=6):
    global g_decider_utils_dbg_printing
    global openai_client

    prompt = text + "\n\n" + question

    if g_decider_utils_dbg_printing:
        print(prompt)

    hopefully_number = openai_client.chat.completions.create(
        model="gpt-4-turbo",
        messages=[{"role": "system", "content": prompt}],
        temperature=0,
        max_tokens=20,  # A while ago when testing with gpt-4-turbo, at first I tried max_tokens = 1 or 2,  but the davinci-002 model produced zero output (immediate stop) unless I increased max_token to around 20
        frequency_penalty=0,
        presence_penalty=0,
        n=1,
    ).choices[0].message.content

    if g_decider_utils_dbg_printing:
        print(hopefully_number)

    hopefully_number = hopefully_number.upper().strip().split(" ")[0].strip(".")

    if g_decider_utils_dbg_printing:
        print(hopefully_number)

    if hopefully_number == "ONE":
        hopefully_number = "1"

    if hopefully_number == "TWO":
        hopefully_number = "2"

    if hopefully_number == "THREE":
        hopefully_number = "3"

    if hopefully_number == "FOUR":
        hopefully_number = "4"

    if hopefully_number == "FIVE":
        hopefully_number = "5"

    if hopefully_number == "SIX":
        hopefully_number = "6"

    result = default

    if hopefully_number.startswith("ALL"):
        result = maximum
    else:

        try:
            if hopefully_number.isnumeric():
                result = int(hopefully_number)
        except:
            pass

    return result


# In certain cases, I need more special-case logic in order to behave correctly,
# which we verify using the unit tests in run_unit_tests.py:


def special_case_is_running_away(text):
    might_really_be_fleeing = False
    for keyword in ["run", "away", "hide", "escape", "flee", "sprint", "teleport"]:
        if keyword in text.lower():
            might_really_be_fleeing = True
            break

    if might_really_be_fleeing:
        return yesno(decider_questions.QUESTION_IS_ACTION_RUNNING_AWAY, text, default=NO)
    else:
        return NO


def special_case_is_magic(text):
    is_magic = False
    for keyword in ["magic", "spell", "fly", "invisib", "levitat", "shapeshift", "morph", "shrink", "transform", "teleport", "dragon", "genie", "fairy", "demon", "devil", "angel", "griffin", "wand"]:
        if keyword in text.lower():
            is_magic = True
            break

    if is_magic:
        return YES
    else:
        return yesno(decider_questions.QUESTION_IS_ACTION_MAGIC, text, default=NO)


def special_case_is_action_lethal(text):
    if len(text.strip()) <= 3:
        return NO

    text_l = text.lower()

    is_negotiation = False
    for keyword in ["say", "ask", "negotiat", "warn", "fair", "consider", "please", "family", "children", "challenge", "request", "inquire", "price", "pay", "gold", "coin", "away", "flee", "don't shoot", "dont shoot"]:
        if keyword in text_l:
            is_negotiation = True
            break

    if is_negotiation:
        return NO

    aiming_but_not_shooting = False
    if ("aim" in text_l or "cock" in text_l or "pull" in text_l or "hammer" in text_l or "point" in text_l) and not ("shoot" in text_l or "fire" in text_l or "trigger" in text_l):
        aiming_but_not_shooting = True

    if aiming_but_not_shooting:
        return NO

    bool1 = yesno(decider_questions.QUESTION_IS_ACTION_LIKELY_LETHAL, text, default=NO)
    return bool1