File size: 30,414 Bytes
3943768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
import ast
import contextlib
import gc
import os
import shutil
import tempfile
import threading
import traceback
import time
import base64
import mimetypes
import uuid
from enum import Enum
from pathlib import Path
from collections import defaultdict

import numpy as np
from pydantic import BaseModel

from .chat_history_render import chat_to_pretty_markdown

# control convert_to_pdf as expensive use of cores
num_convert_threads = max(min(10, os.cpu_count() or 1), 1)
convert_sem = threading.Semaphore(num_convert_threads)


class MyReturnType(BaseModel):
    class Config:
        extra = "allow"


# Local copy of minimal version from h2oGPT server
class LangChainAction(Enum):
    """LangChain action"""

    QUERY = "Query"
    SUMMARIZE_MAP = "Summarize"
    EXTRACT = "Extract"


def get_files_from_ids(usage=None, client=None, file_ids=None, work_dir=None):
    if usage is None and file_ids:
        pass
    elif hasattr(usage, "file_ids"):
        file_ids = usage.file_ids
    else:
        return []

    response_dict = {
        file_id: dict(client.files.retrieve(file_id)) for file_id in file_ids
    }

    # sort file_ids by server ctime, so first is newest
    file_ids = list(
        reversed(sorted(file_ids, key=lambda x: response_dict[x]["created_at"]))
    )

    if work_dir is None:
        temp_dir = tempfile.mkdtemp()
        if os.path.exists(temp_dir):
            shutil.rmtree(temp_dir)
        os.makedirs(temp_dir, exist_ok=True)
        work_dir = temp_dir

    files = []
    for file_id in file_ids:
        new_filename = os.path.join(
            work_dir, os.path.basename(response_dict[file_id]["filename"])
        )
        if os.path.exists(new_filename):
            # FIXME: small chance different with same name
            pass
        else:
            content = client.files.content(file_id).content
            with open(new_filename, "wb") as f:
                f.write(content)
        files.append(new_filename)

    return files


def file_to_base64(file_path, file_path_to_use=None):
    # Detect the file's MIME type
    mime_type, _ = mimetypes.guess_type(file_path)
    if not mime_type:
        mime_type = "unknown"

    # Read the file and encode it in base64
    with open(file_path, "rb") as file:
        encoded_file = base64.b64encode(file.read()).decode("utf-8")

    # Construct the data URL
    data_url = f"data:{mime_type};base64,{encoded_file}"
    if file_path_to_use is None:
        file_path_to_use = file_path
    return {file_path_to_use: data_url}


def clean_text_string(input_string):
    lines = input_string.split("\n")
    cleaned_lines = [
        line for line in lines if line and line.strip() and line.strip() != "-"
    ]
    return "\n".join(cleaned_lines)


def local_convert_to_pdf(convert_to_pdf, x, files_already_pdf, *args, **kwargs):
    if x in files_already_pdf:
        return x
    try:
        with convert_sem:
            return convert_to_pdf(x, *args, **kwargs)
    except Exception as e1:
        print(f"Error converting {x} to PDF: {e1}")
        return None


def group_files_by_base_name(file_names):
    grouped_files = defaultdict(list)
    for file in file_names:
        base_name = Path(file).stem
        grouped_files[base_name].append(file)
    return grouped_files


def group_and_prioritize_files(file_names):
    grouped_files = group_files_by_base_name(file_names)

    prioritized_files = []
    for base_name, files in grouped_files.items():
        preferred_file = select_preferred_file(files)
        # Put the preferred file first, then add all other files
        prioritized_group = [preferred_file] + [f for f in files if f != preferred_file]
        prioritized_files.extend(prioritized_group)

    return prioritized_files


def select_preferred_file(files):
    # Preference order: PDF, PNG, SVG, others
    for ext in [".pdf", ".png", ".svg"]:
        for file in files:
            if file.lower().endswith(ext):
                return file
    # If no preferred format found, return the first file
    return files[0]


