File size: 3,730 Bytes
28a47b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
from pathlib import Path
from typing import Any, Dict

from model import load_embedding_from_config, load_llm_from_config
from setting import Settings
import logging
from pythonjsonlogger import jsonlogger



def verify_openai_token(token: str) -> str:
    import openai

    openai.api_key = token
    try:
        openai.Completion.create(
            model="text-ada-001",
            prompt="Hello",
            temperature=0,
            max_tokens=10,
            top_p=1,
            frequency_penalty=0.5,
            presence_penalty=0,
        )
        return "OK"
    except Exception as e:
        return str(e)

def get_logging(logger_name,content=''):
    logger = logging.getLogger(logger_name)
    if not logger.handlers:
        logger.setLevel(logging.DEBUG)
        logHandlerJson = logging.FileHandler('./memory_data/'+logger_name+'.json')
        formatter = jsonlogger.JsonFormatter()
        logHandlerJson.setFormatter(formatter)

        # handler = logging.FileHandler('./memory_data/'+logger_name+'.txt')
        # handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
        logger.addHandler(logHandlerJson)
    logger.info(content)


def verify_model_initialization(settings: Settings) -> str:
    try:
        load_llm_from_config(settings.model.llm)
    except Exception as e:
        return f"LLM initialization check failed: {e}"

    try:
        load_embedding_from_config(settings.model.embedding)
    except Exception as e:
        return f"Embedding initialization check failed: {e}"

    return "OK"


def verify_pinecone_token(token: str) -> str:
    return "OK"


def verify_discord_token(token: str) -> str:
    return "OK"


def load_json_value(filepath: Path, key: str, default_value: Any) -> Any:
    if not Path(filepath).exists():
        return default_value
    json_obj = load_json(filepath)
    if key not in json_obj:
        return default_value
    return json_obj[key]


def set_json_value(filepath: Path, key: str, value: Any) -> None:
    # key needs to follow python naming convention, such as trial_id
    json_obj = load_json(filepath)
    json_obj[key] = value
    with open(filepath, "w+") as json_file:
        json.dump(json_obj, json_file, sort_keys=True)
        json_file.flush()


def load_json(filepath: Path) -> Dict:
    if not Path(filepath).exists():
        return {}
    with open(filepath, "r") as file:
        try:
            json_obj = json.load(file)
            return json_obj
        except json.JSONDecodeError as e:
            if os.stat(filepath).st_size == 0:
                # Empty file
                return {}
            else:
                raise e

def load_log(file_name, key_name):
    content_list = []
    key_list = []
    with open('./memory_data/'+file_name) as f:
        contents = f.readlines()
        for i in contents:
            print(i)
            contents =  json.loads(i)
            content_list.append(list(contents.values())[1][key_name])
            key_list.append(list(contents.keys())[1])
    return content_list, key_list

def load_log_full(file_name, key_name):
    content_list = []
    key_list = []
    with open(file_name) as f:
        contents = f.readlines()
        for i in contents:
            #print(i)
            contents = json.loads(i)
            if key_name is None:
                content_list.append(list(contents.values())[1])
            else:
                content_list.append(list(contents.values())[1][key_name])
            key_list.append(list(contents.keys())[1])
    return content_list, key_list

def get_checkpoint_dir(agent_file: str) -> str:
    return "./{}.cpt".format(os.path.basename(agent_file))