modi-cogitandi 0.0.1__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.
- modi_cogitandi/__init__.py +20 -0
- modi_cogitandi/cli.py +151 -0
- modi_cogitandi/config.py +43 -0
- modi_cogitandi/functions.py +493 -0
- modi_cogitandi/githf.py +83 -0
- modi_cogitandi/machina.yaml +70 -0
- modi_cogitandi/main.py +167 -0
- modi_cogitandi/providers/__init__.py +7 -0
- modi_cogitandi/providers/camelids.py +161 -0
- modi_cogitandi/providers/depsek.py +164 -0
- modi_cogitandi/providers/fireworks.py +185 -0
- modi_cogitandi/providers/metai.py +170 -0
- modi_cogitandi/providers/openai.py +169 -0
- modi_cogitandi/providers/strangelove.py +170 -0
- modi_cogitandi/providers/tinker.py +200 -0
- modi_cogitandi/utilities.py +433 -0
- modi_cogitandi-0.0.1.dist-info/METADATA +76 -0
- modi_cogitandi-0.0.1.dist-info/RECORD +22 -0
- modi_cogitandi-0.0.1.dist-info/WHEEL +5 -0
- modi_cogitandi-0.0.1.dist-info/entry_points.txt +2 -0
- modi_cogitandi-0.0.1.dist-info/licenses/LICENSE +21 -0
- modi_cogitandi-0.0.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Python
|
|
3
|
+
|
|
4
|
+
"""Copyright (c) Alexander Fedotov.
|
|
5
|
+
This source code is licensed under the license found in the
|
|
6
|
+
LICENSE file in the root directory of this source tree.
|
|
7
|
+
"""
|
|
8
|
+
from .config import Config
|
|
9
|
+
from .main import machina
|
|
10
|
+
from .githf import fetch_instructions
|
|
11
|
+
from .utilities import (plato_text_to_muj,
|
|
12
|
+
plato_text_to_mpuj,
|
|
13
|
+
llm_soup_to_text,
|
|
14
|
+
new_plato_text)
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
'machina',
|
|
18
|
+
'fetch_instructions',
|
|
19
|
+
'Config'
|
|
20
|
+
]
|
modi_cogitandi/cli.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Python
|
|
3
|
+
|
|
4
|
+
"""Copyright (c) Alexander Fedotov.
|
|
5
|
+
This source code is licensed under the license found in the
|
|
6
|
+
LICENSE file in the root directory of this source tree.
|
|
7
|
+
"""
|
|
8
|
+
import os
|
|
9
|
+
import sys
|
|
10
|
+
import select
|
|
11
|
+
import fileinput
|
|
12
|
+
import argparse
|
|
13
|
+
import syslog
|
|
14
|
+
from .config import Config
|
|
15
|
+
from .utilities import new_plato_text
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def options_and_arguments():
|
|
19
|
+
parser = argparse.ArgumentParser(
|
|
20
|
+
description="Machina-Ratiocinatrix ratiocinates.",
|
|
21
|
+
epilog="Example: machina input_text.txt > output_text.txt"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
# Give the access key and token, or set the environment variables in advance.
|
|
25
|
+
parser.add_argument('-p', '--provider-api-key',
|
|
26
|
+
default=os.getenv('PROVIDER_API_KEY', 'no_key'),
|
|
27
|
+
help="LLM provider API key (defaults to $PROVIDER_API_KEY)")
|
|
28
|
+
parser.add_argument('-g', '--github-token',
|
|
29
|
+
default=os.getenv('GITHUB_TOKEN', 'no_token'),
|
|
30
|
+
help="GitHub API token (defaults to $GITHUB_TOKEN)")
|
|
31
|
+
|
|
32
|
+
parser.add_argument('-a', '--append',
|
|
33
|
+
action='store_true',
|
|
34
|
+
help="Append the proceeds to the input.")
|
|
35
|
+
|
|
36
|
+
parser.add_argument('-d', '--debug',
|
|
37
|
+
action='store_true',
|
|
38
|
+
help="Debug flag.")
|
|
39
|
+
|
|
40
|
+
parser.add_argument('-i', '--interactive',
|
|
41
|
+
action='store_true',
|
|
42
|
+
help="Enable interactive mode (defaults to False)")
|
|
43
|
+
|
|
44
|
+
# Positional arguments (files)
|
|
45
|
+
# '*' captures zero or more arguments into a list, nargs='+' one or more.
|
|
46
|
+
parser.add_argument('filenames',
|
|
47
|
+
nargs='*',
|
|
48
|
+
help="Zero (when text comes though a pipe) or more files to process.")
|
|
49
|
+
return parser
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def run():
|
|
53
|
+
"""After installation, on Linux prompt
|
|
54
|
+
$ machina file1.txt file2.txt > output.txt
|
|
55
|
+
"""
|
|
56
|
+
args = options_and_arguments().parse_args()
|
|
57
|
+
|
|
58
|
+
# If no files are provided AND no data is being piped in - exit.
|
|
59
|
+
# if not args.filenames:
|
|
60
|
+
# # Check if stdin (fd 0) is ready to be read
|
|
61
|
+
# readable, _, _ = select.select([sys.stdin], [], [], 0.1)
|
|
62
|
+
# if not readable:
|
|
63
|
+
# print("Error: No input files or piped text stream.")
|
|
64
|
+
# options_and_arguments().print_help()
|
|
65
|
+
# sys.exit(1)
|
|
66
|
+
|
|
67
|
+
# Ingest files line by line. Join is here for long files.
|
|
68
|
+
# lines = []
|
|
69
|
+
# for line in fileinput.input(files=args.filenames or ['-'], encoding="utf-8"):
|
|
70
|
+
# lines.append(line)
|
|
71
|
+
# raw_input = "".join(lines)
|
|
72
|
+
|
|
73
|
+
config = Config()
|
|
74
|
+
|
|
75
|
+
if args.provider_api_key:
|
|
76
|
+
if args.provider_api_key.startswith('sk-'):
|
|
77
|
+
if args.provider_api_key.startswith('sk-proj-'):
|
|
78
|
+
config.provider = 'OpenAI'
|
|
79
|
+
os.environ['OPENAI_API_KEY'] = args.provider_api_key
|
|
80
|
+
elif args.provider_api_key.startswith('sk-ant-'):
|
|
81
|
+
config.provider = 'Anthropic'
|
|
82
|
+
os.environ['ANTHROPIC_API_KEY'] = args.provider_api_key
|
|
83
|
+
elif args.provider_api_key.startswith('LLM_'):
|
|
84
|
+
config.provider = 'MetAI'
|
|
85
|
+
os.environ['METAI_API_KEY'] = args.provider_api_key
|
|
86
|
+
elif args.provider_api_key.startswith('tml-'):
|
|
87
|
+
config.provider = 'Tinker'
|
|
88
|
+
os.environ['TINKER_API_KEY'] = args.provider_api_key
|
|
89
|
+
else:
|
|
90
|
+
if config.provider == '':
|
|
91
|
+
raise ValueError(f"Unrecognized API key prefix and no provider specified.")
|
|
92
|
+
else:
|
|
93
|
+
if config.provider == 'Baseten':
|
|
94
|
+
os.environ['BASETEN_API_KEY'] = args.provider_api_key
|
|
95
|
+
elif config.provider == 'Fireworks':
|
|
96
|
+
os.environ['FIREWORKS_API_KEY'] = args.provider_api_key
|
|
97
|
+
elif config.provider == 'Lightning':
|
|
98
|
+
os.environ['LIGHTNING_API_KEY'] = args.provider_api_key
|
|
99
|
+
elif config.provider == 'DepSek':
|
|
100
|
+
os.environ['DEPSEK_API_KEY'] = args.provider_api_key
|
|
101
|
+
else:
|
|
102
|
+
raise ValueError(f"Unsupported provider specified.")
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
config.provider_api_key = args.provider_api_key
|
|
106
|
+
|
|
107
|
+
if args.github_token:
|
|
108
|
+
config.github_token = args.github_token
|
|
109
|
+
os.environ['GITHUB_TOKEN'] = args.github_token
|
|
110
|
+
|
|
111
|
+
# Ingest files line by line. Join is here for long files.
|
|
112
|
+
lines = []
|
|
113
|
+
for line in fileinput.input(files=args.filenames or ['-'], encoding="utf-8"):
|
|
114
|
+
lines.append(line)
|
|
115
|
+
raw_input = "".join(lines)
|
|
116
|
+
|
|
117
|
+
from .main import machina
|
|
118
|
+
|
|
119
|
+
try:
|
|
120
|
+
thoughts, text = machina(raw_input, config)
|
|
121
|
+
output = new_plato_text(thoughts, text, config.name)
|
|
122
|
+
if args.append:
|
|
123
|
+
output = raw_input + '\n\n' + output
|
|
124
|
+
sys.stdout.write(output)
|
|
125
|
+
sys.stdout.flush()
|
|
126
|
+
|
|
127
|
+
# Assesment and signals.
|
|
128
|
+
|
|
129
|
+
utterance = "Machina is ready"
|
|
130
|
+
# Open syslog connection
|
|
131
|
+
syslog.openlog(
|
|
132
|
+
ident="machina-ratiocinatrix",
|
|
133
|
+
logoption=syslog.LOG_NDELAY,
|
|
134
|
+
facility=syslog.LOG_USER
|
|
135
|
+
)
|
|
136
|
+
# Signal (single line less than 4096 only!)
|
|
137
|
+
syslog.syslog(syslog.LOG_INFO, utterance)
|
|
138
|
+
syslog.closelog()
|
|
139
|
+
|
|
140
|
+
except Exception as e:
|
|
141
|
+
if args.debug:
|
|
142
|
+
import traceback
|
|
143
|
+
traceback.print_exc()
|
|
144
|
+
else:
|
|
145
|
+
sys.stderr.write(f'Machina did not work {e}\n')
|
|
146
|
+
sys.stderr.flush()
|
|
147
|
+
sys.exit(1)
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
if __name__ == '__main__':
|
|
151
|
+
run()
|
modi_cogitandi/config.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
# Python
|
|
3
|
+
|
|
4
|
+
"""Copyright (c) Alexander Fedotov.
|
|
5
|
+
This source code is licensed under the license found in the
|
|
6
|
+
LICENSE file in the root directory of this source tree.
|
|
7
|
+
"""
|
|
8
|
+
from os import environ
|
|
9
|
+
from dataclasses import dataclass, field, fields, asdict
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass
|
|
13
|
+
class Config:
|
|
14
|
+
github_token: str = field(default_factory=lambda: environ.get('GITHUB_TOKEN', ''))
|
|
15
|
+
github_name: str = field(default_factory=lambda: environ.get('GITHUB_NAME', ''))
|
|
16
|
+
github_email: str = field(default_factory=lambda: environ.get('GITHUB_EMAIL', ''))
|
|
17
|
+
provider_api_key: str = field(default_factory=lambda: environ.get('PROVIDER_API_KEY', ''))
|
|
18
|
+
provider: str = field(default_factory=lambda: environ.get('PROVIDER', ''))
|
|
19
|
+
machine_organization_name: str = field(
|
|
20
|
+
default_factory=lambda: environ.get('ORGANIZATION_NAME', 'machina-ratiocinatrix'))
|
|
21
|
+
private_repo_with_text: str = field(
|
|
22
|
+
default_factory=lambda: environ.get('REPO_WITH_INSTRUCTIONS', 'machina_ratiocinatrix'))
|
|
23
|
+
system_prompt_file: str = field(default_factory=lambda: environ.get('SYSTEM_INSTRUCTIONS_FILE', 'machina.yaml'))
|
|
24
|
+
name: str = field(default_factory=lambda: environ.get('NAME', ''))
|
|
25
|
+
instructions: str = field(default_factory=lambda: environ.get('INSTRUCTIONS', ''))
|
|
26
|
+
verb: str = field(default_factory=lambda: environ.get('VERB', ''))
|
|
27
|
+
tools: str = field(
|
|
28
|
+
default_factory=lambda: environ.get('TOOLS', '')) # Swapped [] to '' for consistency with str annotation
|
|
29
|
+
rubric: str = field(default_factory=lambda: environ.get('RUBRIC', ''))
|
|
30
|
+
|
|
31
|
+
def to_dict(self):
|
|
32
|
+
return asdict(self)
|
|
33
|
+
|
|
34
|
+
def update_from_dict(self, data: dict):
|
|
35
|
+
# Now self.__dict__ is fully populated, but fields() is still safer!
|
|
36
|
+
valid_fields = {f.name for f in fields(self)}
|
|
37
|
+
for key, value in data.items():
|
|
38
|
+
if key in valid_fields:
|
|
39
|
+
setattr(self, key, value)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
if __name__ == '__main__':
|
|
43
|
+
...
|