File size: 8,152 Bytes
e2b757a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import json
import os
import random

import openai
from sentence_transformers import util
from transformers import pipeline

openai.api_key = os.getenv("OPENAI_API_KEY")


def hint(secret, n, model, last_pista, lang, Config):
    pista = ""

    # Pistas avanzadas
    if n >= 3:
        j = random.randint(0, 2)
        while j == last_pista:
            j = random.randint(0, 2)
        if j == 0:
            response = openai.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {
                        "role": "user",
                        "content": Config.hint_0_0  # type: ignore
                        + secret
                        + Config.hint_0_1  # type: ignore
                        + secret
                        + Config.hint_0_2,  # type: ignore
                    }
                ],
                temperature=1,
                max_tokens=256,
                top_p=1,
                frequency_penalty=0.5,
                presence_penalty=0,
            )
            output = str(response.choices[0].message.content)
            output = output.replace('"', "").replace("'", "")

            if lang == 0:
                output = ireplace("la " + secret, "La palabra secreta", output)
                output = ireplace("las " + secret, "La palabra secreta", output)
                output = ireplace("el " + secret, "La palabra secreta", output)
                output = ireplace("los " + secret, "La palabra secreta", output)
                output = ireplace("un " + secret, "La palabra secreta", output)
                output = ireplace("una " + secret, "La palabra secreta", output)
                output = ireplace("unos " + secret, "La palabra secreta", output)
                output = ireplace("unas " + secret, "La palabra secreta", output)
            elif lang == 1:
                output = ireplace("the " + secret, "The secret word", output)
                output = ireplace("a " + secret, "The secret word", output)

            pista += Config.hint_0_3 + output  # type: ignore
            last_pista = 0
        elif j == 1:
            response = openai.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {
                        "role": "user",
                        "content": Config.hint_1_0 + secret + Config.hint_1_1,  # type: ignore
                    }
                ],
                temperature=1,
                max_tokens=256,
                top_p=1,
                frequency_penalty=0,
                presence_penalty=0,
            )
            output = str(response.choices[0].message.content)
            pista += Config.hint_1_2 + output  # type: ignore
            last_pista = 1
        elif j == 2:
            response = openai.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {
                        "role": "user",
                        "content": Config.hint_2_0 + secret + Config.hint_2_1,  # type: ignore
                    }
                ],
                temperature=1,
                max_tokens=256,
                top_p=1,
                frequency_penalty=0,
                presence_penalty=0,
            )
            output = str(response.choices[0].message.content)

            pista += Config.hint_2_2 + output  # type: ignore

            last_pista = 2
    # Pistas iniciales
    else:
        j = random.randint(3, 4)
        while j == last_pista:
            j = random.randint(3, 4)
        if j == 3:
            words = []
            response = openai.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {
                        "role": "user",
                        "content": Config.hint_3_0,  # type: ignore
                    }
                ],
                temperature=1.25,
                max_tokens=256,
                top_p=1,
                frequency_penalty=0,
                presence_penalty=0,
            )
            output = str(response.choices[0].message.content)
            output = (output.replace(" ", "").replace(".", "")).lower()
            words.extend(output.strip().split(","))
            response = openai.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {
                        "role": "user",
                        "content": Config.hint_3_1  # type: ignore
                        + secret
                        + Config.hint_3_2,  # type: ignore
                    }
                ],
                temperature=1.1,
                max_tokens=256,
                top_p=1,
                frequency_penalty=0,
                presence_penalty=0,
            )
            output = str(response.choices[0].message.content)
            output = (output.replace(".", "")).lower()
            words.append(output)  # type: ignore
            random.shuffle(words)
            sentences1 = [secret, secret, secret, secret]
            sentences2 = words
            embeddings1 = model.encode(sentences1, convert_to_tensor=True)
            embeddings2 = model.encode(sentences2, convert_to_tensor=True)

            cosine_scores = util.cos_sim(embeddings1, embeddings2)
            scores = cosine_scores[0].tolist()
            sum_scores = sum(scores)
            normalized_scores = [round(score * 100 / sum_scores, 1) for score in scores]

            pista += Config.hint_3_3  # type: ignore

            for i in range(len(words)):
                pista += (
                    words[i]
                    + (" ") * (15 - len(words[i]))
                    + "|"
                    + ("🟩") * round(normalized_scores[i] * 0.5)
                    + " "
                    + str(normalized_scores[i])
                    + "%\n"
                )
            last_pista = 3
        elif j == 4:
            response = openai.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {
                        "role": "user",
                        "content": Config.hint_4_0  # type: ignore
                        + secret
                        + Config.hint_4_1,  # type: ignore
                    }
                ],
                temperature=1,
                max_tokens=256,
                top_p=1,
                frequency_penalty=0,
                presence_penalty=0,
            )
            film_title = str(response.choices[0].message.content).replace('"', "")
            response = openai.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {
                        "role": "user",
                        "content": Config.hint_4_2  # type: ignore
                        + film_title
                        + Config.hint_4_3,  # type: ignore
                    }
                ],
                temperature=1,
                max_tokens=256,
                top_p=1,
                frequency_penalty=0,
                presence_penalty=0,
            )
            output = str(response.choices[0].message.content)
            pista += Config.hint_4_4 + film_title + "\n" + output  # type: ignore
            last_pista = 4

    return pista, n + 1, last_pista


def curiosity(secret, Config):
    response = openai.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {
                "role": "user",
                "content": Config.curiosity + secret + '".',
            }
        ],
        temperature=1,
        max_tokens=256,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0,
    )
    output = str(response.choices[0].message.content)

    return output


def ireplace(old, new, text):
    idx = 0
    while idx < len(text):
        index_l = text.lower().find(old.lower(), idx)
        if index_l == -1:
            return text
        text = text[:index_l] + new + text[index_l + len(old) :]
        idx = index_l + len(new)
    return text