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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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import json
import os
import sys
import time
import copy
import re
from pathlib import Path
from typing import List, Literal, Optional, Tuple, TypedDict, Dict
import numpy as np
from tqdm import tqdm

# Get the path from environment variable
prj_root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(prj_root_path)
from code_interpreter.JuypyterClient import JupyterNotebook
from code_interpreter.BaseCodeInterpreter import BaseCodeInterpreter
from utils.const import *
from prompt.gpt4_prompt import CODE_INTERPRETER_SYSTEM_PROMPT

# from prompt.gpt4_prompt import CODE_INTERPRETER_SYSTEM_PROMPT
from colorama import init, Fore, Style, Back
from rich.markdown import Markdown
import base64

import openai
from retrying import retry
import requests
import logging
from termcolor import colored

# load from key file
with open("./openai_api_key.txt") as f:
    OPENAI_API_KEY = key = f.read()
openai.api_key = OPENAI_API_KEY
from utils.cleaner import clean_error_msg


def remove_string(s):
    pattern = r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{6}:.*LD_LIBRARY_PATH: /usr/local/nvidia/lib:/usr/local/nvidia/lib64\n"
    return re.sub(pattern, "", s)


def clean_the_dialog(dialog, question):
    question_idx = 0
    for idx, item in enumerate(dialog):
        if item["content"] == question:
            question_idx = idx

    filtered_dialog = dialog[question_idx:]

    user_qinit_dict = filtered_dialog[0]
    answer_fuse_str = "\n".join([i["content"].strip() for i in filtered_dialog[1::2]])

    final_dialog_dict = [
        {"role": "user", "content": user_qinit_dict["content"]},
        {"role": "assistant", "content": answer_fuse_str},
    ]

    return final_dialog_dict


@retry(
    stop_max_attempt_number=7,
    wait_exponential_multiplier=1000,
    wait_exponential_max=10000,
)
def get_embedding(text, model="text-embedding-ada-002"):
    global counter
    headers = {
        "Authorization": f"Bearer {OPENAI_API_KEY}",  # Make sure to replace with your OpenAI API key
        "Content-Type": "application/json",
    }
    payload = {"input": text, "model": model}

    response = requests.post(
        "https://api.openai.com/v1/embeddings", headers=headers, json=payload
    )

    if response.status_code != 200:
        raise Exception(f"Request failed with status {response.status_code}")

    return np.array(response.json()["data"][0]["embedding"])


class QueryRetrospect:
    def __init__(
        self,
        data_directory="./gpt_data_gen_retrospect/",
        embeddings_path="./gpt_data_gen_retrospect/embeddings.npy",
    ):
        self.data_directory = data_directory
        self.embeddings_path = embeddings_path
        self.data = []
        self.embeddings = []

        if os.path.exists(embeddings_path):
            print("++ Embedding Exists!")
            self.embeddings = np.load(embeddings_path)
            for fname in [i for i in os.listdir(data_directory) if i.endswith(".json")]:
                with open(
                    os.path.join(data_directory, fname),
                    "r",
                    encoding="utf-8",
                    errors="replace",
                ) as f:
                    self.data.append(json.load(f))
        else:
            only_files = [
                f
                for f in os.listdir(data_directory)
                if os.path.isfile(os.path.join(data_directory, f))
                and f.endswith(".json")
            ]

            for fname in tqdm(only_files):
                with open(
                    os.path.join(data_directory, fname), "r", encoding="cp1252"
                ) as f:
                    data_point = json.load(f)
                    self.data.append(data_point)
                    self.embeddings.append(
                        get_embedding(data_point["execution_result"])
                    )
            self.embeddings = np.array(self.embeddings)
            self.save_embeddings()
            print(f"++ Embedding Saved! {self.embeddings.shape}")

    def save_embeddings(self):
        np.save(self.embeddings_path, self.embeddings)

    def __call__(self, query, top_k=3, VERBOSE: bool = False):
        query_embedding = get_embedding(query)
        similarities = np.dot(self.embeddings, query_embedding)
        top_indices = similarities.argsort()[-top_k:][::-1]
        return [self.data[i]["retrospection"] for i in top_indices]


