Skip to content

Commit 4ac1737

Browse files
committed
Handle code and textfiles in -n
1 parent 83f6296 commit 4ac1737

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

.env.default

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
OPENAI_API_KEY=
2-
OPENAI_MODEL=text-davinci-003
3-
OPENAI_DISABLE_NOTICE=false
2+
OPENAI_MODEL=gpt-3.5-turbo
3+
OPENAI_DISABLE_NOTICE=true

oai.py

+52-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818

1919
console = Console()
20-
version = "0.4.0"
21-
_session_file_=".messages.json"
20+
version = "0.4.1"
21+
_session_file_ = ".messages.json"
2222

2323
def get_lindata():
2424
lindata = "Users Kernel:" + platform.platform() + "\n" \
@@ -50,6 +50,54 @@ def put_session(messages):
5050
separators=(',', ': '), ensure_ascii=False)
5151
outfile.write(to_unicode(str_))
5252

53+
def read_text(url):
54+
messages = ""
55+
with open(url) as sf:
56+
for line in sf:
57+
line = line.rstrip()
58+
messages += line + ' '
59+
return messages
60+
61+
def stripp_it(rawt):
62+
rawt.replace('\n', ' ').replace('\r', '')
63+
rawt.replace('\t', ' ')
64+
return rawt
65+
66+
def read_csv(url):
67+
messages = ""
68+
with open(url) as sf:
69+
for line in sf:
70+
line = line.rstrip()
71+
messages += line + '\n'
72+
return messages
73+
74+
75+
def read_pdf(prompt):
76+
console.log("PDF not supported yet")
77+
exit()
78+
79+
def extract_jsonstr(prompt):
80+
file, ext = prompt.split('.')
81+
if ext == 'txt':
82+
rawt = read_text(prompt)
83+
pro = stripp_it(rawt)
84+
85+
elif ext in ['py', 'html', 'css', 'c', 'h', 'cpp', 'hpp']:
86+
rawt = read_text(prompt)
87+
pro = stripp_it(rawt)
88+
89+
elif ext == 'csv':
90+
rawt = read_csv(prompt)
91+
pro = stripp_it(rawt)
92+
93+
elif ext == 'pdf':
94+
rawt = read_pdf(prompt)
95+
96+
else:
97+
console.status("File format not supported: "+ext)
98+
exit()
99+
return pro
100+
53101
def main():
54102
desc = "This tool sends a query to OpenAIs Chat API from the command line.\n\n"\
55103
"A new chat session is started with -n <pre-info> and gives the opportunity to\n"\
@@ -82,6 +130,8 @@ def main():
82130
os.remove(_session_file_)
83131
if args.prompt:
84132
prompt = args.prompt
133+
if os.path.exists(prompt):
134+
prompt = extract_jsonstr(prompt)
85135
else:
86136
prompt = ""
87137
pprompt = f"{prompt}\n\n" \

0 commit comments

Comments
 (0)