aboutsummaryrefslogtreecommitdiff
path: root/gn3/llms/response.py
blob: 11cbd94468f9465a4511281652f8f6a024ca7396 (plain)
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
# pylint: skip-file
import string
import json
import os


basedir           = os.path.abspath(os.path.dirname(__file__))


class DocIDs():
    def __init__(self):
        # open doc ids for GN refs
        self.doc_ids = self.loadFile("doc_ids.json")
        # open doc ids for Diabetes references
        self.sugar_doc_ids = self.loadFile("all_files.json")
        # format is not what I prefer, it needs to be rebuilt
        self.formatDocIDs(self.sugar_doc_ids)

    def loadFile(self, file_name):
        file_path = os.path.join(basedir, file_name)
        if os.path.isfile(file_path):
            f = open(file_path, "rb")
            result = json.load(f)
            f.close()
            return result
        else:
            raise Exception("\n{0} -- File does not exist\n".format(file_path))
    
    def formatDocIDs(self, values):
        for _key, _val in values.items():
            if isinstance(_val, list):
                for theObject in _val:
                    docName = self.formatDocumentName(theObject['filename'])
                    docID   = theObject['id']
                    self.doc_ids.update({docID: docName})
                    
    def formatDocumentName(self, val):
       result = val.removesuffix('.pdf') 
       result = result.removesuffix('.txt') 
       result = result.replace('_', ' ')
       return result


    def getInfo(self, doc_id):
        if doc_id in self.doc_ids.keys():
            return self.doc_ids[doc_id]
        else:
            return doc_id

class RespContext():
    def __init__(self, context):
        self.cntxt = context
        self.theObj = {}

    def parseIntoObject(self, info):
        # check for obj, arr, or val
        for key, val in info.items():
            if isinstance(val, list):
                self.parseIntoObject(val)
            elif isinstance(val, str) or isinstance(val, int):
                self.theObj[key] = val
            self.theObj[key] = self.val


def createAccordionFromJson(theContext):
    result = ''
    # loop thru json array
    ndx = 0
    for docID, summaryLst in theContext.items():
        # item is a key with a list
        comboTxt = ''
        for entry in summaryLst:
            comboTxt += '\t' + entry['text']
    return result