-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
72 lines (55 loc) · 2.06 KB
/
helpers.py
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
from typing import Dict, Any, Optional
import yaml
import json
import base64
import re
def read_config(config_path = 'config.yaml'):
with open(config_path, 'r') as file:
config = yaml.safe_load(file)
return config
def save_to_json(data, file_path):
with open(file_path, "w", encoding="utf-8")as fp:
json.dump(data, fp)
def read_json(file_path):
with open(file_path, "r", encoding="utf-8")as fp:
return json.load(fp)
def singleton(cls):
instances = {}
def get_instance(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return get_instance
def encode_image(image_file):
return base64.b64encode(image_file.getvalue()).decode('utf-8')
def generator_simulator(content: str):
for token in content.split(" "):
yield token + " "
def decode_and_reverse_password(encoded_password):
decoded_bytes = base64.b64decode(encoded_password)
decoded_password = decoded_bytes.decode('utf-8')
reversed_password = decoded_password[::-1]
return reversed_password
@staticmethod
def extract_json_content(response: str) -> Optional[Dict[str, Any]]:
"""Extract and parse JSON content from LLM response."""
# Try to find content within code blocks first
code_block_pattern = r'\`\`\`(?:json|yml)?\n([\s\S]*?)\n?\`\`\`'
code_blocks = re.findall(code_block_pattern, response, re.DOTALL)
try:
if code_blocks:
parsed_json = json.loads(code_blocks[0])
else:
# If no code blocks found, try to parse the entire response as YAML
parsed_json = json.loads(response)
# If the parsed result is a list, merge all dictionaries in the list
if isinstance(parsed_json, list):
return parsed_json
return parsed_json if isinstance(parsed_json, dict) else None
except yaml.YAMLError:
return None
if __name__ == "__main__":
content= """```json\n[1, 2, 3]```"""
e = extract_json_content(content)
print(e)
print(type(e))