hcli-hai 1.5.2__tar.gz → 2.0.0__tar.gz
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-1.5.2/hcli_hai.egg-info → hcli_hai-2.0.0}/PKG-INFO +1 -2
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai/cli/ai.py +47 -33
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai/cli/cli.py +16 -3
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai/cli/config.py +47 -1
- hcli_hai-2.0.0/hcli_hai/cli/context.py +309 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai/cli/model.py +4 -9
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai/cli/template.json +59 -0
- hcli_hai-2.0.0/hcli_hai/cli/utils/formatting.py +14 -0
- {hcli_hai-1.5.2/hcli_hai/cli → hcli_hai-2.0.0/hcli_hai/cli/utils}/summary.py +3 -0
- hcli_hai-2.0.0/hcli_hai/package.py +3 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0/hcli_hai.egg-info}/PKG-INFO +1 -2
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai.egg-info/SOURCES.txt +3 -10
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai.egg-info/requires.txt +0 -1
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/requirements.txt +0 -1
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/tests/test_hai.py +16 -5
- hcli_hai-1.5.2/hcli_hai/cli/__pycache__/ai.cpython-311.pyc +0 -0
- hcli_hai-1.5.2/hcli_hai/cli/__pycache__/cli.cpython-311.pyc +0 -0
- hcli_hai-1.5.2/hcli_hai/cli/__pycache__/config.cpython-311.pyc +0 -0
- hcli_hai-1.5.2/hcli_hai/cli/__pycache__/context.cpython-311.pyc +0 -0
- hcli_hai-1.5.2/hcli_hai/cli/__pycache__/hutils.cpython-311.pyc +0 -0
- hcli_hai-1.5.2/hcli_hai/cli/__pycache__/logger.cpython-311.pyc +0 -0
- hcli_hai-1.5.2/hcli_hai/cli/__pycache__/model.cpython-311.pyc +0 -0
- hcli_hai-1.5.2/hcli_hai/cli/__pycache__/summary.cpython-311.pyc +0 -0
- hcli_hai-1.5.2/hcli_hai/cli/context.py +0 -167
- hcli_hai-1.5.2/hcli_hai/package.py +0 -4
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/LICENSE.txt +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/MANIFEST.in +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/README.rst +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai/__init__.py +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai/__main__.py +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai/cli/__init__.py +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai/cli/logger.py +0 -0
- {hcli_hai-1.5.2/hcli_hai/cli → hcli_hai-2.0.0/hcli_hai/cli/utils}/hutils.py +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai/config.py +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai/data/hcli_hai.1 +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai/hutils.py +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai.egg-info/dependency_links.txt +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai.egg-info/entry_points.txt +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/hcli_hai.egg-info/top_level.txt +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/pyproject.toml +0 -0
- {hcli_hai-1.5.2 → hcli_hai-2.0.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: hcli_hai
|
|
3
|
-
Version:
|
|
3
|
+
Version: 2.0.0
|
|
4
4
|
Summary: HCLI hai is a python package wrapper that contains an HCLI sample application (hai); hai is an HCLI for interacting with Anthropic's Claude or OpenAI's GPT AI models via terminal input and output streams.
|
|
5
5
|
Author-email: Jeff Michaud <cometaj2@comcast.net>
|
|
6
6
|
License: MIT
|
|
@@ -15,7 +15,6 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.12
|
|
16
16
|
Description-Content-Type: text/x-rst
|
|
17
17
|
License-File: LICENSE.txt
|
|
18
|
-
Requires-Dist: openai==1.7.0
|
|
19
18
|
Requires-Dist: tiktoken==0.8.0
|
|
20
19
|
Requires-Dist: anthropic==0.36.2
|
|
21
20
|
|
|
@@ -6,48 +6,47 @@ import inspect
|
|
|
6
6
|
import traceback
|
|
7
7
|
import logger
|
|
8
8
|
import config
|
|
9
|
-
import context
|
|
9
|
+
import context as c
|
|
10
10
|
import config
|
|
11
11
|
import uuid
|
|
12
12
|
import shutil
|
|
13
13
|
|
|
14
14
|
from datetime import datetime
|
|
15
|
-
from openai import OpenAI
|
|
16
15
|
from anthropic import Anthropic
|
|
17
16
|
from model import models
|
|
18
17
|
|
|
18
|
+
|
|
19
19
|
logging = logger.Logger()
|
|
20
20
|
|
|
21
21
|
|
|
22
22
|
class AI:
|
|
23
23
|
config = None
|
|
24
|
-
|
|
24
|
+
contextmgr = None
|
|
25
25
|
|
|
26
26
|
def __init__(self):
|
|
27
27
|
self.config = config.Config()
|
|
28
|
-
self.
|
|
28
|
+
self.contextmgr = c.ContextManager()
|
|
29
29
|
|
|
30
30
|
def chat(self, inputstream):
|
|
31
31
|
if self.config.model is not None:
|
|
32
32
|
inputstream = inputstream.read().decode('utf-8')
|
|
33
33
|
if inputstream != "":
|
|
34
|
+
inputstream = inputstream.rstrip()
|
|
34
35
|
question = { "role" : "user", "content" : inputstream }
|
|
35
|
-
self.
|
|
36
|
-
self.
|
|
36
|
+
self.contextmgr.append(question)
|
|
37
|
+
self.contextmgr.trim()
|
|
38
|
+
|
|
39
|
+
tokens = self.contextmgr.counter.get_stats(self.contextmgr.context)
|
|
40
|
+
logging.info("Request - total context tokens: " + str(tokens['total_tokens']))
|
|
37
41
|
|
|
38
|
-
if self.
|
|
42
|
+
if self.contextmgr.counter.total_tokens != 0:
|
|
39
43
|
try:
|
|
40
|
-
#client = OpenAI(api_key=os.environ['OPENAI_API_KEY'])
|
|
41
|
-
#response = client.chat.completions.create(
|
|
42
|
-
# **model.openai_baseline,
|
|
43
|
-
# messages=self.context
|
|
44
|
-
# )
|
|
45
44
|
client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
|
|
46
45
|
|
|
47
46
|
# Separate system message from user messages
|
|
48
47
|
model = models[self.config.model]
|
|
49
|
-
system_message = next((msg["content"] for msg in self.
|
|
50
|
-
user_messages = [msg for msg in self.
|
|
48
|
+
system_message = next((msg["content"] for msg in self.contextmgr.messages() if msg["role"] == "system"), "")
|
|
49
|
+
user_messages = [msg for msg in self.contextmgr.messages() if msg["role"] != "system"]
|
|
51
50
|
|
|
52
51
|
response = client.messages.create(
|
|
53
52
|
**model,
|
|
@@ -61,39 +60,44 @@ class AI:
|
|
|
61
60
|
logging.error(traceback.format_exc())
|
|
62
61
|
return None
|
|
63
62
|
else:
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
warning = "The token trim backoff completely collapsed. This means that the stream was too large to fit within the total allowable context limit of " + str(self.contextmgr.counter.max_context_length) + " tokens, and the last trimming operation ended up completely wiping out the remaining conversation context."
|
|
64
|
+
logging.warning(warning)
|
|
65
|
+
self.contextmgr.save()
|
|
66
|
+
|
|
67
|
+
return warning
|
|
66
68
|
|
|
67
|
-
# # Output for context retention
|
|
68
|
-
# output_response = response.choices[0].message
|
|
69
69
|
output_response = response
|
|
70
70
|
|
|
71
71
|
# Extract the text content from the response
|
|
72
72
|
output_content = " ".join([block.text for block in output_response.content if block.type == 'text'])
|
|
73
73
|
|
|
74
|
-
self.
|
|
74
|
+
self.contextmgr.append({ "role" : output_response.role, "content" : output_content})
|
|
75
|
+
|
|
76
|
+
tokens = self.contextmgr.counter.get_stats(self.contextmgr.context)
|
|
77
|
+
logging.info("Response - total context tokens: " + str(tokens['total_tokens']))
|
|
75
78
|
|
|
76
79
|
output = output_content
|
|
77
|
-
output = output + "\n"
|
|
78
80
|
|
|
79
|
-
self.
|
|
80
|
-
self.
|
|
81
|
+
self.contextmgr.generate_title()
|
|
82
|
+
self.contextmgr.save()
|
|
81
83
|
|
|
82
|
-
return
|
|
84
|
+
return output
|
|
83
85
|
else:
|
|
84
|
-
|
|
86
|
+
warning = "No model selected. Select one from the list of models."
|
|
87
|
+
logging.warning(warning)
|
|
88
|
+
return warning
|
|
85
89
|
|
|
86
90
|
def get_context(self):
|
|
87
|
-
return self.
|
|
91
|
+
return self.contextmgr.get_context()
|
|
88
92
|
|
|
89
93
|
def get_readable_context(self):
|
|
90
|
-
return self.
|
|
94
|
+
return self.contextmgr.get_readable_context()
|
|
91
95
|
|
|
92
96
|
def clear(self):
|
|
93
|
-
self.
|
|
97
|
+
self.contextmgr.clear()
|
|
94
98
|
|
|
95
99
|
def behavior(self, inputstream):
|
|
96
|
-
self.
|
|
100
|
+
self.contextmgr.behavior(inputstream)
|
|
97
101
|
|
|
98
102
|
def ls(self):
|
|
99
103
|
contexts = []
|
|
@@ -109,12 +113,14 @@ class AI:
|
|
|
109
113
|
if os.path.exists(context_file):
|
|
110
114
|
try:
|
|
111
115
|
with open(context_file, 'r') as f:
|
|
112
|
-
|
|
113
|
-
title =
|
|
116
|
+
context = c.Context(json.load(f))
|
|
117
|
+
title = context.title
|
|
118
|
+
name = context.name
|
|
114
119
|
update_time = os.path.getmtime(context_file)
|
|
115
120
|
contexts.append({
|
|
116
121
|
'context_id': item,
|
|
117
122
|
'title': title,
|
|
123
|
+
'name': name,
|
|
118
124
|
'update_time': update_time
|
|
119
125
|
})
|
|
120
126
|
except json.JSONDecodeError:
|
|
@@ -122,13 +128,15 @@ class AI:
|
|
|
122
128
|
contexts.append({
|
|
123
129
|
'context_id': item,
|
|
124
130
|
'title': 'N/A',
|
|
131
|
+
'name': 'N/A',
|
|
125
132
|
'update_time': os.path.getmtime(context_file) if os.path.exists(context_file) else 0
|
|
126
133
|
})
|
|
127
134
|
else:
|
|
128
|
-
# If context.json doesn't exist, use the directory name
|
|
135
|
+
# If context.json doesn't exist, use the directory name only
|
|
129
136
|
contexts.append({
|
|
130
137
|
'context_id': item,
|
|
131
138
|
'title': 'N/A',
|
|
139
|
+
'name': 'N/A',
|
|
132
140
|
'update_time': 0
|
|
133
141
|
})
|
|
134
142
|
|
|
@@ -145,7 +153,7 @@ class AI:
|
|
|
145
153
|
contexts = self.ls()
|
|
146
154
|
context_ids = [context['context_id'] for context in contexts]
|
|
147
155
|
if id in context_ids:
|
|
148
|
-
self.
|
|
156
|
+
self.contextmgr.set(id)
|
|
149
157
|
else:
|
|
150
158
|
logging.warning("provided context id is not found in available contexts.")
|
|
151
159
|
|
|
@@ -153,7 +161,7 @@ class AI:
|
|
|
153
161
|
if os.path.exists(self.config.dot_hai_config_file):
|
|
154
162
|
os.remove(self.config.dot_hai_config_file)
|
|
155
163
|
self.config.init()
|
|
156
|
-
self.
|
|
164
|
+
self.contextmgr.init()
|
|
157
165
|
|
|
158
166
|
return self.current()
|
|
159
167
|
|
|
@@ -176,3 +184,9 @@ class AI:
|
|
|
176
184
|
models = self.list_models()
|
|
177
185
|
if model in models:
|
|
178
186
|
self.config.model = model
|
|
187
|
+
|
|
188
|
+
def name(self):
|
|
189
|
+
return self.contextmgr.name()
|
|
190
|
+
|
|
191
|
+
def set_name(self, name):
|
|
192
|
+
self.contextmgr.set_name(name)
|
|
@@ -8,7 +8,6 @@ import tiktoken
|
|
|
8
8
|
import logger
|
|
9
9
|
import ai
|
|
10
10
|
|
|
11
|
-
from openai import OpenAI
|
|
12
11
|
from anthropic import Anthropic
|
|
13
12
|
|
|
14
13
|
logging = logger.Logger()
|
|
@@ -27,7 +26,8 @@ class CLI:
|
|
|
27
26
|
def execute(self):
|
|
28
27
|
if len(self.commands) == 1:
|
|
29
28
|
if self.inputstream is not None:
|
|
30
|
-
|
|
29
|
+
response = self.ai.chat(self.inputstream)
|
|
30
|
+
return io.BytesIO(response.encode('utf-8') + "\n".encode('utf-8'))
|
|
31
31
|
|
|
32
32
|
if self.commands[1] == "clear":
|
|
33
33
|
self.ai.clear()
|
|
@@ -36,7 +36,7 @@ class CLI:
|
|
|
36
36
|
if self.commands[1] == "context":
|
|
37
37
|
if len(self.commands) == 2:
|
|
38
38
|
context = self.ai.get_context()
|
|
39
|
-
return io.BytesIO(
|
|
39
|
+
return io.BytesIO(context.serialize().encode('utf-8') + "\n".encode('utf-8'))
|
|
40
40
|
if len(self.commands) == 3:
|
|
41
41
|
readable_context = self.ai.get_readable_context()
|
|
42
42
|
return io.BytesIO(readable_context.encode('utf-8') + "\n".encode('utf-8'))
|
|
@@ -58,6 +58,19 @@ class CLI:
|
|
|
58
58
|
self.ai.behavior(self.inputstream)
|
|
59
59
|
return
|
|
60
60
|
|
|
61
|
+
if self.commands[1] == "name":
|
|
62
|
+
if len(self.commands) == 2:
|
|
63
|
+
name = self.ai.name()
|
|
64
|
+
if name is not None:
|
|
65
|
+
return io.BytesIO(name.encode('utf-8') + "\n".encode('utf-8'))
|
|
66
|
+
else:
|
|
67
|
+
return io.BytesIO(b"None\n")
|
|
68
|
+
|
|
69
|
+
if len(self.commands) == 4:
|
|
70
|
+
if self.commands[2] == "set":
|
|
71
|
+
self.ai.set_name(self.commands[3])
|
|
72
|
+
return None
|
|
73
|
+
|
|
61
74
|
if self.commands[1] == "model":
|
|
62
75
|
if len(self.commands) == 2:
|
|
63
76
|
model = self.ai.model()
|
|
@@ -5,7 +5,7 @@ from os.path import isfile, join, isdir
|
|
|
5
5
|
from os import path, listdir
|
|
6
6
|
from model import models
|
|
7
7
|
|
|
8
|
-
import hutils
|
|
8
|
+
from utils import hutils
|
|
9
9
|
|
|
10
10
|
import os
|
|
11
11
|
import sys
|
|
@@ -13,6 +13,7 @@ import shutil
|
|
|
13
13
|
import json
|
|
14
14
|
import logger
|
|
15
15
|
import uuid
|
|
16
|
+
import context as c
|
|
16
17
|
|
|
17
18
|
logging = logger.Logger()
|
|
18
19
|
|
|
@@ -77,6 +78,51 @@ class Config:
|
|
|
77
78
|
with open(self.dot_hai_config_file, "w") as config:
|
|
78
79
|
self.parser.write(config)
|
|
79
80
|
|
|
81
|
+
def get_context(self):
|
|
82
|
+
context_file_path = self.context_file_path()
|
|
83
|
+
if os.path.exists(context_file_path):
|
|
84
|
+
try:
|
|
85
|
+
with open(context_file_path, 'r') as f:
|
|
86
|
+
context = c.Context(json.load(f))
|
|
87
|
+
return context
|
|
88
|
+
except:
|
|
89
|
+
return self.new()
|
|
90
|
+
else:
|
|
91
|
+
return self.new()
|
|
92
|
+
|
|
93
|
+
return None
|
|
94
|
+
|
|
95
|
+
def new(self):
|
|
96
|
+
share = self.dot_hai_context
|
|
97
|
+
current_context = self.dot_hai_context + "/" + self.context
|
|
98
|
+
|
|
99
|
+
if not os.path.exists(share):
|
|
100
|
+
hutils.create_folder(share)
|
|
101
|
+
|
|
102
|
+
if not os.path.exists(current_context):
|
|
103
|
+
hutils.create_folder(current_context)
|
|
104
|
+
|
|
105
|
+
context_file_path = self.context_file_path()
|
|
106
|
+
if not os.path.exists(context_file_path):
|
|
107
|
+
with open(context_file_path, 'w') as f:
|
|
108
|
+
context = c.Context()
|
|
109
|
+
f.write(context.serialize())
|
|
110
|
+
return context
|
|
111
|
+
|
|
112
|
+
return None
|
|
113
|
+
|
|
114
|
+
def clear(self):
|
|
115
|
+
context_file_path = self.context_file_path()
|
|
116
|
+
if os.path.exists(context_file_path):
|
|
117
|
+
os.remove(context_file_path)
|
|
118
|
+
return self.new()
|
|
119
|
+
|
|
120
|
+
return None
|
|
121
|
+
|
|
122
|
+
def context_file_path(self):
|
|
123
|
+
return os.path.join(self.dot_hai_context, self.context, "context.json")
|
|
124
|
+
|
|
80
125
|
def list_models(self):
|
|
81
126
|
model_names = list(models.keys())
|
|
127
|
+
model_names.sort()
|
|
82
128
|
return model_names
|
|
@@ -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)
|
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
models = {
|
|
2
|
-
"gpt-3-5-turbo": {
|
|
3
|
-
'model' : "gpt-3.5-turbo",
|
|
4
|
-
'temperature' : 0.7,
|
|
5
|
-
'max_tokens' : 2048,
|
|
6
|
-
'n' : 1,
|
|
7
|
-
'stop' : None,
|
|
8
|
-
'frequency_penalty' : 0.5,
|
|
9
|
-
'presence_penalty' : 0.5
|
|
10
|
-
},
|
|
11
2
|
"claude-3-opus-20240229": {
|
|
12
3
|
'model' : "claude-3-opus-20240229",
|
|
13
4
|
'max_tokens' : 4096
|
|
@@ -16,6 +7,10 @@ models = {
|
|
|
16
7
|
'model' : "claude-3-haiku-20240307",
|
|
17
8
|
'max_tokens' : 2048
|
|
18
9
|
},
|
|
10
|
+
"claude-3-5-sonnet-20241022": {
|
|
11
|
+
'model' : "claude-3-5-sonnet-20241022",
|
|
12
|
+
'max_tokens' : 4096
|
|
13
|
+
},
|
|
19
14
|
"claude-3-5-sonnet-20240620": {
|
|
20
15
|
'model' : "claude-3-5-sonnet-20240620",
|
|
21
16
|
'max_tokens' : 4096
|
|
@@ -13,6 +13,14 @@
|
|
|
13
13
|
"command": "hai new",
|
|
14
14
|
"http": "post"
|
|
15
15
|
},
|
|
16
|
+
{
|
|
17
|
+
"command": "hai name",
|
|
18
|
+
"http": "get"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"command": "hai name set {p}",
|
|
22
|
+
"http": "post"
|
|
23
|
+
},
|
|
16
24
|
{
|
|
17
25
|
"command": "hai context",
|
|
18
26
|
"http": "get"
|
|
@@ -92,6 +100,11 @@
|
|
|
92
100
|
"name": "context",
|
|
93
101
|
"description": "The \"context\" command allows you to view the current context."
|
|
94
102
|
},
|
|
103
|
+
{
|
|
104
|
+
"href": "hainame",
|
|
105
|
+
"name": "name",
|
|
106
|
+
"description": "The \"name\" command allows you to view the user provided name for the current context."
|
|
107
|
+
},
|
|
95
108
|
{
|
|
96
109
|
"href": "haicurrent",
|
|
97
110
|
"name": "current",
|
|
@@ -229,6 +242,52 @@
|
|
|
229
242
|
}
|
|
230
243
|
]
|
|
231
244
|
},
|
|
245
|
+
{
|
|
246
|
+
"id": "hainame",
|
|
247
|
+
"name": "name",
|
|
248
|
+
"section": [
|
|
249
|
+
{
|
|
250
|
+
"name": "name",
|
|
251
|
+
"description": "name - name of the current context."
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
"name": "synopsis",
|
|
255
|
+
"description": "hai name"
|
|
256
|
+
},
|
|
257
|
+
{
|
|
258
|
+
"name": "description",
|
|
259
|
+
"description": "The \"name\" command allows you to view the user provided name for the current context."
|
|
260
|
+
}
|
|
261
|
+
],
|
|
262
|
+
"command": [
|
|
263
|
+
{
|
|
264
|
+
"href": "hainameset",
|
|
265
|
+
"name": "set",
|
|
266
|
+
"description": "The \"set\" command allows you to set the user provided name for the current context."
|
|
267
|
+
}
|
|
268
|
+
]
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
"id": "hainameset",
|
|
272
|
+
"name": "set",
|
|
273
|
+
"section": [
|
|
274
|
+
{
|
|
275
|
+
"name": "name",
|
|
276
|
+
"description": "set - sets the name for the current context."
|
|
277
|
+
},
|
|
278
|
+
{
|
|
279
|
+
"name": "synopsis",
|
|
280
|
+
"description": "hai name set <user provided name>"
|
|
281
|
+
},
|
|
282
|
+
{
|
|
283
|
+
"name": "description",
|
|
284
|
+
"description": "The \"set\" command allows you to set the user provided name for the current context."
|
|
285
|
+
}
|
|
286
|
+
],
|
|
287
|
+
"parameter": {
|
|
288
|
+
"href": "hainamesetparameter"
|
|
289
|
+
}
|
|
290
|
+
},
|
|
232
291
|
{
|
|
233
292
|
"id": "haicontext",
|
|
234
293
|
"name": "context",
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class Formatting:
|
|
2
|
+
SEPARATOR = "----"
|
|
3
|
+
NEWLINES = "\n\n"
|
|
4
|
+
SECTION_TEMPLATE = "{separator}{name}:{newlines}{content}{newlines}"
|
|
5
|
+
|
|
6
|
+
@classmethod
|
|
7
|
+
def format(cls, name, content):
|
|
8
|
+
return cls.SECTION_TEMPLATE.format(
|
|
9
|
+
separator=cls.SEPARATOR,
|
|
10
|
+
name=name,
|
|
11
|
+
content=content,
|
|
12
|
+
newlines=cls.NEWLINES
|
|
13
|
+
)
|
|
14
|
+
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import re
|
|
2
|
+
|
|
2
3
|
from collections import defaultdict
|
|
3
4
|
from heapq import nlargest
|
|
4
5
|
from typing import List, Dict
|
|
5
6
|
|
|
7
|
+
|
|
6
8
|
class AdvancedTitleGenerator:
|
|
7
9
|
def __init__(self):
|
|
8
10
|
pass
|
|
@@ -40,6 +42,7 @@ class AdvancedTitleGenerator:
|
|
|
40
42
|
summary = ' '.join([sentences[i] for i in sorted(summary_sentences)])
|
|
41
43
|
return summary
|
|
42
44
|
|
|
45
|
+
# produces a summary then a title for the current context that's at most 10 words long from a limited bit of conversation context.
|
|
43
46
|
def generate_title(self, text: str, max_words: int = 20) -> str:
|
|
44
47
|
# Generate a summary
|
|
45
48
|
summary = self.generate_summary(text, num_sentences=1)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: hcli_hai
|
|
3
|
-
Version:
|
|
3
|
+
Version: 2.0.0
|
|
4
4
|
Summary: HCLI hai is a python package wrapper that contains an HCLI sample application (hai); hai is an HCLI for interacting with Anthropic's Claude or OpenAI's GPT AI models via terminal input and output streams.
|
|
5
5
|
Author-email: Jeff Michaud <cometaj2@comcast.net>
|
|
6
6
|
License: MIT
|
|
@@ -15,7 +15,6 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.12
|
|
16
16
|
Description-Content-Type: text/x-rst
|
|
17
17
|
License-File: LICENSE.txt
|
|
18
|
-
Requires-Dist: openai==1.7.0
|
|
19
18
|
Requires-Dist: tiktoken==0.8.0
|
|
20
19
|
Requires-Dist: anthropic==0.36.2
|
|
21
20
|
|
|
@@ -20,18 +20,11 @@ hcli_hai/cli/ai.py
|
|
|
20
20
|
hcli_hai/cli/cli.py
|
|
21
21
|
hcli_hai/cli/config.py
|
|
22
22
|
hcli_hai/cli/context.py
|
|
23
|
-
hcli_hai/cli/hutils.py
|
|
24
23
|
hcli_hai/cli/logger.py
|
|
25
24
|
hcli_hai/cli/model.py
|
|
26
|
-
hcli_hai/cli/summary.py
|
|
27
25
|
hcli_hai/cli/template.json
|
|
28
|
-
hcli_hai/cli/
|
|
29
|
-
hcli_hai/cli/
|
|
30
|
-
hcli_hai/cli/
|
|
31
|
-
hcli_hai/cli/__pycache__/context.cpython-311.pyc
|
|
32
|
-
hcli_hai/cli/__pycache__/hutils.cpython-311.pyc
|
|
33
|
-
hcli_hai/cli/__pycache__/logger.cpython-311.pyc
|
|
34
|
-
hcli_hai/cli/__pycache__/model.cpython-311.pyc
|
|
35
|
-
hcli_hai/cli/__pycache__/summary.cpython-311.pyc
|
|
26
|
+
hcli_hai/cli/utils/formatting.py
|
|
27
|
+
hcli_hai/cli/utils/hutils.py
|
|
28
|
+
hcli_hai/cli/utils/summary.py
|
|
36
29
|
hcli_hai/data/hcli_hai.1
|
|
37
30
|
tests/test_hai.py
|
|
@@ -8,25 +8,36 @@ def test_function():
|
|
|
8
8
|
#!/bin/bash
|
|
9
9
|
set -x
|
|
10
10
|
|
|
11
|
-
gunicorn --workers=1 --threads=1 "hcli_core:connector(\\"`hcli_hai path`\\")" --daemon
|
|
11
|
+
gunicorn --workers=1 --threads=1 "hcli_core:connector(\\"`hcli_hai path`\\")" --error-log ./error.log --daemon
|
|
12
|
+
sleep 3
|
|
12
13
|
huckle cli install http://127.0.0.1:8000
|
|
14
|
+
|
|
15
|
+
cat ./error.log
|
|
13
16
|
"""
|
|
14
17
|
|
|
15
|
-
p1 = subprocess.Popen(['bash', '-c', setup], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
18
|
+
p1 = subprocess.Popen(['bash', '-c', setup], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
16
19
|
out, err = p1.communicate()
|
|
17
20
|
|
|
21
|
+
error = err.decode('utf-8')
|
|
22
|
+
result = out.decode('utf-8')
|
|
23
|
+
print(error)
|
|
24
|
+
print(result)
|
|
25
|
+
|
|
18
26
|
hello = """
|
|
19
27
|
#!/bin/bash
|
|
20
28
|
set -x
|
|
21
29
|
|
|
22
|
-
|
|
30
|
+
eval $(huckle env)
|
|
23
31
|
hai new > /dev/null 2>&1
|
|
24
32
|
hai context
|
|
25
33
|
kill $(ps aux | grep '[g]unicorn' | awk '{print $2}')
|
|
26
34
|
"""
|
|
27
35
|
|
|
28
|
-
p2 = subprocess.Popen(['bash', '-c', hello], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
36
|
+
p2 = subprocess.Popen(['bash', '-c', hello], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
29
37
|
out, err = p2.communicate()
|
|
38
|
+
error = err.decode('utf-8')
|
|
30
39
|
result = out.decode('utf-8')
|
|
40
|
+
print(error)
|
|
41
|
+
print(result)
|
|
31
42
|
|
|
32
|
-
assert(result == '{\n "
|
|
43
|
+
assert(result == '{\n "messages": [\n {\n "content": "",\n "role": "system"\n }\n ],\n "name": "",\n "title": ""\n}\n')
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
import json
|
|
2
|
-
import io
|
|
3
|
-
import os
|
|
4
|
-
import logger
|
|
5
|
-
import tiktoken
|
|
6
|
-
import config as c
|
|
7
|
-
import hutils
|
|
8
|
-
import summary as s
|
|
9
|
-
|
|
10
|
-
logging = logger.Logger()
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class Context:
|
|
14
|
-
title = None
|
|
15
|
-
message_tokens = 0
|
|
16
|
-
context_tokens = 0
|
|
17
|
-
total_tokens = 0
|
|
18
|
-
max_context_length = 10000
|
|
19
|
-
encoding_base = "p50k_base"
|
|
20
|
-
context = None
|
|
21
|
-
context_file = None
|
|
22
|
-
config = None
|
|
23
|
-
summary = None
|
|
24
|
-
instance = None
|
|
25
|
-
|
|
26
|
-
def __new__(cls):
|
|
27
|
-
if cls.instance is None:
|
|
28
|
-
cls.instance = super().__new__(cls)
|
|
29
|
-
cls.instance.init()
|
|
30
|
-
return cls.instance
|
|
31
|
-
|
|
32
|
-
def init(self):
|
|
33
|
-
self.config = c.Config()
|
|
34
|
-
self.context_file = self.config.dot_hai_context + "/" + self.config.context + "/context.json"
|
|
35
|
-
self.summary = s.AdvancedTitleGenerator()
|
|
36
|
-
self.get_context()
|
|
37
|
-
|
|
38
|
-
def count(self):
|
|
39
|
-
encoding = tiktoken.get_encoding(self.encoding_base)
|
|
40
|
-
|
|
41
|
-
self.message_tokens = 0
|
|
42
|
-
for item in self.context:
|
|
43
|
-
if "content" in item:
|
|
44
|
-
self.message_tokens += len(encoding.encode(item["content"]))
|
|
45
|
-
|
|
46
|
-
self.context_tokens = 0
|
|
47
|
-
for item in self.context:
|
|
48
|
-
self.context_tokens += len(encoding.encode(json.dumps(item)))
|
|
49
|
-
|
|
50
|
-
self.total_tokens = self.message_tokens + self.context_tokens
|
|
51
|
-
message = "Context tokens: " + str(self.total_tokens)
|
|
52
|
-
logging.info(message)
|
|
53
|
-
|
|
54
|
-
if self.total_tokens > self.max_context_length:
|
|
55
|
-
return True
|
|
56
|
-
|
|
57
|
-
return False
|
|
58
|
-
|
|
59
|
-
def trim(self):
|
|
60
|
-
while(self.count()):
|
|
61
|
-
self.context.pop(1)
|
|
62
|
-
message = "Context tokens: " + str(self.total_tokens) + ". Trimming the oldest entries to remain under " + str(self.max_context_length) + " tokens."
|
|
63
|
-
logging.info(message)
|
|
64
|
-
|
|
65
|
-
def clear(self):
|
|
66
|
-
if os.path.exists(self.context_file):
|
|
67
|
-
os.remove(self.context_file)
|
|
68
|
-
return self.new()
|
|
69
|
-
|
|
70
|
-
return None
|
|
71
|
-
|
|
72
|
-
def behavior(self, inputstream):
|
|
73
|
-
self.context_file = self.config.dot_hai_context + "/" + self.config.context + "/context.json"
|
|
74
|
-
if os.path.exists(self.context_file):
|
|
75
|
-
with open(self.context_file, 'w') as f:
|
|
76
|
-
inputstream = inputstream.read().decode('utf-8')
|
|
77
|
-
behavior = { "role" : "system", "content" : inputstream }
|
|
78
|
-
self.context[0] = behavior
|
|
79
|
-
titled_context = { 'title': self.title, 'context': self.context }
|
|
80
|
-
json.dump(titled_context, f)
|
|
81
|
-
return None
|
|
82
|
-
|
|
83
|
-
return None
|
|
84
|
-
|
|
85
|
-
def append(self, question):
|
|
86
|
-
logging.debug(question)
|
|
87
|
-
self.context.append(question)
|
|
88
|
-
|
|
89
|
-
def get_context(self):
|
|
90
|
-
self.context_file = self.config.dot_hai_context + "/" + self.config.context + "/context.json"
|
|
91
|
-
if os.path.exists(self.context_file):
|
|
92
|
-
try:
|
|
93
|
-
with open(self.context_file, 'r') as f:
|
|
94
|
-
titled_context = json.load(f)
|
|
95
|
-
self.title = titled_context.get('title', '')
|
|
96
|
-
self.context = titled_context.get('context', {})
|
|
97
|
-
return titled_context
|
|
98
|
-
except:
|
|
99
|
-
return self.new()
|
|
100
|
-
else:
|
|
101
|
-
return self.new()
|
|
102
|
-
|
|
103
|
-
return None
|
|
104
|
-
|
|
105
|
-
# Ouput for human consumption and longstanding conversation tracking
|
|
106
|
-
def get_readable_context(self):
|
|
107
|
-
context_data = self.get_context()
|
|
108
|
-
readable_context = []
|
|
109
|
-
if context_data:
|
|
110
|
-
readable_context.append(f"----Title: {context_data.get('title', 'No title')}\n\n")
|
|
111
|
-
context = context_data.get('context', [])
|
|
112
|
-
for item in context:
|
|
113
|
-
role = item.get('role', 'Unknown')
|
|
114
|
-
content = item.get('content', '')
|
|
115
|
-
readable_context.append(f"----{role.capitalize()}:\n\n{content}\n")
|
|
116
|
-
|
|
117
|
-
result = "".join(readable_context)
|
|
118
|
-
return result
|
|
119
|
-
|
|
120
|
-
def messages(self):
|
|
121
|
-
return self.context
|
|
122
|
-
|
|
123
|
-
def new(self):
|
|
124
|
-
share = self.config.dot_hai_context
|
|
125
|
-
current_context = self.config.dot_hai_context + "/" + self.config.context
|
|
126
|
-
|
|
127
|
-
if not os.path.exists(share):
|
|
128
|
-
hutils.create_folder(share)
|
|
129
|
-
|
|
130
|
-
if not os.path.exists(current_context):
|
|
131
|
-
hutils.create_folder(current_context)
|
|
132
|
-
|
|
133
|
-
if not os.path.exists(self.context_file):
|
|
134
|
-
with open(self.context_file, 'w') as f:
|
|
135
|
-
self.title = None
|
|
136
|
-
self.context = [{ "role" : "system", "content" : "" }]
|
|
137
|
-
titled_context = { 'title': self.title, 'context': self.context }
|
|
138
|
-
json.dump(titled_context, f)
|
|
139
|
-
return titled_context
|
|
140
|
-
|
|
141
|
-
self.total_tokens = 0
|
|
142
|
-
|
|
143
|
-
return None
|
|
144
|
-
|
|
145
|
-
def save(self):
|
|
146
|
-
titled_context = { 'title': self.title, 'context': self.context }
|
|
147
|
-
|
|
148
|
-
with open(self.context_file, 'w') as f:
|
|
149
|
-
json.dump(titled_context, f)
|
|
150
|
-
|
|
151
|
-
def set(self, id):
|
|
152
|
-
self.config.context = id
|
|
153
|
-
self.config.save()
|
|
154
|
-
self.context_file = self.config.dot_hai_context + "/" + self.config.context + "/context.json"
|
|
155
|
-
|
|
156
|
-
# produces a summary then a title for the current context that's at most 10 words long from a limited bit of conversation context.
|
|
157
|
-
def generate_title(self):
|
|
158
|
-
text = ""
|
|
159
|
-
for item in self.context:
|
|
160
|
-
if "content" in item:
|
|
161
|
-
text += item["content"]
|
|
162
|
-
|
|
163
|
-
title = self.summary.generate_title(text)
|
|
164
|
-
logging.debug("title: " + title)
|
|
165
|
-
self.title = title
|
|
166
|
-
|
|
167
|
-
return self.title
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|