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/__init__.py
ADDED
hcli_hai/__main__.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
from subprocess import call
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
from . import package
|
|
6
|
+
from . import config
|
|
7
|
+
from . import hutils
|
|
8
|
+
|
|
9
|
+
def main():
|
|
10
|
+
if len(sys.argv) == 2:
|
|
11
|
+
|
|
12
|
+
if sys.argv[1] == "--version":
|
|
13
|
+
show_dependencies()
|
|
14
|
+
|
|
15
|
+
elif sys.argv[1] == "help":
|
|
16
|
+
display_man_page(config.hcli_hai_manpage_path)
|
|
17
|
+
sys.exit(0)
|
|
18
|
+
|
|
19
|
+
elif sys.argv[1] == "path":
|
|
20
|
+
print(config.plugin_path)
|
|
21
|
+
sys.exit(0)
|
|
22
|
+
|
|
23
|
+
else:
|
|
24
|
+
hcli_hai_help()
|
|
25
|
+
|
|
26
|
+
hcli_hai_help()
|
|
27
|
+
|
|
28
|
+
# show huckle's version and the version of its dependencies
|
|
29
|
+
def show_dependencies():
|
|
30
|
+
dependencies = ""
|
|
31
|
+
for i, x in enumerate(package.dependencies):
|
|
32
|
+
dependencies += " "
|
|
33
|
+
dependencies += package.dependencies[i].rsplit('==', 1)[0] + "/"
|
|
34
|
+
dependencies += package.dependencies[i].rsplit('==', 1)[1]
|
|
35
|
+
print("hcli_hai/" + package.__version__ + dependencies)
|
|
36
|
+
|
|
37
|
+
def hcli_hai_help():
|
|
38
|
+
hutils.eprint("for help, use:\n")
|
|
39
|
+
hutils.eprint(" hcli_hai help")
|
|
40
|
+
sys.exit(2)
|
|
41
|
+
|
|
42
|
+
# displays a man page (file) located on a given path
|
|
43
|
+
def display_man_page(path):
|
|
44
|
+
call(["man", path])
|
hcli_hai/cli/__init__.py
ADDED
|
File without changes
|
hcli_hai/cli/ai.py
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import io
|
|
3
|
+
import os
|
|
4
|
+
import sys
|
|
5
|
+
import inspect
|
|
6
|
+
import traceback
|
|
7
|
+
import logger
|
|
8
|
+
import config
|
|
9
|
+
import context as c
|
|
10
|
+
import config
|
|
11
|
+
import uuid
|
|
12
|
+
import shutil
|
|
13
|
+
|
|
14
|
+
from datetime import datetime
|
|
15
|
+
from anthropic import Anthropic
|
|
16
|
+
from model import models
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
logging = logger.Logger()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class AI:
|
|
23
|
+
config = None
|
|
24
|
+
contextmgr = None
|
|
25
|
+
|
|
26
|
+
def __init__(self):
|
|
27
|
+
self.config = config.Config()
|
|
28
|
+
self.contextmgr = c.ContextManager()
|
|
29
|
+
|
|
30
|
+
def chat(self, inputstream):
|
|
31
|
+
if self.config.model is not None:
|
|
32
|
+
inputstream = inputstream.read().decode('utf-8')
|
|
33
|
+
if inputstream != "":
|
|
34
|
+
inputstream = inputstream.rstrip()
|
|
35
|
+
question = { "role" : "user", "content" : inputstream }
|
|
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']))
|
|
41
|
+
|
|
42
|
+
if self.contextmgr.counter.total_tokens != 0:
|
|
43
|
+
try:
|
|
44
|
+
client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
|
|
45
|
+
|
|
46
|
+
# Separate system message from user messages
|
|
47
|
+
model = models[self.config.model]
|
|
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"]
|
|
50
|
+
|
|
51
|
+
response = client.messages.create(
|
|
52
|
+
**model,
|
|
53
|
+
system=system_message,
|
|
54
|
+
messages=user_messages
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
logging.debug(response)
|
|
58
|
+
|
|
59
|
+
except Exception as e:
|
|
60
|
+
logging.error(traceback.format_exc())
|
|
61
|
+
return None
|
|
62
|
+
else:
|
|
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
|
|
68
|
+
|
|
69
|
+
output_response = response
|
|
70
|
+
|
|
71
|
+
# Extract the text content from the response
|
|
72
|
+
output_content = " ".join([block.text for block in output_response.content if block.type == 'text'])
|
|
73
|
+
|
|
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']))
|
|
78
|
+
|
|
79
|
+
output = output_content
|
|
80
|
+
|
|
81
|
+
self.contextmgr.generate_title()
|
|
82
|
+
self.contextmgr.save()
|
|
83
|
+
|
|
84
|
+
return output
|
|
85
|
+
else:
|
|
86
|
+
warning = "No model selected. Select one from the list of models."
|
|
87
|
+
logging.warning(warning)
|
|
88
|
+
return warning
|
|
89
|
+
|
|
90
|
+
def get_context(self):
|
|
91
|
+
return self.contextmgr.get_context()
|
|
92
|
+
|
|
93
|
+
def get_readable_context(self):
|
|
94
|
+
return self.contextmgr.get_readable_context()
|
|
95
|
+
|
|
96
|
+
def clear(self):
|
|
97
|
+
self.contextmgr.clear()
|
|
98
|
+
|
|
99
|
+
def behavior(self, inputstream):
|
|
100
|
+
self.contextmgr.behavior(inputstream)
|
|
101
|
+
|
|
102
|
+
def ls(self):
|
|
103
|
+
contexts = []
|
|
104
|
+
share = self.config.dot_hai_context
|
|
105
|
+
|
|
106
|
+
# Iterate through all items in the folder
|
|
107
|
+
for item in os.listdir(share):
|
|
108
|
+
item_path = os.path.join(share, item)
|
|
109
|
+
# Check if the item is a directory
|
|
110
|
+
if os.path.isdir(item_path):
|
|
111
|
+
|
|
112
|
+
context_file = os.path.join(item_path, 'context.json')
|
|
113
|
+
if os.path.exists(context_file):
|
|
114
|
+
try:
|
|
115
|
+
with open(context_file, 'r') as f:
|
|
116
|
+
context = c.Context(json.load(f))
|
|
117
|
+
title = context.title
|
|
118
|
+
name = context.name
|
|
119
|
+
update_time = os.path.getmtime(context_file)
|
|
120
|
+
contexts.append({
|
|
121
|
+
'context_id': item,
|
|
122
|
+
'title': title,
|
|
123
|
+
'name': name,
|
|
124
|
+
'update_time': update_time
|
|
125
|
+
})
|
|
126
|
+
except json.JSONDecodeError:
|
|
127
|
+
# If there's an error reading the JSON, just use the directory name
|
|
128
|
+
contexts.append({
|
|
129
|
+
'context_id': item,
|
|
130
|
+
'title': 'N/A',
|
|
131
|
+
'name': 'N/A',
|
|
132
|
+
'update_time': os.path.getmtime(context_file) if os.path.exists(context_file) else 0
|
|
133
|
+
})
|
|
134
|
+
else:
|
|
135
|
+
# If context.json doesn't exist, use the directory name only
|
|
136
|
+
contexts.append({
|
|
137
|
+
'context_id': item,
|
|
138
|
+
'title': 'N/A',
|
|
139
|
+
'name': 'N/A',
|
|
140
|
+
'update_time': 0
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
# Sort contexts by creation time, newest at the bottom of the list
|
|
144
|
+
sorted_contexts = sorted(contexts, key=lambda x: x['update_time'], reverse=False)
|
|
145
|
+
|
|
146
|
+
# Format the creation time and remove it from the final output
|
|
147
|
+
for context in sorted_contexts:
|
|
148
|
+
context['update_time'] = datetime.fromtimestamp(context['update_time']).strftime('%Y-%m-%d %H:%M:%S')
|
|
149
|
+
|
|
150
|
+
return sorted_contexts
|
|
151
|
+
|
|
152
|
+
def set(self, id):
|
|
153
|
+
contexts = self.ls()
|
|
154
|
+
context_ids = [context['context_id'] for context in contexts]
|
|
155
|
+
if id in context_ids:
|
|
156
|
+
self.contextmgr.set(id)
|
|
157
|
+
else:
|
|
158
|
+
logging.warning("provided context id is not found in available contexts.")
|
|
159
|
+
|
|
160
|
+
def new(self):
|
|
161
|
+
if os.path.exists(self.config.dot_hai_config_file):
|
|
162
|
+
os.remove(self.config.dot_hai_config_file)
|
|
163
|
+
self.config.init()
|
|
164
|
+
self.contextmgr.init()
|
|
165
|
+
|
|
166
|
+
return self.current()
|
|
167
|
+
|
|
168
|
+
def rm(self, id):
|
|
169
|
+
context_folder = os.path.join(self.config.dot_hai_context, id)
|
|
170
|
+
if os.path.exists(context_folder):
|
|
171
|
+
shutil.rmtree(context_folder)
|
|
172
|
+
logging.info("removed " + context_folder)
|
|
173
|
+
|
|
174
|
+
def current(self):
|
|
175
|
+
return self.config.context
|
|
176
|
+
|
|
177
|
+
def list_models(self):
|
|
178
|
+
return self.config.list_models()
|
|
179
|
+
|
|
180
|
+
def model(self):
|
|
181
|
+
return self.config.model
|
|
182
|
+
|
|
183
|
+
def set_model(self, model):
|
|
184
|
+
models = self.list_models()
|
|
185
|
+
if model in models:
|
|
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)
|
hcli_hai/cli/cli.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import io
|
|
3
|
+
import os
|
|
4
|
+
import sys
|
|
5
|
+
import inspect
|
|
6
|
+
import traceback
|
|
7
|
+
import tiktoken
|
|
8
|
+
import logger
|
|
9
|
+
import ai
|
|
10
|
+
|
|
11
|
+
from anthropic import Anthropic
|
|
12
|
+
|
|
13
|
+
logging = logger.Logger()
|
|
14
|
+
logging.setLevel(logger.INFO)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class CLI:
|
|
18
|
+
commands = None
|
|
19
|
+
inputstream = None
|
|
20
|
+
|
|
21
|
+
def __init__(self, commands, inputstream):
|
|
22
|
+
self.commands = commands
|
|
23
|
+
self.inputstream = inputstream
|
|
24
|
+
self.ai = ai.AI()
|
|
25
|
+
|
|
26
|
+
def execute(self):
|
|
27
|
+
if len(self.commands) == 1:
|
|
28
|
+
if self.inputstream is not None:
|
|
29
|
+
response = self.ai.chat(self.inputstream)
|
|
30
|
+
return io.BytesIO(response.encode('utf-8') + "\n".encode('utf-8'))
|
|
31
|
+
|
|
32
|
+
if self.commands[1] == "clear":
|
|
33
|
+
self.ai.clear()
|
|
34
|
+
return
|
|
35
|
+
|
|
36
|
+
if self.commands[1] == "context":
|
|
37
|
+
if len(self.commands) == 2:
|
|
38
|
+
context = self.ai.get_context()
|
|
39
|
+
return io.BytesIO(context.serialize().encode('utf-8') + "\n".encode('utf-8'))
|
|
40
|
+
if len(self.commands) == 3:
|
|
41
|
+
readable_context = self.ai.get_readable_context()
|
|
42
|
+
return io.BytesIO(readable_context.encode('utf-8') + "\n".encode('utf-8'))
|
|
43
|
+
|
|
44
|
+
if self.commands[1] == "ls":
|
|
45
|
+
contexts = self.ai.ls()
|
|
46
|
+
return io.BytesIO(json.dumps(contexts, indent=4).encode('utf-8') + "\n".encode('utf-8'))
|
|
47
|
+
|
|
48
|
+
if self.commands[1] == "new":
|
|
49
|
+
current = self.ai.new()
|
|
50
|
+
return io.BytesIO(current.encode('utf-8') + "\n".encode('utf-8'))
|
|
51
|
+
|
|
52
|
+
if self.commands[1] == "current":
|
|
53
|
+
current = self.ai.current()
|
|
54
|
+
return io.BytesIO(current.encode('utf-8') + "\n".encode('utf-8'))
|
|
55
|
+
|
|
56
|
+
if self.commands[1] == "behavior":
|
|
57
|
+
if self.inputstream is not None:
|
|
58
|
+
self.ai.behavior(self.inputstream)
|
|
59
|
+
return
|
|
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
|
+
|
|
74
|
+
if self.commands[1] == "model":
|
|
75
|
+
if len(self.commands) == 2:
|
|
76
|
+
model = self.ai.model()
|
|
77
|
+
if model is not None:
|
|
78
|
+
return io.BytesIO(model.encode('utf-8') + "\n".encode('utf-8'))
|
|
79
|
+
else:
|
|
80
|
+
return io.BytesIO(b"None\n")
|
|
81
|
+
|
|
82
|
+
if len(self.commands) == 3:
|
|
83
|
+
if self.commands[2] == "ls":
|
|
84
|
+
models = self.ai.list_models()
|
|
85
|
+
return io.BytesIO(json.dumps(models, indent=4).encode('utf-8') + "\n".encode('utf-8'))
|
|
86
|
+
|
|
87
|
+
if len(self.commands) == 4:
|
|
88
|
+
if self.commands[2] == "set":
|
|
89
|
+
self.ai.set_model(self.commands[3])
|
|
90
|
+
return
|
|
91
|
+
|
|
92
|
+
if self.commands[1] == "set":
|
|
93
|
+
if len(self.commands) == 3:
|
|
94
|
+
self.ai.set(self.commands[2])
|
|
95
|
+
return
|
|
96
|
+
|
|
97
|
+
if self.commands[1] == "rm":
|
|
98
|
+
if len(self.commands) == 3:
|
|
99
|
+
self.ai.rm(self.commands[2])
|
|
100
|
+
return
|
|
101
|
+
|
|
102
|
+
return None
|
hcli_hai/cli/config.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
from configparser import ConfigParser
|
|
2
|
+
from io import StringIO
|
|
3
|
+
from os import listdir
|
|
4
|
+
from os.path import isfile, join, isdir
|
|
5
|
+
from os import path, listdir
|
|
6
|
+
from model import models
|
|
7
|
+
|
|
8
|
+
from utils import hutils
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
import sys
|
|
12
|
+
import shutil
|
|
13
|
+
import json
|
|
14
|
+
import logger
|
|
15
|
+
import uuid
|
|
16
|
+
import context as c
|
|
17
|
+
|
|
18
|
+
logging = logger.Logger()
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Config:
|
|
22
|
+
home = os.path.expanduser("~")
|
|
23
|
+
dot_hai = "%s/.hai" % home
|
|
24
|
+
dot_hai_config = dot_hai + "/etc"
|
|
25
|
+
dot_hai_config_file = dot_hai_config + "/config"
|
|
26
|
+
dot_hai_context = dot_hai + "/share"
|
|
27
|
+
context = ""
|
|
28
|
+
model = None
|
|
29
|
+
parser = None
|
|
30
|
+
instance = None
|
|
31
|
+
|
|
32
|
+
def __new__(cls):
|
|
33
|
+
if cls.instance is None:
|
|
34
|
+
cls.instance = super().__new__(cls)
|
|
35
|
+
cls.instance.init()
|
|
36
|
+
return cls.instance
|
|
37
|
+
|
|
38
|
+
def init(self):
|
|
39
|
+
self.parser = ConfigParser()
|
|
40
|
+
self.parser.read(self.dot_hai_config_file)
|
|
41
|
+
|
|
42
|
+
self.create_configuration()
|
|
43
|
+
self.parse_configuration()
|
|
44
|
+
|
|
45
|
+
# parses the configuration of a given cli to set configured execution
|
|
46
|
+
def parse_configuration(self):
|
|
47
|
+
if self.parser.has_section("default"):
|
|
48
|
+
for section_name in self.parser.sections():
|
|
49
|
+
for name, value in self.parser.items("default"):
|
|
50
|
+
if name == "context":
|
|
51
|
+
self.context = value
|
|
52
|
+
else:
|
|
53
|
+
sys.exit("hai: no available configuration.")
|
|
54
|
+
|
|
55
|
+
# creates a configuration file for a named cli
|
|
56
|
+
def create_configuration(self):
|
|
57
|
+
hutils.create_folder(self.dot_hai)
|
|
58
|
+
hutils.create_folder(self.dot_hai_config)
|
|
59
|
+
hutils.create_folder(self.dot_hai_context)
|
|
60
|
+
|
|
61
|
+
if not os.path.exists(self.dot_hai_config_file):
|
|
62
|
+
hutils.create_file(self.dot_hai_config_file)
|
|
63
|
+
|
|
64
|
+
self.parser.read_file(StringIO(u"[default]"))
|
|
65
|
+
self.parser.set("default", "context", str(uuid.uuid4()))
|
|
66
|
+
with open(self.dot_hai_config_file, "w") as config:
|
|
67
|
+
self.parser.write(config)
|
|
68
|
+
else:
|
|
69
|
+
logging.debug("the configuration for hai already exists. leaving the existing configuration untouched.")
|
|
70
|
+
return
|
|
71
|
+
|
|
72
|
+
logging.info("hai was successfully configured.")
|
|
73
|
+
return
|
|
74
|
+
|
|
75
|
+
def save(self):
|
|
76
|
+
if os.path.exists(self.dot_hai_config_file):
|
|
77
|
+
self.parser.set("default", "context", self.context)
|
|
78
|
+
with open(self.dot_hai_config_file, "w") as config:
|
|
79
|
+
self.parser.write(config)
|
|
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
|
+
|
|
125
|
+
def list_models(self):
|
|
126
|
+
model_names = list(models.keys())
|
|
127
|
+
model_names.sort()
|
|
128
|
+
return model_names
|