hcli-hai 2.1.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hcli_hai/__init__.py +4 -0
- hcli_hai/__main__.py +44 -0
- hcli_hai/cli/__init__.py +0 -0
- hcli_hai/cli/ai.py +192 -0
- hcli_hai/cli/cli.py +102 -0
- hcli_hai/cli/config.py +128 -0
- hcli_hai/cli/context.py +309 -0
- hcli_hai/cli/logger.py +88 -0
- hcli_hai/cli/model.py +18 -0
- hcli_hai/cli/template.json +413 -0
- hcli_hai/cli/utils/formatting.py +14 -0
- hcli_hai/cli/utils/hutils.py +29 -0
- hcli_hai/cli/utils/summary.py +56 -0
- hcli_hai/config.py +19 -0
- hcli_hai/data/hcli_hai.1 +29 -0
- hcli_hai/hutils.py +5 -0
- hcli_hai/package.py +3 -0
- hcli_hai-2.1.2.dist-info/LICENSE.txt +19 -0
- hcli_hai-2.1.2.dist-info/METADATA +108 -0
- hcli_hai-2.1.2.dist-info/RECORD +23 -0
- hcli_hai-2.1.2.dist-info/WHEEL +5 -0
- hcli_hai-2.1.2.dist-info/entry_points.txt +2 -0
- hcli_hai-2.1.2.dist-info/top_level.txt +1 -0
hcli_hai/cli/context.py
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import io
|
|
3
|
+
import os
|
|
4
|
+
import logger
|
|
5
|
+
import tiktoken
|
|
6
|
+
import config as c
|
|
7
|
+
import resource
|
|
8
|
+
import threading
|
|
9
|
+
|
|
10
|
+
from utils import hutils
|
|
11
|
+
from utils import formatting as f
|
|
12
|
+
from utils import summary as s
|
|
13
|
+
|
|
14
|
+
logging = logger.Logger()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# We create a default context and allow for it to be initialized in a few different ways to facilitate initialization from file
|
|
18
|
+
class Context:
|
|
19
|
+
|
|
20
|
+
def __init__(self, model=None):
|
|
21
|
+
self.rlock = threading.RLock()
|
|
22
|
+
self._title = ""
|
|
23
|
+
self._name = ""
|
|
24
|
+
self._messages = [{"role": "system", "content": ""}]
|
|
25
|
+
|
|
26
|
+
# If model is provided, load it
|
|
27
|
+
if model is not None:
|
|
28
|
+
with self.rlock:
|
|
29
|
+
self.__load_model(model)
|
|
30
|
+
|
|
31
|
+
def __load_model(self, model):
|
|
32
|
+
|
|
33
|
+
# If model is a JSON string, parse it
|
|
34
|
+
if isinstance(model, str):
|
|
35
|
+
try:
|
|
36
|
+
data = json.loads(model)
|
|
37
|
+
self.__load_dict(data)
|
|
38
|
+
except json.JSONDecodeError:
|
|
39
|
+
logging.error("Invalid JSON string provided")
|
|
40
|
+
raise ValueError("Invalid JSON string provided")
|
|
41
|
+
|
|
42
|
+
# If model is already a dictionary
|
|
43
|
+
elif isinstance(model, dict):
|
|
44
|
+
self.__load_dict(model)
|
|
45
|
+
|
|
46
|
+
# If model is another object
|
|
47
|
+
else:
|
|
48
|
+
with self.rlock:
|
|
49
|
+
for key, value in vars(model).items():
|
|
50
|
+
if not key.startswith('_'): # Skip private attributes
|
|
51
|
+
setattr(self, key, value)
|
|
52
|
+
|
|
53
|
+
def __load_dict(self, data):
|
|
54
|
+
with self.rlock:
|
|
55
|
+
for key, value in data.items():
|
|
56
|
+
if not key.startswith('_'): # Skip private attributes
|
|
57
|
+
setattr(self, key, value)
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def title(self):
|
|
61
|
+
with self.rlock:
|
|
62
|
+
return self._title
|
|
63
|
+
|
|
64
|
+
@title.setter
|
|
65
|
+
def title(self, value):
|
|
66
|
+
with self.rlock:
|
|
67
|
+
self._title = value
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def name(self):
|
|
71
|
+
with self.rlock:
|
|
72
|
+
return self._name
|
|
73
|
+
|
|
74
|
+
@name.setter
|
|
75
|
+
def name(self, value):
|
|
76
|
+
with self.rlock:
|
|
77
|
+
self._name = value
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def messages(self):
|
|
81
|
+
with self.rlock:
|
|
82
|
+
# Return a deep copy to prevent external modifications while preserving the list structure
|
|
83
|
+
return [{k: v for k, v in msg.items()} for msg in self._messages]
|
|
84
|
+
|
|
85
|
+
@messages.setter
|
|
86
|
+
def messages(self, value):
|
|
87
|
+
with self.rlock:
|
|
88
|
+
# Make a deep copy when setting
|
|
89
|
+
self._messages = [{k: v for k, v in msg.items()} for msg in value]
|
|
90
|
+
|
|
91
|
+
def serialize(self):
|
|
92
|
+
with self.rlock:
|
|
93
|
+
# Create a clean dict without lock and private attributes
|
|
94
|
+
clean_dict = {
|
|
95
|
+
'title': self._title,
|
|
96
|
+
'name': self._name,
|
|
97
|
+
'messages': self._messages
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return json.dumps(clean_dict, sort_keys=True, indent=4)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class ContextManager:
|
|
104
|
+
init_lock = threading.Lock()
|
|
105
|
+
instance = None
|
|
106
|
+
|
|
107
|
+
def __new__(cls):
|
|
108
|
+
with cls.init_lock:
|
|
109
|
+
if cls.instance is None:
|
|
110
|
+
cls.instance = super().__new__(cls)
|
|
111
|
+
cls.instance.__init()
|
|
112
|
+
return cls.instance
|
|
113
|
+
|
|
114
|
+
# rlock is only initialized once but the rest of the state can be reinitialized
|
|
115
|
+
def __init(self):
|
|
116
|
+
self.rlock = threading.RLock()
|
|
117
|
+
with self.rlock:
|
|
118
|
+
self.init()
|
|
119
|
+
|
|
120
|
+
def init(self):
|
|
121
|
+
with self.rlock:
|
|
122
|
+
self.counter = TrimCounter()
|
|
123
|
+
self.config = c.Config()
|
|
124
|
+
self.context = self.get_context()
|
|
125
|
+
|
|
126
|
+
# def __count(self):
|
|
127
|
+
# with self.rlock:
|
|
128
|
+
# encoding = tiktoken.get_encoding(self.encoding_base)
|
|
129
|
+
#
|
|
130
|
+
# self.message_tokens = 0
|
|
131
|
+
# for item in self.context.messages:
|
|
132
|
+
# if "content" in item:
|
|
133
|
+
# self.message_tokens += len(encoding.encode(item["content"]))
|
|
134
|
+
#
|
|
135
|
+
# self.context_tokens = 0
|
|
136
|
+
# for item in self.context.messages:
|
|
137
|
+
# self.context_tokens += len(encoding.encode(json.dumps(item)))
|
|
138
|
+
#
|
|
139
|
+
# self.total_tokens = self.message_tokens + self.context_tokens
|
|
140
|
+
# message = "Context tokens: " + str(self.total_tokens)
|
|
141
|
+
# logging.info(message)
|
|
142
|
+
#
|
|
143
|
+
# if self.total_tokens > self.max_context_length:
|
|
144
|
+
# return True
|
|
145
|
+
#
|
|
146
|
+
# return False
|
|
147
|
+
#
|
|
148
|
+
# def trim(self):
|
|
149
|
+
# with self.rlock:
|
|
150
|
+
# while(self.__count()):
|
|
151
|
+
# self.context.messages.pop(1)
|
|
152
|
+
# message = "Context tokens: " + str(self.total_tokens) + ". Trimming the oldest entries to remain under " + str(self.max_context_length) + " tokens."
|
|
153
|
+
# logging.info(message)
|
|
154
|
+
|
|
155
|
+
def trim(self):
|
|
156
|
+
self.counter.trim(self.context)
|
|
157
|
+
|
|
158
|
+
def clear(self):
|
|
159
|
+
with self.rlock:
|
|
160
|
+
return self.config.clear()
|
|
161
|
+
|
|
162
|
+
def behavior(self, inputstream):
|
|
163
|
+
with self.rlock:
|
|
164
|
+
inputstream = inputstream.read().decode('utf-8').rstrip()
|
|
165
|
+
behavior = { "role" : "system", "content" : inputstream }
|
|
166
|
+
|
|
167
|
+
current_messages = self.context.messages
|
|
168
|
+
current_messages[0] = behavior
|
|
169
|
+
self.context.messages = current_messages
|
|
170
|
+
|
|
171
|
+
self.save()
|
|
172
|
+
|
|
173
|
+
return None
|
|
174
|
+
|
|
175
|
+
def append(self, question):
|
|
176
|
+
with self.rlock:
|
|
177
|
+
if not isinstance(question, dict) or 'role' not in question or 'content' not in question:
|
|
178
|
+
raise ValueError("Invalid message format. Expected dict with 'role' and 'content' keys")
|
|
179
|
+
|
|
180
|
+
logging.debug(question)
|
|
181
|
+
current_messages = self.context.messages
|
|
182
|
+
current_messages.append(question)
|
|
183
|
+
self.context.messages = current_messages # This ensures proper copying
|
|
184
|
+
|
|
185
|
+
def get_context(self):
|
|
186
|
+
with self.rlock:
|
|
187
|
+
self.context = self.config.get_context()
|
|
188
|
+
return self.context
|
|
189
|
+
|
|
190
|
+
# Ouput for human consumption and longstanding conversation tracking
|
|
191
|
+
def get_readable_context(self):
|
|
192
|
+
with self.rlock:
|
|
193
|
+
self.context = self.config.get_context()
|
|
194
|
+
|
|
195
|
+
if self.context is None:
|
|
196
|
+
return ""
|
|
197
|
+
|
|
198
|
+
sections = []
|
|
199
|
+
|
|
200
|
+
# Add name section
|
|
201
|
+
sections.append(f.Formatting.format("Name", self.context.name))
|
|
202
|
+
|
|
203
|
+
# Add title section
|
|
204
|
+
sections.append(f.Formatting.format("Title", self.context.title))
|
|
205
|
+
|
|
206
|
+
# Add message sections
|
|
207
|
+
for item in self.context.messages:
|
|
208
|
+
role = item.get('role', 'Unknown').capitalize()
|
|
209
|
+
content = item.get('content', '')
|
|
210
|
+
sections.append(f.Formatting.format(role, content))
|
|
211
|
+
|
|
212
|
+
return "".join(sections)
|
|
213
|
+
|
|
214
|
+
def messages(self):
|
|
215
|
+
with self.rlock:
|
|
216
|
+
return self.context.messages
|
|
217
|
+
|
|
218
|
+
def new(self):
|
|
219
|
+
with self.rlock:
|
|
220
|
+
self.context = self.config.new()
|
|
221
|
+
self.total_tokens = 0
|
|
222
|
+
|
|
223
|
+
return None
|
|
224
|
+
|
|
225
|
+
def save(self):
|
|
226
|
+
with self.rlock:
|
|
227
|
+
context_file_path = self.config.context_file_path()
|
|
228
|
+
with open(context_file_path, 'w') as f:
|
|
229
|
+
f.write(self.context.serialize())
|
|
230
|
+
|
|
231
|
+
def set(self, id):
|
|
232
|
+
with self.rlock:
|
|
233
|
+
self.config.context = id
|
|
234
|
+
self.config.save()
|
|
235
|
+
|
|
236
|
+
def name(self):
|
|
237
|
+
with self.rlock:
|
|
238
|
+
return self.context.name
|
|
239
|
+
|
|
240
|
+
def set_name(self, name):
|
|
241
|
+
with self.rlock:
|
|
242
|
+
self.context.name = name
|
|
243
|
+
self.save()
|
|
244
|
+
|
|
245
|
+
# produces a summary then a title for the current context.
|
|
246
|
+
def generate_title(self):
|
|
247
|
+
with self.rlock:
|
|
248
|
+
text = ""
|
|
249
|
+
for item in self.context.messages:
|
|
250
|
+
if "content" in item:
|
|
251
|
+
text += item["content"]
|
|
252
|
+
|
|
253
|
+
title = s.AdvancedTitleGenerator().generate_title(text)
|
|
254
|
+
logging.debug("title: " + title)
|
|
255
|
+
self.context.title = title
|
|
256
|
+
|
|
257
|
+
self.save()
|
|
258
|
+
|
|
259
|
+
return self.context.title
|
|
260
|
+
|
|
261
|
+
class TrimCounter:
|
|
262
|
+
def __init__(self):
|
|
263
|
+
self.encoding_base = "cl100k_base"
|
|
264
|
+
self.max_context_length = 200000
|
|
265
|
+
self.total_tokens = 0
|
|
266
|
+
self._encoding = tiktoken.get_encoding(self.encoding_base)
|
|
267
|
+
self._cached_encodings = {}
|
|
268
|
+
|
|
269
|
+
def get_token_counts(self, messages):
|
|
270
|
+
total_tokens = 0
|
|
271
|
+
|
|
272
|
+
for message in messages:
|
|
273
|
+
if "content" in message:
|
|
274
|
+
content = message["content"]
|
|
275
|
+
if content not in self._cached_encodings:
|
|
276
|
+
self._cached_encodings[content] = len(self._encoding.encode(content))
|
|
277
|
+
total_tokens += self._cached_encodings[content]
|
|
278
|
+
|
|
279
|
+
return {
|
|
280
|
+
"total_tokens": total_tokens,
|
|
281
|
+
"exceeds_max": total_tokens > self.max_context_length
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
def __count(self, messages):
|
|
285
|
+
counts = self.get_token_counts(messages)
|
|
286
|
+
self.total_tokens = counts["total_tokens"]
|
|
287
|
+
|
|
288
|
+
if counts["exceeds_max"]:
|
|
289
|
+
logging.warning(f"Exceeding maximum context length by {self.total_tokens - self.max_context_length} tokens")
|
|
290
|
+
|
|
291
|
+
return counts["exceeds_max"]
|
|
292
|
+
|
|
293
|
+
def trim(self, context):
|
|
294
|
+
while self.__count(context.messages):
|
|
295
|
+
if len(context.messages) > 1:
|
|
296
|
+
|
|
297
|
+
# Create new list without the second message (index 1)
|
|
298
|
+
new_messages = [context.messages[0]] + context.messages[2:]
|
|
299
|
+
context.messages = new_messages
|
|
300
|
+
|
|
301
|
+
logging.info(f"Context tokens: {self.total_tokens}. Trimming the oldest entries to remain under {self.max_context_length} tokens.")
|
|
302
|
+
else:
|
|
303
|
+
logging.warning("Cannot trim further: only system message remains")
|
|
304
|
+
break
|
|
305
|
+
|
|
306
|
+
return context.messages
|
|
307
|
+
|
|
308
|
+
def get_stats(self, context):
|
|
309
|
+
return self.get_token_counts(context.messages)
|
hcli_hai/cli/logger.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import traceback
|
|
3
|
+
import os
|
|
4
|
+
import io
|
|
5
|
+
import datetime
|
|
6
|
+
import threading
|
|
7
|
+
import collections
|
|
8
|
+
|
|
9
|
+
DEBUG = logging.DEBUG
|
|
10
|
+
INFO = logging.INFO
|
|
11
|
+
WARNING = logging.WARNING
|
|
12
|
+
ERROR = logging.ERROR
|
|
13
|
+
CRITICAL = logging.CRITICAL
|
|
14
|
+
|
|
15
|
+
# custom deque implementation to have log line limits as a sliding window that behaves like a stream being consumed when read.
|
|
16
|
+
class DequeHandler(logging.Handler):
|
|
17
|
+
def __init__(self, *args, **kwargs):
|
|
18
|
+
super().__init__(*args, **kwargs)
|
|
19
|
+
self.deque = collections.deque(maxlen=100)
|
|
20
|
+
|
|
21
|
+
def emit(self, record):
|
|
22
|
+
log_entry = self.format(record)
|
|
23
|
+
self.deque.append(log_entry)
|
|
24
|
+
|
|
25
|
+
def read_and_clear(self):
|
|
26
|
+
logs = list(self.deque)
|
|
27
|
+
self.deque.clear()
|
|
28
|
+
return logs
|
|
29
|
+
|
|
30
|
+
# custom logger implementation that allows for streaming to stdout and tailing of logs per a deque sliding window.
|
|
31
|
+
class Logger:
|
|
32
|
+
instance = None
|
|
33
|
+
streamHandler = None
|
|
34
|
+
clientHandler = None
|
|
35
|
+
lock = None
|
|
36
|
+
name = None
|
|
37
|
+
|
|
38
|
+
def __new__(cls, name=None, *args, **kwargs):
|
|
39
|
+
cls.lock = threading.Lock()
|
|
40
|
+
with cls.lock:
|
|
41
|
+
if cls.instance is None:
|
|
42
|
+
cls.instance = super().__new__(cls, *args, **kwargs)
|
|
43
|
+
cls.instance.init(name, *args, **kwargs)
|
|
44
|
+
return cls.instance
|
|
45
|
+
|
|
46
|
+
def init(self, name=None, *args, **kwargs):
|
|
47
|
+
self.name = "hai"
|
|
48
|
+
self.instance = logging.getLogger(self.name)
|
|
49
|
+
|
|
50
|
+
date_format = "%Y-%m-%d %H:%M:%S %z"
|
|
51
|
+
message_format = "[%(asctime)s] [%(levelname)-8s] [%(name)s] [%(filename)13s:%(lineno)-3s] %(message)s"
|
|
52
|
+
|
|
53
|
+
formatter = logging.Formatter(fmt=message_format, datefmt=date_format)
|
|
54
|
+
|
|
55
|
+
self.streamHandler = logging.StreamHandler()
|
|
56
|
+
self.streamHandler.setFormatter(formatter)
|
|
57
|
+
self.instance.addHandler(self.streamHandler)
|
|
58
|
+
|
|
59
|
+
self.clientHandler = DequeHandler()
|
|
60
|
+
self.clientHandler.setFormatter(formatter)
|
|
61
|
+
self.instance.addHandler(self.clientHandler)
|
|
62
|
+
|
|
63
|
+
def setLevel(self, level):
|
|
64
|
+
self.instance.setLevel(level)
|
|
65
|
+
|
|
66
|
+
def info(self, msg, *args, **kwargs):
|
|
67
|
+
self.instance.info(msg, *args, stacklevel=2, **kwargs)
|
|
68
|
+
|
|
69
|
+
def debug(self, msg, *args, **kwargs):
|
|
70
|
+
self.instance.debug(msg, *args, stacklevel=2, **kwargs)
|
|
71
|
+
|
|
72
|
+
def warning(self, msg, *args, **kwargs):
|
|
73
|
+
self.instance.warning(msg, *args, stacklevel=2, **kwargs)
|
|
74
|
+
|
|
75
|
+
def error(self, msg, *args, **kwargs):
|
|
76
|
+
self.instance.error(msg, *args, stacklevel=2, **kwargs)
|
|
77
|
+
|
|
78
|
+
def critical(self, msg, *args, **kwargs):
|
|
79
|
+
self.instance.critical(msg, *args, stacklevel=2, **kwargs)
|
|
80
|
+
|
|
81
|
+
def tail(self):
|
|
82
|
+
log_entries = self.clientHandler.read_and_clear()
|
|
83
|
+
log_text = '\n'.join(log_entries)
|
|
84
|
+
|
|
85
|
+
if log_text:
|
|
86
|
+
log_text += '\n' # Adds newline character at the end if there is some log_text
|
|
87
|
+
|
|
88
|
+
return log_text.encode('utf-8')
|
hcli_hai/cli/model.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
models = {
|
|
2
|
+
"claude-3-opus-20240229": {
|
|
3
|
+
'model' : "claude-3-opus-20240229",
|
|
4
|
+
'max_tokens' : 4096
|
|
5
|
+
},
|
|
6
|
+
"claude-3-haiku-20240307": {
|
|
7
|
+
'model' : "claude-3-haiku-20240307",
|
|
8
|
+
'max_tokens' : 2048
|
|
9
|
+
},
|
|
10
|
+
"claude-3-5-sonnet-20241022": {
|
|
11
|
+
'model' : "claude-3-5-sonnet-20241022",
|
|
12
|
+
'max_tokens' : 4096
|
|
13
|
+
},
|
|
14
|
+
"claude-3-5-sonnet-20240620": {
|
|
15
|
+
'model' : "claude-3-5-sonnet-20240620",
|
|
16
|
+
'max_tokens' : 4096
|
|
17
|
+
}
|
|
18
|
+
}
|