def get_pdf_files(file_names, convert_to_pdf):
    # Group files by base name
    prioritized_files = group_and_prioritize_files(file_names)

    # Filter out binary files with text-like extensions
    # e.g. .txt but giant binary, then libreoffice will take too long to convert
    selected_files = [
        file
        for file in prioritized_files
        if not (is_binary(file) and Path(file).suffix.lower() in TEXT_EXTENSIONS)
    ]

    # Filter out audio files
    audio_exts = [
        ".mp3",
        ".wav",
        ".flac",
        ".ogg",
        ".m4a",
        ".aac",
        ".wma",
        ".aiff",
        ".mp4",
        ".mpeg",
        ".mpg",
        ".mpga",
        ".webm",
    ]

    exclude_exts = audio_exts + [".zip", ".tar", ".gz", ".bz2", ".xz", ".7z", ".rar"]

    selected_files = [
        file
        for file in selected_files
        if not any(file.lower().endswith(ext) for ext in exclude_exts)
    ]

    # 5MB limit to avoid long conversions
    selected_files = [
        f for f in selected_files if os.path.getsize(f) <= 5 * 1024 * 1024
    ]

    # Convert files to PDF
    pdf_file_names = []
    pdf_base_names = set()
    errors = []

    def process_file(file, pdf_base_names, convert_to_pdf):
        file_path = Path(file)
        base_name = file_path.stem
        ext_name = file_path.suffix.lower()

        if file_path.suffix.lower() == ".pdf":
            pdf_base_names.add(base_name)
            return str(file_path), base_name, None

        if base_name in pdf_base_names:
            new_pdf_name = f"{base_name}{ext_name}.pdf"
        else:
            new_pdf_name = f"{base_name}.pdf"
            pdf_base_names.add(base_name)

        new_pdf_path = file_path.with_name(new_pdf_name)
        new_dir = os.path.dirname(new_pdf_path)
        temp_file = file_path.with_suffix(f".{uuid.uuid4()}{file_path.suffix}")

        try:
            if not os.path.exists(new_dir):
                os.makedirs(new_dir, exist_ok=True)
            shutil.copy(file_path, temp_file)
            converted_pdf = local_convert_to_pdf(
                convert_to_pdf,
                temp_file,
                set(),
                correct_image=False,
            )
            if converted_pdf:
                shutil.move(converted_pdf, str(new_pdf_path))
                return str(new_pdf_path), base_name, None
        except Exception as e:
            return None, None, f"Error converting {file} to PDF: {e}"
        finally:
            if os.path.isfile(temp_file):
                try:
                    os.remove(temp_file)
                except Exception as e:
                    print(f"Error removing temp file {temp_file}: {e}")

        return None, None, f"Failed to process {file}"

    from concurrent.futures import ThreadPoolExecutor, as_completed, TimeoutError

    # Set timeouts
    timeout_seconds = 3 * 60
    timeout_seconds_per_file = 30

    t0 = time.time()

    with ThreadPoolExecutor() as executor:
        future_to_file = {
            executor.submit(process_file, file, pdf_base_names, convert_to_pdf): file
            for file in selected_files
        }

        while future_to_file:
            # Re-check remaining time for the overall timeout
            remaining_time = timeout_seconds - (time.time() - t0)
            if remaining_time <= 0:
                errors.append(f"Overall timeout of {timeout_seconds} seconds reached.")
                break

            # Check the futures as they complete or timeout
            try:
                for future in as_completed(future_to_file, timeout=remaining_time):
                    file = future_to_file[future]  # Get the corresponding file
                    try:
                        # Wait for the result of each future with a per-file timeout
                        result, base_name, error = future.result(
                            timeout=timeout_seconds_per_file
                        )

                        # Only pop the future after successful completion
                        future_to_file.pop(future)

                        if error:
                            errors.append(f"Error processing {file}: {error}")
                        elif result:
                            pdf_file_names.append(result)
                            pdf_base_names.add(base_name)
                    except TimeoutError:
                        errors.append(
                            f"Timeout error processing {file}: operation took longer than {timeout_seconds_per_file} seconds"
                        )
                    except Exception as exc:
                        errors.append(f"Unexpected error processing {file}: {exc}")
                        # We still want to pop the future on failure
                        future_to_file.pop(future)
            except TimeoutError:
                errors.append(
                    f"Timeout error processing {file}: operation took longer than {timeout_seconds_per_file} seconds"
                )
            except Exception as exc:
                errors.append(f"Unexpected error processing {file}: {exc}")

            # If all futures are processed or timeout reached, break
            if time.time() - t0 > timeout_seconds:
                errors.append(
                    f"Overall timeout of {timeout_seconds} seconds reached.  {len(future_to_file)} files remaining."
                )
                break

    if errors:
        print(errors)

    return pdf_file_names


