File size: 11,036 Bytes
b599481
 
1703ab1
b599481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1703ab1
b599481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1703ab1
 
 
 
b599481
 
 
 
 
 
 
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import json
import os
from copy import deepcopy
from typing import Any, Dict, List, Tuple, Union

import numpy as np
import openai
import tiktoken
from accelerate.utils import set_seed
from loguru import logger
from openai.types import CreateEmbeddingResponse
from sklearn.metrics.pairwise import cosine_similarity
from tenacity import Retrying, _utils, retry_if_not_exception_type
from tenacity.stop import stop_base
from tenacity.wait import wait_base
from tqdm import tqdm


def my_before_sleep(retry_state):
    logger.debug(
        f"Retrying: attempt {retry_state.attempt_number} ended with: "
        f"{retry_state.outcome}, spend {retry_state.seconds_since_start} in "
        "total"
    )


class my_wait_exponential(wait_base):
    def __init__(
        self,
        multiplier: Union[int, float] = 1,
        max: _utils.time_unit_type = _utils.MAX_WAIT,  # noqa
        exp_base: Union[int, float] = 2,
        min: _utils.time_unit_type = 0,  # noqa
    ) -> None:
        self.multiplier = multiplier
        self.min = _utils.to_seconds(min)
        self.max = _utils.to_seconds(max)
        self.exp_base = exp_base

    def __call__(self, retry_state: "RetryCallState") -> float:
        if retry_state.outcome == openai.Timeout:
            return 0

        try:
            exp = self.exp_base ** (retry_state.attempt_number - 1)
            result = self.multiplier * exp
        except OverflowError:
            return self.max
        return max(max(0, self.min), min(result, self.max))


class my_stop_after_attempt(stop_base):
    """Stop when the previous attempt >= max_attempt."""

    def __init__(self, max_attempt_number: int) -> None:
        self.max_attempt_number = max_attempt_number

    def __call__(self, retry_state: "RetryCallState") -> bool:
        if retry_state.outcome == openai.Timeout:
            retry_state.attempt_number -= 1
        return retry_state.attempt_number >= self.max_attempt_number


def annotate(conv_str: str) -> CreateEmbeddingResponse:
    """Creates embeddings for the given conversation string."""
    request_timeout = 6.0
    for attempt in Retrying(
        reraise=True,
        retry=retry_if_not_exception_type(
            (
                openai.BadRequestError,
                openai.AuthenticationError,
            )
        ),
        wait=my_wait_exponential(min=1, max=60),
        stop=(my_stop_after_attempt(8)),
        before_sleep=my_before_sleep,
    ):
        with attempt:
            response = openai.embeddings.create(
                model="text-embedding-ada-002",
                input=conv_str,
                timeout=request_timeout,
            )
        request_timeout = min(30, request_timeout * 2)

    return response


def annotate_chat(messages, logit_bias=None) -> str:
    """Generates a response given a conversation context.

    Args:
        messages: Conversation context (previous utterances).
        logit_bias: Logit bias for the model.

    Returns:
        Generated response.
    """

    request_timeout = 20.0
    for attempt in Retrying(
        reraise=True,
        retry=retry_if_not_exception_type(
            (
                openai.BadRequestError,
                openai.AuthenticationError,
            )
        ),
        wait=my_wait_exponential(min=1, max=60),
        stop=(my_stop_after_attempt(8)),
        before_sleep=my_before_sleep,
    ):
        with attempt:
            response = (
                openai.chat.completions.create(
                    model="gpt-3.5-turbo",
                    messages=messages,
                    temperature=0.0,
                    logit_bias=logit_bias,
                    timeout=request_timeout,
                )
                .choices[0]
                .message.content
            )
        request_timeout = min(300, request_timeout * 2)

    return response


