-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
94 lines (70 loc) · 2.28 KB
/
utils.py
File metadata and controls
94 lines (70 loc) · 2.28 KB
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
import requests
import json
import re
from db import VectorDB
from contextlib import suppress
def add_flows_to_rag(vdb):
"""From flows dir add each items."""
vdb.empty()
for k, v in get_populated_dict().items():
print("Adding:", k)
doc = f"""
# EXAMPLE : {k}
# Response :\n{v}
"""
vdb.add_document(k, doc)
def jgrep(text):
"""
Extracts JSON content from the given text and parses it.
Args:
text (str): The input text containing JSON within ```json ``` markers.
Returns:
dict: Parsed JSON as a Python dictionary if successful.
None: If no JSON content is found or if parsing fails.
"""
# try default load
with suppress(json.JSONDecodeError):
return json.loads(text)
pattern = r"```json\n(.*?)```"
match = re.search(pattern, text, re.DOTALL)
if match:
json_string = match.group(1)
try:
parsed_json = json.loads(json_string)
return parsed_json
except json.JSONDecodeError as e:
print(f"Error parsing JSON: {e}")
return None
else:
print("No JSON content found,returning Same content")
return text
import logging
def setup_logger(logger_name: str, log_file: str = "logs/app.log", level=logging.DEBUG):
"""
Setup logger with file and console handlers.
Args:
logger_name: Name of the logger instance
log_file: Path to log file
level: Logging level
Returns:
Logger instance configured with handlers
"""
import os
os.makedirs(os.path.dirname(log_file), exist_ok=True)
logger = logging.getLogger(logger_name)
logger.setLevel(level)
for handler in logger.handlers:
logger.removeHandler(handler)
file_handler = logging.handlers.RotatingFileHandler(log_file, maxBytes=1024 * 1024, backupCount=5)
console_handler = logging.StreamHandler()
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
console_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.addHandler(console_handler)
return logger
if __name__ == "__main__":
# Test JSON parsing
# [Test VectorDB]
vdb = VectorDB()
add_flows_to_rag(vdb)