def completion_with_backoff(
    get_client,
    model,
    messages,
    stream_output,
    hyper_kwargs,
    extra_body,
    timeout,
    time_to_first_token_max,
    ReturnType=None,
    use_agent=False,
    add_extra_endofturn=False,
    max_chars_per_turn=1024 * 4,
):
    t0_outer = time.time()
    ntrials = 3
    trial = 0
    while True:
        t0 = time.time()
        responses = None
        client = None
        time_to_first_token = None
        response = ""
        usage = None
        file_names = []
        try:
            client = get_client()
            responses = client.chat.completions.create(
                model=model,
                messages=messages,
                stream=stream_output,
                **hyper_kwargs,
                extra_body=extra_body,
                timeout=timeout,
            )

            if not stream_output:
                usage = responses.usage
                if responses.choices:
                    response = responses.choices[-1].message.content
                else:
                    response = ""
                yield ReturnType(reply=response)
                time_to_first_token = time.time() - t0
            else:
                response = ""
                usages = []
                for chunk in responses:
                    if chunk.usage is not None:
                        usages.append(chunk.usage)
                    if chunk.choices:
                        delta = chunk.choices[0].delta.content
                        if delta:
                            response += delta
                            # ensure if h2oGPTe wants full or delta, looks like delta from gradio code, except at very end?
                            yield ReturnType(reply=delta)
                            if time_to_first_token is None:
                                time_to_first_token = time.time() - t0
                            if use_agent and add_extra_endofturn:
                                splits = response.split("ENDOFTURN")
                                if splits and len(splits[-1]) > max_chars_per_turn:
                                    # force end of turn for UI purposes
                                    delta = "\n\nENDOFTURN\n\n"
                                    response += delta
                                    yield ReturnType(reply=delta)
                    time.sleep(0.005)
                    if (
                        time_to_first_token is None
                        and time.time() - t0 > time_to_first_token_max
                    ):
                        raise TimeoutError(
                            f"LLM {model} timed out without any response after {time_to_first_token_max} seconds, for total {time.time() - t0_outer} seconds.."
                        )
                    if time.time() - t0 > timeout:
                        print("Timed out, but had response: %s" % response, flush=True)
                        raise TimeoutError(
                            f"LLM {model} timed out after {time.time() - t0} seconds, for total {time.time() - t0_outer} seconds."
                        )
                assert len(usages) == 1, 'Missing usage"'
                usage = usages[0]

            # Get files
            file_names = (
                get_files_from_ids(usage=usage, client=client) if use_agent else []
            )
            return (
                response,
                usage,
                file_names,
                time_to_first_token or (time.time() - t0),
                None,
                None,
            )
        except (GeneratorExit, StopIteration):
            # caller is trying to cancel
            print(f"Caller initiated GeneratorExit in completion_with_backoff.")
            raise
        except Exception as e:
            error_ex = traceback.format_exc()
            error_e = str(e)
            if trial == ntrials - 1 or "Output contains sensitive information" in str(
                e
            ):
                print(
                    f"{model} hit final error in completion_with_backoff: {e}. Retrying trial {trial}."
                )
                if os.getenv("HARD_ASSERTS"):
                    raise
                # Note: response can be partial
                return (
                    response,
                    usage,
                    file_names,
                    time_to_first_token or (time.time() - t0),
                    error_e,
                    error_ex,
                )
            else:
                if trial == 0:
                    time.sleep(1)
                elif trial == 1:
                    time.sleep(5)
                else:
                    time.sleep(20)
                trial += 1
                print(
                    f"{model} hit error in completion_with_backoff: {e}. Retrying trial {trial}."
                )
        finally:
            if responses is not None:
                try:
                    responses.close()
                    del responses
                    gc.collect()
                except Exception as e:
                    print("Failed to close OpenAI response: %s" % str(e), flush=True)
            if client is not None:
                try:
                    client.close()
                    del client
                    gc.collect()
                except Exception as e:
                    print("Failed to close OpenAI client: %s" % str(e), flush=True)


