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 
 
 
 
 
 
3281c07
 
aca9481
 
 
 
 
 
 
3281c07
 
aca9481
 
 
 
3281c07
aca9481
 
 
 
 
 
 
 
 
 
 
 
 
 
3281c07
aca9481
 
 
 
3281c07
aca9481
 
3281c07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aca9481
 
 
3281c07
 
aca9481
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3281c07
 
 
aca9481
 
3281c07
 
 
 
 
 
 
 
 
 
 
 
aca9481
 
3281c07
aca9481
 
 
 
 
3281c07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aca9481
 
 
 
 
 
3281c07
aca9481
 
 
 
3281c07
aca9481
 
 
 
 
 
 
 
 
 
 
3281c07
aca9481
 
 
 
 
 
 
 
 
 
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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
https://huggingface.co/spaces/fffiloni/langchain-chat-with-pdf-openai
"""
import argparse
import json
import logging
import time
from typing import List, Tuple

logging.basicConfig(
    level=logging.DEBUG,
    format="%(asctime)s %(levelname)s %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
)

import gradio as gr
import openai
from openai import OpenAI
from threading import Thread
import _queue
from queue import Queue

import project_settings as settings
from project_settings import project_path

logger = logging.getLogger(__name__)


def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--openai_api_key",
        default=settings.environment.get("openai_api_key", default=None, dtype=str),
        type=str
    )
    args = parser.parse_args()
    return args


def greet(question: str, history: List[Tuple[str, str]]):
    answer = "Hello " + question + "!"
    result = history + [(question, answer)]
    return result


def click_create_assistant(openai_api_key: str,
                           name: str,
                           instructions: str,
                           description: str,
                           tools: str,
                           files: List[str],
                           file_ids: str,
                           model: str,
                           ):
    logger.info("click create assistant, name: {}".format(name))
    client = OpenAI(
        api_key=openai_api_key,
    )

    # tools
    tools = str(tools).strip()
    if tools is not None and len(tools) != 0:
        tools = tools.split("\n")
        tools = [json.loads(tool) for tool in tools if len(tool.strip()) != 0]
    else:
        tools = list()

    # files
    if files is not None and len(files) != 0:
        files = [
            client.files.create(
                file=open(file, "rb"),
                purpose='assistants'
            ) for file in files
        ]
    else:
        files = list()

    # file_ids
    file_ids = str(file_ids).strip()
    if file_ids is not None and len(file_ids) != 0:
        file_ids = file_ids.split("\n")
        file_ids = [file_id.strip() for file_id in file_ids if len(file_id.strip()) != 0]
    else:
        file_ids = list()

    # assistant
    assistant = client.beta.assistants.create(
        name=name,
        instructions=instructions,
        description=description,
        tools=tools,
        file_ids=file_ids + [file.id for file in files],
        model=model,
    )
    assistant_id = assistant.id

    # thread
    thread = client.beta.threads.create()
    thread_id = thread.id

    return assistant_id, thread_id


def click_list_assistant(openai_api_key: str) -> str:
    client = OpenAI(
        api_key=openai_api_key,
    )
    assistant_list = client.beta.assistants.list()
    assistant_list.model_dump(mode="json")

    result = ""
    for a in assistant_list["data"]:
        assis = "id: \n{}\nname: \n{}\ndescription: \n{}\n\n".format(a["id"], a["name"], a["description"])
        result += assis

    return result


def click_delete_assistant(openai_api_key: str,
                           assistant_id: str) -> str:
    logger.info("click delete assistant, assistant_id: {}".format(assistant_id))

    client = OpenAI(
        api_key=openai_api_key,
    )

    try:
        assistant_deleted = client.beta.assistants.delete(assistant_id=assistant_id)
        result = "success" if assistant_deleted.deleted else "failed"
    except openai.NotFoundError as e:
        result = e.message
    return result


def click_list_file(openai_api_key: str):
    client = OpenAI(
        api_key=openai_api_key,
    )
    file_list = client.files.list()
    file_list = file_list.model_dump(mode="json")

    result = ""
    for f in file_list["data"]:
        file = "id: \n{}\nfilename: \n{}\nbytes: \n{}\nstatus: \n{}\n\n".format(
            f["id"], f["filename"], f["bytes"], f["status"]
        )
        result += file

    return result


def click_delete_file(openai_api_key: str,
                      file_id: str) -> str:
    logger.info("click delete file, file_id: {}".format(file_id))

    client = OpenAI(
        api_key=openai_api_key,
    )

    try:
        assistant_deleted = client.files.delete(file_id=file_id)
        result = "success" if assistant_deleted.deleted else "failed"
    except openai.NotFoundError as e:
        result = e.message
    return result


def click_upload_files(openai_api_key: str,
                       files: List[str],
                       ):
    logger.info("click upload files, files: {}".format(files))

    client = OpenAI(
        api_key=openai_api_key,
    )

    result = list()
    if files is not None and len(files) != 0:
        files = [
            client.files.create(
                file=open(file, "rb"),
                purpose='assistants'
            ) for file in files
        ]
        file_ids = [file.id for file in files]
        result.extend(file_ids)
    return result


def get_message_list(client: OpenAI, thread_id: str):
    """
    SyncCursorPage[ThreadMessage](
        data=[
            ThreadMessage(
                id='msg_kb0f2fyDC6OwMyXxKbUpcuBS',
                assistant_id='asst_DzVVZkE0dIGe0gsOdsdn3A0w',
                content=[
                    MessageContentText(
                        text=Text(
                            annotations=[
                                TextAnnotationFileCitation(
                                    end_index=44,
                                    file_citation=TextAnnotationFileCitationFileCitation(
                                        file_id='file-IwzwXQkixMu7fvgGoC1alIWu',
                                        quote='念刘备、关羽、张飞,虽然异姓,既结为兄弟,则同心协力,救困扶危;上报国家,下安黎庶。不求同年同月同日生,只愿同年同月同日死。皇天后土,实鉴此心,背义忘恩,天人共戮!”誓毕,拜玄德为兄,关羽次之,张飞为弟'
                                    ),
                                    start_index=34,
                                    text='【7†source】',
                                    type='file_citation'
                                )
                            ],
                            value='刘备和张飞虽然是异姓,但他们结为了兄弟,其中刘备被拜为兄,而张飞为弟【7†source】。'
                        ),
                        type='text'
                    )
                ],
                created_at=1699493845,
                file_ids=[],
                metadata={},
                object='thread.message',
                role='assistant',
                run_id='run_zJYZX0KFEvEh2VG5x5zSLq9s',
                thread_id='thread_3JWRdjvZDJTBgZ0tlrrKXnrt'
            ),

            ThreadMessage(
                id='msg_tc5Tit7q19S5TSgvmBauME3H',
                assistant_id=None,
                content=[
                    MessageContentText(
                        text=Text(
                            annotations=[],
                            value='刘备和张飞是什么关系。'
                        ),
                        type='text'
                    )
                ],
                created_at=1699493838,
                file_ids=[],
                metadata={},
                object='thread.message',
                role='user',
                run_id=None,
                thread_id='thread_3JWRdjvZDJTBgZ0tlrrKXnrt'
            )

        ],
        object='list',
        first_id='msg_kb0f2fyDC6OwMyXxKbUpcuBS',
        last_id='msg_tc5Tit7q19S5TSgvmBauME3H',
        has_more=False
    )
    """
    messages = client.beta.threads.messages.list(
        thread_id=thread_id
    )
    # print(messages)

    result = list()
    for message in messages.data:

        content = list()
        for msg in message.content:
            annotations = list()
            for annotation in msg.text.annotations:
                a = {
                    "start_index": annotation.start_index,
                    "end_index": annotation.end_index,
                    "text": annotation.text,
                    "type": annotation.type,
                }

                if annotation.type == "file_citation":
                    a["file_citation"] = {
                        "file_id": annotation.file_citation.file_id,
                        "quote": annotation.file_citation.quote,
                    }

                annotations.append(a)

            content.append({
                "text": {
                    "annotations": annotations,
                    "value": msg.text.value,
                },
                "type": msg.type,

            })

        result.append({
            "id": message.id,
            "assistant_id": message.assistant_id,
            "content": content,
            "created_at": message.created_at,
            "file_ids": message.file_ids,
            "metadata": message.metadata,
            "object": message.object,
            "role": message.role,
            "run_id": message.run_id,
            "thread_id": message.thread_id,

        })

    result = list(sorted(result, key=lambda x: x["created_at"]))
    return result


def convert_message_list_to_response(message_list: List[dict]) -> str:
    response = ""
    for message in message_list:
        role = message["role"]
        content = message["content"]

        for c in content:
            if c["type"] != "text":
                continue
            text: dict = c["text"]
            msg = "{}: \n{}\n".format(role, text["value"])
            response += msg
            response += "-" * 80
            response += "\n"

    return response


def convert_message_list_to_conversation(message_list: List[dict]) -> List[Tuple[str, str]]:
    conversation = list()
    for message in message_list:
        role = message["role"]
        content = message["content"]
        for c in content:
            c_type = c["type"]
            if c_type != "text":
                continue
            text: dict = c["text"]

            if c_type == "text":
                text_value = text["value"]
                text_annotations = text["annotations"]

                msg = text_value
                for text_annotation in text_annotations:
                    a_type = text_annotation["type"]
                    if a_type == "file_citation":
                        msg += "\n\n"
                        msg += "\nquote: \n{}\nfile_id: \n{}".format(
                            text_annotation["file_citation"]["quote"],
                            text_annotation["file_citation"]["file_id"],
                        )
            else:
                raise NotImplementedError

            if role == "assistant":
                msg = [None, msg]
            else:
                msg = [msg, None]

            conversation.append(msg)

    return conversation


def streaming_refresh(openai_api_key: str,
                      thread_id: str,
                      queue: Queue,
                      ):
    delta_time = 0.3
    last_response = None
    no_updates_count = 0
    max_no_updates_count = 5
    while True:
        time.sleep(delta_time)

        this_response = refresh(openai_api_key, thread_id)

        if this_response == last_response:
            no_updates_count += 1
        if no_updates_count >= max_no_updates_count:
            break
        last_response = this_response

        queue.put(this_response, block=True, timeout=2)

    return last_response


def refresh(openai_api_key: str,
            thread_id: str,
            ):
    client = OpenAI(
        api_key=openai_api_key,
    )

    message_list = get_message_list(client, thread_id=thread_id)
    print(message_list)
    logger.info("message_list: {}".format(message_list))
    conversation = convert_message_list_to_conversation(message_list)
    return conversation


def add_and_run(openai_api_key: str,
                assistant_id: str,
                thread_id: str,
                name: str,
                instructions: str,
                description: str,
                tools: str,
                files: List[str],
                file_ids: str,
                model: str,
                query: str,
                ):
    client = OpenAI(
        api_key=openai_api_key,
    )
    if assistant_id is None or len(assistant_id.strip()) == 0:
        assistant_id = click_create_assistant(
            openai_api_key,
            name, instructions, description, tools, files, file_ids, model
        )

    if thread_id is None or len(thread_id.strip()) == 0:
        thread = client.beta.threads.create()
        thread_id = thread.id

    message = client.beta.threads.messages.create(
        thread_id=thread_id,
        role="user",
        content=query
    )
    run = client.beta.threads.runs.create(
        thread_id=thread_id,
        assistant_id=assistant_id,
    )
    run = client.beta.threads.runs.retrieve(
        thread_id=thread_id,
        run_id=run.id
    )

    response_queue = Queue(maxsize=10)
    refresh_kwargs = dict(
        openai_api_key=openai_api_key,
        thread_id=thread_id,
        queue=response_queue,
    )
    thread = Thread(target=streaming_refresh, kwargs=refresh_kwargs)
    thread.start()

    delta_time = 0.1
    last_response = None
    no_updates_count = 0
    max_no_updates_count = 10
    while True:
        time.sleep(delta_time)

        try:
            this_response = response_queue.get(block=True, timeout=2)
        except _queue.Empty:
            break

        if this_response == last_response:
            no_updates_count += 1
        if no_updates_count >= max_no_updates_count:
            break
        last_response = this_response

        result = [
            assistant_id, thread_id,
            last_response,
            []
        ]
        yield result


def main():
    args = get_args()

    gr_description = """
    OpenAI Assistant
    """

    # ui
    with gr.Blocks() as blocks:
        gr.Markdown(value=gr_description)

        with gr.Row():
            # settings
            with gr.Column(scale=3):
                with gr.Tabs():
                    with gr.TabItem("create assistant"):
                        openai_api_key = gr.Text(
                            value=args.openai_api_key,
                            label="openai_api_key",
                            placeholder="Fill with your `openai_api_key`"
                        )

                        name = gr.Textbox(label="name")
                        instructions = gr.Textbox(label="instructions")
                        description = gr.Textbox(label="description")

                        model = gr.Dropdown(["gpt-4-1106-preview"], value="gpt-4-1106-preview", label="model")

                        # functions
                        tools = gr.TextArea(label="functions")

                        # upload files
                        retrieval_files = gr.Files(label="retrieval_files")
                        retrieval_file_ids = gr.TextArea(label="retrieval_file_ids")

                        # create assistant
                        create_assistant_button = gr.Button("create assistant")

                    with gr.TabItem("list assistant"):
                        list_assistant_button = gr.Button("list assistant")
                        assistant_list = gr.TextArea(label="assistant_list")

                        delete_assistant_id = gr.Textbox(label="delete_assistant_id")
                        delete_assistant_button = gr.Button("delete assistant")

                    with gr.TabItem("list file"):
                        list_file_button = gr.Button("list file")
                        file_list = gr.TextArea(label="file_list")

                        delete_file_id = gr.Textbox(label="delete_file_id")
                        delete_file_button = gr.Button("delete file")

                        upload_files = gr.Files(label="upload_files")
                        upload_files_button = gr.Button("upload file")

            # chat
            with gr.Column(scale=5):
                chat_bot = gr.Chatbot(label="conversation", height=600)
                query = gr.Textbox(lines=1, label="query")

                with gr.Row():
                    with gr.Column(scale=1):
                        add_and_run_button = gr.Button("Add and run")
                    with gr.Column(scale=1):
                        refresh_button = gr.Button("Refresh")

            # states
            with gr.Column(scale=2):
                assistant_id = gr.Textbox(value=None, label="assistant_id")
                thread_id = gr.Textbox(value=None, label="thread_id")

        # examples
        with gr.Row():
            gr.Examples(
                examples=[
                    [
                        "Math Tutor",
                        "You are a personal math tutor. Write and run code to answer math questions.",
                        "Official math test cases",
                        None,
                        None,
                        "gpt-4-1106-preview",
                        "123 * 524 等于多少?"
                    ],
                    [
                        "小说专家",
                        "根据小说内容回答问题。",
                        "三国演义文档问答测试",
                        "{\"type\": \"retrieval\"}",
                        [
                            (project_path / "data/三国演义.txt").as_posix()
                        ],
                        "gpt-4-1106-preview",
                        "刘备和张飞是什么关系。"
                    ],
                ],
                inputs=[
                    name, instructions, description, tools, retrieval_files, model,
                    query,
                ],
                examples_per_page=5
            )

        # create assistant
        create_assistant_button.click(
            click_create_assistant,
            inputs=[
                openai_api_key,
                name, instructions, description, tools, retrieval_files, retrieval_file_ids, model,
            ],
            outputs=[
                assistant_id, thread_id
            ]
        )

        # list assistant
        list_assistant_button.click(
            click_list_assistant,
            inputs=[
                openai_api_key
            ],
            outputs=[
                assistant_list
            ]
        )

        # delete assistant button
        delete_assistant_button.click(
            click_delete_assistant,
            inputs=[
                openai_api_key,
                delete_assistant_id
            ],
            outputs=[
                delete_assistant_id
            ]
        )

        # list file
        list_file_button.click(
            click_list_file,
            inputs=[
                openai_api_key
            ],
            outputs=[
                file_list
            ],
        )

        # delete file
        delete_file_button.click(
            click_delete_file,
            inputs=[
                openai_api_key,
                delete_file_id
            ],
            outputs=[
                delete_file_id
            ]
        )

        # upload files
        upload_files_button.click(
            click_upload_files,
            inputs=[
                openai_api_key,
                upload_files
            ],
            outputs=[

            ]
        )

        # add and run
        add_and_run_button.click(
            add_and_run,
            inputs=[
                openai_api_key,
                assistant_id, thread_id,
                name, instructions, description, tools, retrieval_files, retrieval_file_ids, model,
                query,
            ],
            outputs=[
                assistant_id, thread_id,
                chat_bot
            ],
        )

        # refresh
        refresh_button.click(
            refresh,
            inputs=[
                openai_api_key,
                thread_id,
            ],
            outputs=[
                chat_bot
            ]
        )

    blocks.queue().launch()

    return


if __name__ == '__main__':
    main()