-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.py
More file actions
77 lines (67 loc) · 2.24 KB
/
util.py
File metadata and controls
77 lines (67 loc) · 2.24 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
from numbers import Number
import json
import ollama
from collections import defaultdict
def jsonify(value):
if isinstance(value, Number):
return value
elif isinstance(value, str):
return value
elif isinstance(value, bool):
return value
elif value is None:
return value
elif isinstance(value, list):
return [jsonify(item) for item in value]
elif isinstance(value, dict):
return {str(k): jsonify(v) for k,v in value.items()}
else:
return str(value)
def write_jsonl(line, fp):
line_str = json.dumps(line)
if '\n' in line_str:
raise ValueError(f"Newline in jsonl data: {line_str}")
fp.write(line_str + '\n')
def load_jsonl(path, keys=None):
items = []
with path.open() as f:
for line in f:
item = json.loads(line)
if keys:
item = {k: item[k] for k in keys}
items.append(item)
return items
def group_records_by(data, key):
new_data = defaultdict(lambda: [])
for item in data:
new_data[item[key]].append(item)
return list(new_data.values())
def find_nth(haystack: str, needle: str, n: int) -> int:
""" https://stackoverflow.com/a/1884277 """
start = haystack.find(needle)
while start >= 0 and n > 1:
start = haystack.find(needle, start+len(needle))
n -= 1
return start
### OLLAMA STUFF ###
def get_available_models(include_generics=True):
models = sorted(ollama.list()['models'], key=lambda x: x['size'])
default_model = models[0]['name']
available_models = [m['name'] for m in models]
if include_generics:
available_models += list({m.split(':')[0] for m in available_models})
return available_models
def collect_stream(stream, do_print=True):
message = ''
for i, chunk in enumerate(stream):
if i == 0 and do_print:
print(f"{chunk['message']['role']}: ", end='')
message_chunk = chunk['message']['content']
message += message_chunk
if do_print:
end = '\n' if chunk['done'] else ''
print(message_chunk, end=end, flush=True)
return message
def print_message(m, **kwargs):
print(f"{m['role']}: {m['content']}", **kwargs)
### END OLLAMA STUFF ###