def run_openai_client(
    get_client=None,
    ReturnType=None,
    convert_to_pdf=None,
    use_agent=False,
    agent_accuracy="standard",
    autogen_max_turns=80,
    agent_chat_history=[],
    agent_files=[],
    agent_venv_dir=None,
    agent_work_dir=None,
    base64_encode_agent_files=True,
    cute=False,
    time_to_first_token_max=None,
    **query_kwargs,
):
    """
    Bsed upon test in h2oGPT OSS:
    https://github.com/h2oai/h2ogpt/blob/ee3995865c85bf74f3644a4ebd007971c809de11/openai_server/test_openai_server.py#L189-L320
    """
    if ReturnType is None:
        ReturnType = MyReturnType

    # pick correct prompt
    # langchain_mode = query_kwargs.get("langchain_mode", "LLM")
    langchain_action = query_kwargs.get("langchain_action", "Query")
    # prompt will be "" for langchain_action = 'Summarize'
    prompt = query_kwargs["instruction"]
    model = query_kwargs["visible_models"]
    stream_output = query_kwargs["stream_output"]
    max_time = query_kwargs["max_time"]
    time_to_first_token_max = time_to_first_token_max or max_time
    text_context_list = query_kwargs["text_context_list"]
    chat_conversation = query_kwargs["chat_conversation"]
    image_files = query_kwargs["image_file"]
    system_message = query_kwargs["system_prompt"]

    from h2ogpte_core.backend_utils import structure_to_messages

    if use_agent:
        chat_conversation = None  # don't include high-level history yet

        file_ids = []
        if agent_files:
            client = get_client()
            for file_path in agent_files:
                with open(file_path, "rb") as file:
                    ret = client.files.create(
                        file=file,
                        purpose="assistants",
                    )
                    file_id = ret.id
                    file_ids.append(file_id)
                    assert ret.bytes > 0

        extra_body = dict(
            use_agent=use_agent,
            agent_type="auto",
            agent_accuracy=agent_accuracy,
            autogen_stop_docker_executor=False,
            autogen_run_code_in_docker=False,
            autogen_max_consecutive_auto_reply=80,
            autogen_max_turns=autogen_max_turns,
            autogen_timeout=240,
            autogen_cache_seed=None,
            work_dir=agent_work_dir,
            venv_dir=agent_venv_dir,
            agent_verbose=True,
            text_context_list=text_context_list,
            agent_chat_history=agent_chat_history,
            agent_files=file_ids,
            client_metadata=query_kwargs.get("client_metadata", ""),
        )
        # agent needs room, else keep hitting continue
        hyper_kwargs = dict(
            temperature=query_kwargs["temperature"],
            seed=query_kwargs["seed"],
            max_tokens=8192 if "claude-3-5-sonnet" in model else 4096,
        )
    else:
        extra_body = query_kwargs.copy()
        from h2ogpte_core.src.evaluate_params import eval_func_param_names

        extra_body = {k: v for k, v in extra_body.items() if k in eval_func_param_names}
        hyper_kwargs = dict(
            temperature=query_kwargs["temperature"],
            top_p=query_kwargs["top_p"],
            seed=query_kwargs["seed"],
            max_tokens=query_kwargs["max_new_tokens"],
        )
        extra_body = {k: v for k, v in extra_body.items() if k not in hyper_kwargs}
        # remove things that go through OpenAI API messages
        keys_in_api = [
            "visible_models",
            "image_file",
            "chat_conversation",
            "system_prompt",
            "instruction",
            "stream_output",
        ]
        for key in keys_in_api:
            extra_body.pop(key, None)
        # translate
        if "response_format" in extra_body:
            extra_body["response_format"] = dict(type=extra_body["response_format"])

    time_to_first_token = None
    t0 = time.time()

    messages = structure_to_messages(
        prompt, system_message, chat_conversation, image_files
    )

    timeout = 5 * max_time if use_agent else max_time
    (
        response,
        usage,
        file_names,
        time_to_first_token,
        error_e,
        error_ex,
    ) = yield from completion_with_backoff(
        get_client,
        model,
        messages,
        stream_output,
        hyper_kwargs,
        extra_body,
        timeout,
        time_to_first_token_max,
        ReturnType=ReturnType,
        use_agent=use_agent,
    )

    # in case streaming had deletions not yet accounted for, recover at least final answer,
    # e.g. for JSON {} then {}{"response": "yes"}
    if hasattr(usage, "response"):
        response = usage.response

    tf = time.time()

    # See if we can make text in case of no extension
    for file_i, file in enumerate(file_names):
        file_path = Path(file)
        suffix = file_path.suffix.lower()

        # If no suffix and not binary, rename to ".txt"
        if not suffix and not is_binary(file):
            new_file = file_path.with_suffix(".txt")
            try:
                file_path.rename(new_file)  # Rename the file, overwriting if necessary
                file_names[file_i] = str(new_file)
            except OSError as e:
                print(f"Error renaming {file} to {new_file}: {e}")

    if base64_encode_agent_files:
        files = [file_to_base64(x) for x in file_names]
        files = update_file_names(files)
    else:
        files = file_names

    # Process files and get PDF file names
    pdf_file_names = get_pdf_files(files, convert_to_pdf)

    if base64_encode_agent_files:
        files_pdf = [file_to_base64(x, y) for x, y in zip(pdf_file_names, file_names)]
        files_pdf = update_file_names(files_pdf)

        # clean-up
        [remove(x) for x in file_names if os.path.isfile(x)]
        [remove(x) for x in pdf_file_names if os.path.isfile(x)]
    else:
        files_pdf = pdf_file_names

    # Get usage
    input_tokens = usage.prompt_tokens if usage else 0
    output_tokens = usage.completion_tokens if usage else 0
    if hasattr(usage, "cost") and usage.cost:
        usage_no_caching = usage.cost["usage_excluding_cached_inference"]
        assert model in usage_no_caching, "Missing model %s in %s" % (
            model,
            usage_no_caching,
        )
        input_tokens += usage_no_caching[model]["prompt_tokens"]
        output_tokens += usage_no_caching[model]["completion_tokens"]

    # Get internal chat history
    chat_history = (
        usage.chat_history
        if hasattr(usage, "chat_history")
        else [{"role": "assistant", "content": response}]
    )
    chat_history_md = (
        chat_to_pretty_markdown(chat_history, cute=cute) if chat_history else ""
    )

    agent_work_dir = usage.agent_work_dir if hasattr(usage, "agent_work_dir") else None
    agent_venv_dir = usage.agent_venv_dir if hasattr(usage, "agent_venv_dir") else None

    # Get final answer
    response_intermediate = response
    if hasattr(usage, "summary"):
        response = usage.summary
        if not response:
            split1 = response_intermediate.split(
                "code_writer_agent(tocode_executor_agent):"
            )
            if split1 and split1[-1]:
                split2 = split1[-1].split("code_executor_agent(tocode_writer_agent):")
                if split2 and split1[0]:
                    response = split2[0]
                    response = clean_text_string(response)
        if not response:
            response = "The task is complete"
    elif "ENDOFTURN" in response:
        # show last turn as final response
        split_responses = response.split("ENDOFTURN")
        if len(split_responses) > 1:
            response = split_responses[-1]
        if not response:
            response = "The task completed"

    # estimate tokens per second
    tokens_per_second = output_tokens / (tf - t0 + 1e-6)

    t_taken_s = time.time() - t0
    t_taken = "%.4f" % t_taken_s
    if use_agent:
        if not (response or response_intermediate or files or chat_history):
            msg = f"No output from Agent with LLM {model} after {t_taken} seconds."
            if error_e:
                raise ValueError("Error: " + error_e + "\n" + msg)
            else:
                raise TimeoutError(msg)
    else:
        if not (response or response_intermediate):
            msg = f"No response from LLM {model} after {t_taken} seconds."
            if error_e:
                raise ValueError("Error: " + error_e + "\n" + msg)
            else:
                raise TimeoutError(msg)

    # extract other usages:
    sources = usage.sources if hasattr(usage, "sources") else []
    prompt_raw = usage.prompt_raw if hasattr(usage, "prompt_raw") else ""
    save_dict = usage.save_dict if hasattr(usage, "save_dict") else {}
    if not use_agent:
        if not hasattr(usage, "sources"):
            print("missing sources from usage: %s" % usage)
        if not hasattr(usage, "prompt_raw"):
            print("missing prompt_raw from usage: %s" % usage)
        if not hasattr(usage, "save_dict"):
            print("missing save_dict from usage: %s" % usage)
    extra_dict = save_dict.get("extra_dict", {})
    texts_out = [x["content"] for x in sources] if not use_agent else text_context_list
    t_taken_s = time.time() - t0
    t_taken = "%.4f" % t_taken_s

    if langchain_action != LangChainAction.EXTRACT.value:
        response = response.strip() if response else ""
        response_intermediate = response_intermediate.strip()
    else:
        response = [r.strip() if r else "" for r in ast.literal_eval(response)]
        response_intermediate = [
            r.strip() if r else "" for r in ast.literal_eval(response_intermediate)
        ]

    try:
        actual_llm = save_dict["display_name"]
    except Exception as e:
        actual_llm = model
        print(f"Unable to access save_dict to get actual_llm: {str(e)}")

    reply = response_intermediate if use_agent else response

    if not reply:
        error_e = (
            error_ex
        ) = f"No final response from LLM {actual_llm} after {t_taken} seconds\nError:{error_e}."
    if "error" in save_dict and not prompt_raw:
        msg = f"Error from LLM {actual_llm}: {save_dict['error']}"
        if os.getenv("HARD_ASSERTS"):
            if error_e:
                raise ValueError("Error: " + error_e + "\n" + msg)
            else:
                raise ValueError(msg)
    if not use_agent:
        if not (prompt_raw or extra_dict):
            msg = "LLM response failed to return final metadata."
            if os.getenv("HARD_ASSERTS"):
                if error_e:
                    raise ValueError("Error: " + error_e + "\n" + msg)
                else:
                    raise ValueError(msg)
    else:
        prompt_raw = prompt

    try:
        input_tokens = extra_dict["num_prompt_tokens"]
        output_tokens = extra_dict["ntokens"]
        vision_visible_model = extra_dict.get("batch_vision_visible_model")
        vision_batch_input_tokens = extra_dict.get("batch_num_prompt_tokens", 0)
        vision_batch_output_tokens = extra_dict.get("batch_ntokens", 0)
        tokens_per_second = np.round(extra_dict["tokens_persecond"], decimals=3)
        vision_batch_tokens_per_second = extra_dict.get("batch_tokens_persecond", 0)
        if vision_batch_tokens_per_second:
            vision_batch_tokens_per_second = np.round(
                vision_batch_tokens_per_second, decimals=3
            )
    except:
        vision_visible_model = model
        vision_batch_input_tokens = 0
        vision_batch_output_tokens = 0
        vision_batch_tokens_per_second = 0
        if not use_agent and os.getenv("HARD_ASSERTS"):
            raise

    if use_agent and not response and reply:
        # show streamed output then, to avoid confusion with whether had response
        response = reply

    if error_e or error_ex:
        delta_error = f"\n\n**Partial Error:**\n\n {error_e}"
        if use_agent:
            yield ReturnType(reply="\nENDOFTURN\n" + delta_error)
            response = delta_error
        else:
            yield ReturnType(reply=delta_error)
            response += delta_error

    # final yield
    yield ReturnType(
        reply=reply,
        reply_final=response,
        prompt_raw=prompt_raw,
        actual_llm=actual_llm,
        text_context_list=texts_out,
        input_tokens=input_tokens,
        output_tokens=output_tokens,
        tokens_per_second=tokens_per_second,
        time_to_first_token=time_to_first_token or (time.time() - t0),
        trial=0,  # Not required, OpenAI has retries
        vision_visible_model=vision_visible_model,
        vision_batch_input_tokens=vision_batch_input_tokens,
        vision_batch_output_tokens=vision_batch_output_tokens,
        vision_batch_tokens_per_second=vision_batch_tokens_per_second,
        agent_work_dir=agent_work_dir,
        agent_venv_dir=agent_venv_dir,
        files=files,
        files_pdf=files_pdf,
        chat_history=chat_history,
        chat_history_md=chat_history_md,
    )