class CHATGPT:
    def __init__(self, seed, debug, kg_dataset) -> None:
        self.seed = seed
        self.debug = debug

        if self.seed is not None:
            set_seed(self.seed)

        self.kg_dataset = kg_dataset

        self.kg_dataset_path = f"data/{self.kg_dataset}"
        with open(
            f"{self.kg_dataset_path}/entity2id.json", "r", encoding="utf-8"
        ) as f:
            self.entity2id = json.load(f)
        with open(
            f"{self.kg_dataset_path}/id2info.json", "r", encoding="utf-8"
        ) as f:
            self.id2info = json.load(f)

        self.id2entityid = {}
        for id, info in self.id2info.items():
            if info["name"] in self.entity2id:
                self.id2entityid[id] = self.entity2id[info["name"]]

        self.item_embedding_path = f"data/embed_items/{self.kg_dataset}"

        item_emb_list = []
        id2item_id = []
        for i, file in tqdm(enumerate(os.listdir(self.item_embedding_path))):
            item_id = os.path.splitext(file)[0]
            if item_id in self.id2entityid:
                id2item_id.append(item_id)

                with open(
                    f"{self.item_embedding_path}/{file}", encoding="utf-8"
                ) as f:
                    embed = json.load(f)
                    item_emb_list.append(embed)

        self.id2item_id_arr = np.asarray(id2item_id)
        self.item_emb_arr = np.asarray(item_emb_list)

        self.chat_recommender_instruction = (
            "You are a recommender chatting with the user to provide "
            "recommendation. You must follow the instructions below during "
            "chat.\nIf you do not have enough information about user "
            "preference, you should ask the user for his preference.\n"
            "If you have enough information about user preference, you can "
            "give recommendation. The recommendation list must contain 10 "
            "items that are consistent with user preference. The "
            "recommendation list can contain items that the dialog mentioned "
            "before. The format of the recommendation list is: no. title. "
            "Don't mention anything other than the title of items in your "
            "recommendation list."
        )

    def get_rec(self, conv_dict):
        rec_labels = [
            self.entity2id[rec]
            for rec in conv_dict["rec"]
            if rec in self.entity2id
        ]

        context = conv_dict["context"]
        context_list = []  # for model

        for i, text in enumerate(context):
            if len(text) == 0:
                continue
            if i % 2 == 0:
                role_str = "user"
            else:
                role_str = "assistant"
            context_list.append({"role": role_str, "content": text})

        conv_str = ""

        for context in context_list[-2:]:
            conv_str += f"{context['role']}: {context['content']} "

        conv_embed = annotate(conv_str).data[0].embedding
        conv_embed = np.asarray(conv_embed).reshape(1, -1)

        sim_mat = cosine_similarity(conv_embed, self.item_emb_arr)
        rank_arr = np.argsort(sim_mat, axis=-1).tolist()
        rank_arr = np.flip(rank_arr, axis=-1)[:, :50]
        item_rank_arr = self.id2item_id_arr[rank_arr].tolist()
        item_rank_arr = [
            [self.id2entityid[item_id] for item_id in item_rank_arr[0]]
        ]

        return item_rank_arr, rec_labels

    def get_conv(self, conv_dict):
        context = conv_dict["context"]
        context_list = []  # for model
        context_list.append(
            {"role": "system", "content": self.chat_recommender_instruction}
        )

        for i, text in enumerate(context):
            if len(text) == 0:
                continue
            if i % 2 == 0:
                role_str = "user"
            else:
                role_str = "assistant"
            context_list.append({"role": role_str, "content": text})

        gen_inputs = None
        gen_str = annotate_chat(context_list)

        return gen_inputs, gen_str

    def get_choice(self, gen_inputs, options, state, conv_dict):
        updated_options = []
        for i, st in enumerate(state):
            if st >= 0:
                updated_options.append(options[i])

        encoding = tiktoken.encoding_for_model("gpt-3.5-turbo")
        logit_bias = {
            encoding.encode(option)[0]: 10 for option in updated_options
        }

        context = conv_dict["context"]
        context_list = []  # for model

        for i, text in enumerate(context[:-1]):
            if len(text) == 0:
                continue
            if i % 2 == 0:
                role_str = "user"
            else:
                role_str = "assistant"
            context_list.append({"role": role_str, "content": text})
        context_list.append({"role": "user", "content": context[-1]})

        response_op = annotate_chat(context_list, logit_bias=logit_bias)
        return response_op[0]

    def get_response(
        self,
        conv_dict: Dict[str, Any],
        id2entity: Dict[int, str],
        options: Tuple[str, Dict[str, str]],
        state: List[float],
    ) -> Tuple[str, List[float]]:
        """Generates a response given a conversation context.

        Args:
            conv_dict: Conversation context.
            id2entity: Mapping from entity id to entity name.
            options: Prompt with options and dictionary of options.
            state: State of the option choices.

        Returns:
            Generated response and updated state.
        """
        initial_conv_dict = deepcopy(conv_dict)
        conv_dict["context"].append(options[0])
        generated_inputs, generated_response = self.get_conv(conv_dict)
        options_letter = list(options[1].keys())

        # Get the choice between recommend and generate
        choice = self.get_choice(
            generated_inputs, options_letter, state, conv_dict
        )

        if choice == options_letter[-1]:
            # Generate a recommendation
            recommended_items, _ = self.get_rec(conv_dict)
            recommended_items_str = ""
            for i, item_id in enumerate(recommended_items[0][:3]):
                recommended_items_str += f"{i+1}: {id2entity[item_id]}  \n"
            response = (
                "I would recommend the following items:  \n"
                f"{recommended_items_str}"
            )
        else:
            # Original : Generate a response to ask for preferences. The
            # fallback is to use the generated response.
            # response = (
            #     options[1].get(choice, {}).get("template", generated_response)
            # )

            # Generate response with original context otherwise generated
            # response is the option's letter.
            _, generated_response = self.get_conv(initial_conv_dict)
            response = generated_response

        # Update the state. Hack: penalize the choice to reduce the
        # likelihood of selecting the same choice again
        state[options_letter.index(choice)] = -1e5

        return response, state