class QueryRetrospectPrefix:
    def __init__(
        self,
        model="gpt-4",
        data_directory="./eval/gpt_mbpp_output",
        embeddings_path="./eval/gpt_mbpp_output/embeddings.npy",
    ):
        self.data_directory = data_directory
        self.embeddings_path = embeddings_path
        self.data = []
        self.embeddings = []

        if os.path.exists(embeddings_path):
            print("++ Embedding Exists!")
            self.embeddings = np.load(embeddings_path)
            for fname in [i for i in os.listdir(data_directory) if i.endswith(".json")]:
                with open(
                    os.path.join(data_directory, fname),
                    "r",
                    encoding="utf-8",
                    errors="replace",
                ) as f:
                    self.data.append(json.load(f))
        else:
            only_files = [
                f
                for f in os.listdir(data_directory)
                if os.path.isfile(os.path.join(data_directory, f))
                and f.endswith(".json")
            ]

            for fname in tqdm(only_files):
                with open(
                    os.path.join(data_directory, fname), "r", encoding="cp1252"
                ) as f:
                    data_point = json.load(f)

                    print(f'Processing "{data_point[1]["content"]}" ...')
                    self.data.append(data_point)
                    self.embeddings.append(get_embedding(data_point[1]["content"]))

            self.embeddings = np.array(self.embeddings)
            self.save_embeddings()
            print(f"++ Embedding Saved! {self.embeddings.shape}")

        self.model = model
        self.dialog = [
            {
                "role": "system",
                "content": "You are retrospection GPT. retrospect from the given data.",
            },
            {
                "role": "user",
                "content": 'Current Question:\n\nWrite a Python function to solve the following task:\n\nfrom typing import List\n\ndef cum_sum(numbers: List[int]) -> List[int]:\n    """\n    From a given list of integers, generate a list representing the cumulative sum of elements at each index.\n    >>> cum_sum([1, 2, 3, 4])\n    [1, 3, 6, 10]\n    """\n\nRetrieved Trajectories : \nIn a past interaction, a function named running_average was provided to calculate the running average of a list of numbers.\n\n```python\ndef running_average(numbers: List[int]) -> List[float]:\n    total = 0\n    averages = []\n    for i, num in enumerate(numbers):\n        total += num\n        averages.append(total / (i+1))\n    return averages\n\nprint(running_average([1,2,3,4])) # expected [1.0, 1.5, 2.0, 2.5]\n```\n```RESULT\n[1.0, 1.5, 2.0, 2.5]\n```\nThe output is expected. \n\n',
            },
            {
                "role": "assistant",
                "content": "From previous similar questions :\nThe `running_average` function highlights an important concept of maintaining a running or cumulative value (total) as one iterates over the list. This is directly applicable to the cum_sum problem.\n\nApplication to the Question:\nFor the cum_sum function, one needs to maintain a cumulative total of the elements as we traverse through the list. The running_average function is most closely related since it involves accumulating a total and storing intermediate results. By adapting this logic (i.e., excluding the division operation to compute the average), one can easily derive the cumulative sum solution.",
            },
        ]
        self.response = ""

    @retry(
        stop_max_attempt_number=7,
        wait_exponential_multiplier=1000,
        wait_exponential_max=10000,
    )
    def ChatCompletion(self):
        try:
            self.response = openai.ChatCompletion.create(
                model=self.model, messages=self.dialog, temperature=0.2, top_p=0.9
            )
        except Exception as e:
            print(f"error while OPENAI api call {e} {self.response}")

    def save_embeddings(self):
        np.save(self.embeddings_path, self.embeddings)

    def __call__(self, query, top_k=3, VERBOSE: bool = False):
        query_embedding = get_embedding(query)
        similarities = np.dot(self.embeddings, query_embedding)
        top_indices = similarities.argsort()[-top_k:][::-1]
        top_i = top_indices[0]
        prior_traj = self.data[top_i][-1]["content"]

        ask_dict = {
            "role": "user",
            "content": f"Current Question:\n\n{query}\n\nRetrieved Trajectories :\n{prior_traj}",
        }

        # print(f"From prior experience:\n{prior_traj}\n\nCurrent Question:\n{query}\n")
        self.dialog.append(ask_dict)
        self.ChatCompletion()

        return self.response["choices"][0]["message"]["content"]