# List of common text file extensions
TEXT_EXTENSIONS = {
    ".txt",
    ".md",
    ".csv",
    ".html",
    ".xml",
    ".json",
    ".yaml",
    ".yml",
    ".log",
}


def is_binary(filename):
    """
    Check if a file is binary or text using a quick check.

    Args:
        filename (str): The path to the file.

    Returns:
        bool: True if the file is binary, False otherwise.
    """

    try:
        with open(filename, "rb") as f:
            chunk = f.read(1024)  # Read the first 1KB of the file for a quick check
            if b"\0" in chunk:  # Null byte found, indicating binary content
                return True
            # Try decoding the chunk as UTF-8
            try:
                chunk.decode("utf-8")
            except UnicodeDecodeError:
                return True  # Decoding failed, likely a binary file
    except Exception as e:
        print(f"Error reading file: {e}")
        return True

    return False  # No null bytes and successful UTF-8 decoding, likely a text file


def update_file_names(file_list):
    def process_item(item):
        if isinstance(item, str):
            return os.path.basename(item)
        elif isinstance(item, dict):
            old_key = list(item.keys())[0]
            return {os.path.basename(old_key): item[old_key]}
        else:
            raise ValueError(f"Unsupported item type: {type(item)}")

    return [process_item(item) for item in file_list]


def shutil_rmtree(*args, **kwargs):
    path = args[0]
    assert not os.path.samefile(
        path, "/"
    ), "Should not be trying to remove entire root directory: %s" % str(path)
    assert not os.path.samefile(
        path, "./"
    ), "Should not be trying to remove entire local directory: %s" % str(path)
    return shutil.rmtree(*args, **kwargs)


def remove(path: str):
    try:
        if path is not None and os.path.exists(path):
            if os.path.isdir(path):
                shutil_rmtree(path, ignore_errors=True)
            else:
                with contextlib.suppress(FileNotFoundError):
                    os.remove(path)
    except BaseException as e:
        print(f"Error removing {path}: {e}")
        pass