-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoai.py
executable file
·357 lines (306 loc) · 12.6 KB
/
oai.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env python3
import argparse
import os
import subprocess
import platform
import sys
import json
import io
import importlib, inspect
from rich import pager
from rich.console import Console
from rich.markdown import Markdown
from rich.prompt import Prompt
from resources import config
# from resources.conduit import get_completion
from resources.conduit import get_chat, get_models, get_image, get_variant, get_edit
from resources.conduoll import ConduOll
console = Console()
version = "0.7.0"
_session_file_ = ".messages.json"
available_parameters = {}
available_descr = {}
available_functions = {}
def get_lindata():
lindata = "Users Kernel:" + platform.platform() + "\n" \
"Users OS:" + os.uname().version + "\n" \
"Users Shell:" + os.environ.get("SHELL", "").split("/")[-1] + "\n\n"
return lindata
def post_completion(openai_response):
if config.get_expert_mode() != "true":
openai_response += '\n\n[Notice] OpenAI\'s models have limited knowledge after 2020. Commands and versions' \
'may be outdated. Recommendations are not guaranteed to work and may be dangerous.' \
'To disable this notice, switch to expert mode with `oai --expert`.'
return openai_response
def get_session():
with open(_session_file_) as sf:
messages=json.load(sf)
return messages
def put_session(messages):
try:
to_unicode = unicode
except NameError:
to_unicode = str
# print(messages)
with open(_session_file_, 'w', encoding='utf8') as outfile:
str_ = json.dumps(messages,
indent=4, sort_keys=True,
separators=(',', ': '), ensure_ascii=False)
outfile.write(to_unicode(str_))
def read_text(url):
messages = ""
with open(url) as sf:
for line in sf:
line = line.rstrip()
messages += line + ' '
return messages
def stripp_it(rawt):
rawt.replace('\n', ' ').replace('\r', '')
rawt.replace('\t', ' ')
return rawt
def read_csv(url):
messages = ""
with open(url) as sf:
for line in sf:
line = line.rstrip()
messages += line + '\n'
return messages
def read_pdf(prompt):
console.log("PDF not supported yet")
exit()
def extract_jsonstr(prompt):
file, ext = prompt.split('.')
if ext == 'txt':
rawt = read_text(prompt)
pro = stripp_it(rawt)
elif ext in ['py', 'html', 'css', 'c', 'h', 'cpp', 'hpp']:
rawt = read_text(prompt)
pro = stripp_it(rawt)
elif ext == 'csv':
rawt = read_csv(prompt)
pro = stripp_it(rawt)
elif ext == 'pdf':
rawt = read_pdf(prompt)
else:
console.status("File format not supported: "+ext)
exit()
return pro
def get_fun_def(func):
for file in os.listdir(os.path.dirname(__file__)+"/functions"):
if file.endswith(".py"):
file_name = file[:-3]
module_name = 'functions.' + file_name
for name, cls in inspect.getmembers(importlib.import_module(module_name), inspect.isclass):
if cls.__module__ == module_name:
if func in dir(cls):
obj = cls()
full = inspect.getfullargspec(getattr(obj, func))
args = ', '.join(full.args)
available_functions[func] = getattr(obj, func)
available_parameters[func] = full.args
available_descr[func] = obj.functions
return available_descr[func][0]
return ""
def print_url_list(heading, responce):
console.print(heading)
row = 1
for img in responce.data:
console.print(Markdown(" [url " + str(row) + "](" + img.url + ") " + img.revised_prompt))
# console.print(img.url)
# print(responce)
def main():
checkit = subprocess.run(["which", "ollama"], capture_output=True, text=True)
# import shutil
#def is_program_installed(program_name):
# """Check whether program_name is installed."""
# return shutil.which(program_name)
# if checkit.returncode == 0:
# print("ollama installed: " + checkit.stdout )
# else:
# print("ollama not installed")
desc = "oai - CLI assistant\n\nThis tool sends a query to an LLM.\n\n"\
"OpenAIs Chat API from the command line, is the default choise. (subscription)\n\n"\
"Ollama can provide a range of locally run LLMs.\n\n"\
"A new chat session is started with -n <pre-info> and gives the opportunity to\n"\
"provide pre-information to your question (openAI only)\n\n"\
"Report any issues at: https://github.com/draupner1/oai/issues"
epilog = "Please note that for Ollama questions, the following flags/functions are not supported:" \
"-c,-w,-e,-d,-s,-f,-i, <pre-info> to a new command.\n"
numb = 2
# Create an ArgumentParser object
parser = argparse.ArgumentParser(prog='oai',
formatter_class=argparse.RawTextHelpFormatter,
description=desc,
epilog=epilog)
if checkit.returncode == 0:
parser.add_argument('prov', nargs='?', default='oai', help='Provider of LLM,<blank>/oai = OpenAI, oll = Ollama')
# Add arguments for expert mode, API key reset, version, and prompt
parser.add_argument('-c', '--create', action="store_true", help='Create a new Image, DALL-E3', dest='create')
parser.add_argument('-w', '--variant', action="store_true", help='Create a variant of an Image-file. Provide "<image.png>", DALL-E2', dest='variant')
parser.add_argument('-e', '--edit', action="store_true", help='Edit part of an Image-file. Provide "<image.png>,<mask.png>, Prompt-string",DALL-E2', dest='edit')
parser.add_argument('-d', '--default', default=2, help='How many Images to create. default=2', dest='default')
parser.add_argument('-s', '--size', default="1024x1024", help='Image size. default=1024x1024', dest='size')
parser.add_argument('-n', '--new', action="store_true", help='Start New Chat', dest='new')
parser.add_argument('-f', '--function', default='', help='Enable function call', dest='function')
# parser.add_argument('name', nargs='?', default="")
parser.add_argument('-l', '--linux', action="store_true", help='Include an assistent message with Kernel/OS/shell', dest='linux')
parser.add_argument('-m', '--model', action="store_true", help='List models available via OpenAI/Ollamass API', dest='model')
parser.add_argument('-x', '--expert', action="store_true", help='Toggle warning', dest='expert')
parser.add_argument('-i', '--key', action="store_true", help='Reset API key', dest='apikey')
parser.add_argument('-v', '--version', action="store_true", help=f'Get Version (hint: it\'s {version})', dest='version')
parser.add_argument('--licenses', action="store_true", help='Show oai & Third Party Licenses', dest='licenses')
parser.add_argument('prompt', type=str, nargs='?', help='Prompt to send')
args = parser.parse_args()
if hasattr(args, 'prov') and args.prov not in ['oai', 'oll']:
print("Prov: "+args.prov)
args.prompt = args.prov
args.prov='oai'
if checkit.returncode == 0 and hasattr(args, 'prov') and args.prov == 'oll':
#set up ollama to run local LLM from ollama
ollamaMode = True
oll = ConduOll()
else:
ollamaMode = False
if args.default:
numb = int(args.default)
if args.new:
console.status("Starting a new chat session")
if os.path.exists(_session_file_):
os.remove(_session_file_)
if args.prompt and not ollamaMode:
prompt = args.prompt
if os.path.exists(prompt):
prompt = extract_jsonstr(prompt)
else:
prompt = ""
pprompt = f"{prompt}\n\n" \
f"Response Format: Markdown\n"
messages=[{'role':'assistant', 'content':pprompt}]
put_session(messages)
sys.exit()
if args.linux:
prompt = get_lindata()
if os.path.isfile(_session_file_):
messages=get_session()
messages.append({'role':'user', 'content':prompt})
else:
messages=[{'role':'user', 'content':prompt}]
put_session(messages)
sys.exit()
if args.model:
print(ollamaMode)
if ollamaMode:
model_list = oll.get_models()
else:
model_list = get_models()
for mod in model_list:
print(mod.id)
sys.exit()
if args.version:
console.print("oai version: " + version)
sys.exit()
if args.licenses:
# print LICENSE file with pagination
with console.pager():
try:
with open("LICENSE", "r") as f:
console.print(f.read())
except FileNotFoundError:
with open("/app/bin/LICENSE", "r") as f:
console.print(f.read())
sys.exit()
config.check_config(console)
if args.apikey and not ollamaMode:
config.prompt_new_key()
sys.exit()
if args.expert:
config.toggle_expert_mode()
sys.exit()
func = ""
if args.function and not ollamaMode:
func = args.function
if func == "":
print("No function provided. Exiting...")
sys.exit()
func = get_fun_def(func)
if func == "":
print('Function not found: ' + func)
sys.exit()
if not args.prompt:
prompt = Prompt.ask("Prompt Please")
if prompt == "":
print("No prompt provided. Exiting...")
sys.exit()
else:
prompt = args.prompt
askDict = {'role':'user', 'content':prompt}
if os.path.isfile(_session_file_):
messages = get_session()
messages.append(askDict)
else:
messages=[askDict]
if args.create and not ollamaMode:
if args.size not in ["1:1", "1024x1024", "16:9", "1792x1024", "9:16", "1024x1792"]:
print('DALL-E3 only supports, "1024x1024", "1792x1024", "1024x1792"')
print("size: " + args.size)
exit()
if args.size == "1:1":
args.size = "1024x1024"
if args.size == "16:9":
args.size = "1792x1024"
if args.size == "9:16":
args.size = "1024x1792"
with console.status(f"Phoning a friend... ", spinner="pong"):
print('Doing an image')
openai_response = get_image(prompt, numb, args.size)
print_url_list("Created links:", openai_response)
exit()
if args.variant and not ollamaMode:
with console.status(f"Phoning a friend... ", spinner="pong"):
print('Variant of an image: ' + prompt)
openai_response = get_variant(prompt, numb)
print_url_list("Variant links:", openai_response)
exit()
if args.edit and not ollamaMode:
with console.status(f"Phoning a friend... ", spinner="pong"):
print('Edit of an image: ' + prompt)
openai_response = get_edit(prompt, numb)
print_url_list("Edited links:", openai_response)
exit()
with console.status(f"Phoning a friend... ", spinner="pong"):
if ollamaMode:
openai_response = oll.get_chat(messages)
else:
openai_response = get_chat(messages, func)
if not ollamaMode and openai_response.function_call != None:
function_name = openai_response.function_call.name
if function_name in available_functions:
fuction_to_call = available_functions[function_name]
else:
print('Bad returned function name from OpenAI API')
print(openai_response)
messages.append({"role": "function", "name": function_name, "content": func})
function_args = json.loads(openai_response["function_call"]["arguments"].strip())
console.print(Markdown(function_args.get("content").strip()))
put_session(messages)
exit()
function_args = json.loads(openai_response.function_call.arguments.strip())
#print("Function arguments")
#print(function_args)
function_response = fuction_to_call(
**function_args
)
messages.append({"role": "function", "name": function_name, "content": function_response})
if function_response != 'stop':
openai_response = get_chat(messages)
else:
openai_response.content = function_args.get("content")
if ollamaMode:
outit = openai_response['content'].strip()
else:
outit = openai_response.content.strip()
console.print(Markdown(outit))
messages.append({'role':'assistant', 'content':outit})
put_session(messages)
if __name__ == "__main__":
main()