class RetrospectiveGPTCodeInterpreter(BaseCodeInterpreter):
    def __init__(self, model="gpt-4"):
        self.model = model
        self.dialog = [
            # {"role": "system", "content":  CODE_INTERPRETER_SYSTEM_PROMPT },
            {
                "role": "system",
                "content": CODE_INTERPRETER_SYSTEM_PROMPT,
            },
            # {"role": "user", "content": "How can I use BeautifulSoup to scrape a website and extract all the URLs on a page?"},
            # {"role": "assistant", "content": "I think I need to use beatifulsoup to find current korean president,"}
        ]

        # self.dialog += few_shot_4
        self.response = None

        assert os.path.isfile(
            "./openai_api_key.txt"
        ), "The openai_api_key.txt file could not be found. Please make sure it is in the same directory as this script, and that it contains your OpenAI API key."

        # load from key file
        with open("./openai_api_key.txt") as f:
            OPENAI_API_KEY = f.read()
        openai.api_key = OPENAI_API_KEY

        self.nb = JupyterNotebook()
        out = self.nb.add_and_run(TOOLS_CODE)  # tool import

        # retrospections
        self.retrospector = QueryRetrospectPrefix()

    def get_response_content(self):
        if self.response:
            return self.response["choices"][0]["message"]["content"]
        else:
            return None

    @retry(
        stop_max_attempt_number=7,
        wait_exponential_multiplier=1000,
        wait_exponential_max=10000,
    )
    def ChatCompletion(self):
        try:
            self.response = openai.ChatCompletion.create(
                model=self.model, messages=self.dialog, temperature=0.2, top_p=0.9
            )
        except Exception as e:
            print(f"error while OPENAI api call {e}")

    def save_dialog(self, path: str = "./output/dialog.json"):
        with open(path, "w") as f:
            json.dump(self.dialog, f)
            print(f" ++Dialog saved to [{path}]")

    def close(self):
        """
        close jupyter notebook, and this class instance
        """
        self.nb.close()

    def chat(
        self,
        user_message: str,
        VERBOSE: bool = False,
        MAX_TRY: int = 6,
        code_exec_prefix: str = "",
        feedback_prompt: str = "",
        append_result: bool = True,
        use_retrospect: bool = True,
    ):
        prefix_retrospection = self.retrospector(query=user_message)
        self.dialog.append(
            {"role": "user", "content": f"{prefix_retrospection}\n\n{user_message}"}
        )
        init_feedback = copy.deepcopy(feedback_prompt)

        code_block_output = ""
        attempt = 0
        img_data = None

        if VERBOSE:
            print(
                "###Retrospection : "
                + Fore.BLUE
                + Back.WHITE
                + Style.BRIGHT
                + prefix_retrospection
                + Style.RESET_ALL
            )
            print(
                "###User : " + Fore.BLUE + Style.BRIGHT + user_message + Style.RESET_ALL
            )
            print("\n###Assistant : ")

        for i in range(MAX_TRY):
            # GPT response
            self.ChatCompletion()

            # Get code block
            generated_text = self.get_response_content()
            generated_code_blocks = self.extract_code_blocks(generated_text)
            # execute code
            if len(generated_code_blocks) > 0:
                # Find the position of the first code block in the last answer
                first_code_block_pos = (
                    generated_text.find(generated_code_blocks[0])
                    if generated_code_blocks
                    else -1
                )
                text_before_first_code_block = (
                    generated_text
                    if first_code_block_pos == -1
                    else generated_text[:first_code_block_pos]
                )
                if VERBOSE:
                    print(Fore.GREEN + text_before_first_code_block + Style.RESET_ALL)
                if VERBOSE:
                    print(
                        Fore.YELLOW
                        + generated_code_blocks[0]
                        + "\n```\n"
                        + Style.RESET_ALL
                    )
                code_block_output, error_flag = self.execute_code_and_return_output(
                    generated_code_blocks[0]
                )

                code_block_output = f"{code_block_output}"

                if code_block_output is not None:
                    code_block_output = code_block_output.strip()

                code_block_output = remove_string(code_block_output)
                if len(code_block_output) > 500:
                    code_block_output = (
                        code_block_output[:200] + "⋯(skip)⋯" + code_block_output[-200:]
                    )
                code_block_output_str = f"\n```RESULT\n{code_block_output}\n```\n"
                if append_result:
                    gen_final = f"{text_before_first_code_block}{generated_code_blocks[0]}\n```{code_block_output_str}"
                    if VERBOSE:
                        print(
                            Fore.LIGHTBLACK_EX + code_block_output_str + Style.RESET_ALL
                        )
                else:
                    gen_final = (
                        f"{text_before_first_code_block}{generated_code_blocks[0]}\n```"
                    )

                self.dialog.append(
                    {
                        "role": "assistant",
                        "content": gen_final,
                    }
                )

                feedback_prompt = f"{init_feedback}\nif you accomplish the instruction just say <done>\nIf not keep going."
                if VERBOSE:
                    print(Fore.MAGENTA + feedback_prompt + Style.RESET_ALL)

                feedback_dict = {
                    "role": "user",
                    "content": feedback_prompt,
                }

                self.dialog.append(feedback_dict)

            else:
                if "<done>" in generated_text:
                    generated_text = generated_text.split("<done>")[0].strip()

                if len(generated_text) <= 0:
                    break

                if VERBOSE:
                    print(Fore.GREEN + generated_text + Style.RESET_ALL)

                self.dialog.append(
                    {
                        "role": "assistant",
                        "content": f"{generated_text}",
                    }
                )
                break

        self.dialog = [self.dialog[0]] + clean_the_dialog(
            self.dialog, question=f"{prefix_retrospection}\n\n{user_message}"
        )  # delete retrospections after generation step

        return self.dialog[-1]


if __name__ == "__main__":
    import pickle
    import random
    from tqdm import tqdm

    # python3 -m code_interpreter.RetrospectiveGPTCodeInterpreter

    retro_interpreter = RetrospectiveGPTCodeInterpreter(model="gpt-4")

    instruction = """
Write a Python script to solve the following problem:

def get_row(lst, x):
	\"\"\"
	You are given a 2 dimensional data, as a nested lists,
	which is similar to matrix, however, unlike matrices,
	each row may contain a different number of columns.
	Given lst, and integer x, find integers x in the list,
	and return list of tuples, [(x1, y1), (x2, y2) ...] such that
	each tuple is a coordinate - (row, columns), starting with 0.
	Sort coordinates initially by rows in ascending order.
	Also, sort coordinates of the row by columns in descending order.
	
	Examples:
	get_row([
	  [1,2,3,4,5,6],
	  [1,2,3,4,1,6],
	  [1,2,3,4,5,1]
	], 1) == [(0, 0), (1, 4), (1, 0), (2, 5), (2, 0)]
	get_row([], 1) == []
	get_row([[], [1], [1, 2, 3]], 3) == [(2, 2)]
	\"\"\"

Ensure the solution is verified by printing the expected output.
"""
    # instruction = "Can you make a image of astraunaut in the garden?"

    # example
    retro_interpreter.chat(
        user_message=instruction,
        MAX_TRY=5,
        use_retrospect=True,
        feedback_prompt="Ensure the output matches the expected result, taking into account any corner cases. If discrepancies arise, pinpoint where you went wrong. Then, refine the code to achieve the desired outcome.",
        VERBOSE=True,
    )