5
5
import sys
6
6
import json
7
7
import io
8
+ import importlib , inspect
8
9
9
10
from rich import pager
10
11
from rich .console import Console
17
18
18
19
19
20
console = Console ()
20
- version = "0.4.1 "
21
+ version = "0.5.0 "
21
22
_session_file_ = ".messages.json"
22
23
24
+ available_parameters = {}
25
+ available_descr = {}
26
+ available_functions = {}
27
+
23
28
def get_lindata ():
24
29
lindata = "Users Kernel:" + platform .platform () + "\n " \
25
30
"Users OS:" + os .uname ().version + "\n " \
@@ -98,6 +103,25 @@ def extract_jsonstr(prompt):
98
103
exit ()
99
104
return pro
100
105
106
+ def get_fun_def (func ):
107
+
108
+ for file in os .listdir (os .path .dirname (__file__ )+ "/functions" ):
109
+ if file .endswith (".py" ):
110
+ file_name = file [:- 3 ]
111
+ module_name = 'functions.' + file_name
112
+ for name , cls in inspect .getmembers (importlib .import_module (module_name ), inspect .isclass ):
113
+ if cls .__module__ == module_name :
114
+ if func in dir (cls ):
115
+ obj = cls ()
116
+ full = inspect .getfullargspec (getattr (obj , func ))
117
+ args = ', ' .join (full .args )
118
+ available_functions [func ] = getattr (obj , func )
119
+ available_parameters [func ] = full .args
120
+ available_descr [func ] = obj .functions
121
+ return available_descr [func ][0 ]
122
+ return ""
123
+
124
+
101
125
def main ():
102
126
desc = "This tool sends a query to OpenAIs Chat API from the command line.\n \n " \
103
127
"A new chat session is started with -n <pre-info> and gives the opportunity to\n " \
@@ -115,6 +139,8 @@ def main():
115
139
116
140
# Add arguments for expert mode, API key reset, version, and prompt
117
141
parser .add_argument ('-n' , '--new' , action = "store_true" , help = 'Start New Chat' , dest = 'new' )
142
+ parser .add_argument ('-f' , '--function' , default = '' , help = 'Enable function call' , dest = 'function' )
143
+ # parser.add_argument('name', nargs='?', default="")
118
144
parser .add_argument ('-l' , '--linux' , action = "store_true" , help = 'Include an assistent message with Kernel/OS/shell' , dest = 'linux' )
119
145
parser .add_argument ('-m' , '--model' , action = "store_true" , help = 'List models available via OpenAIs API' , dest = 'model' )
120
146
parser .add_argument ('-x' , '--expert' , action = "store_true" , help = 'Toggle warning' , dest = 'expert' )
@@ -180,23 +206,62 @@ def main():
180
206
config .toggle_expert_mode ()
181
207
sys .exit ()
182
208
209
+ func = ""
210
+ if args .function :
211
+ func = args .function
212
+ if func == "" :
213
+ print ("No function provided. Exiting..." )
214
+ sys .exit ()
215
+ func = get_fun_def (func )
216
+ if func == "" :
217
+ print ('Function not found: ' + func )
218
+ sys .exit ()
219
+
183
220
if not args .prompt :
184
221
prompt = Prompt .ask ("Documentation Request" )
185
222
if prompt == "" :
186
223
print ("No prompt provided. Exiting..." )
187
224
sys .exit ()
188
225
else :
189
226
prompt = args .prompt
227
+ askDict = {'role' :'user' , 'content' :prompt }
190
228
if os .path .isfile (_session_file_ ):
191
229
messages = get_session ()
192
- messages .append ({ 'role' : 'user' , 'content' : prompt } )
230
+ messages .append (askDict )
193
231
else :
194
- messages = [{ 'role' : 'user' , 'content' : prompt } ]
232
+ messages = [askDict ]
195
233
196
234
with console .status (f"Phoning a friend... " , spinner = "pong" ):
197
- openai_response = post_completion (get_chat (messages ))
198
- console .print (Markdown (openai_response .strip ()))
199
- messages .append ({'role' :'assistant' , 'content' :openai_response .strip ()})
235
+ openai_response = get_chat (messages , func )
236
+ if openai_response .get ("function_call" ):
237
+ function_name = openai_response ["function_call" ]["name" ]
238
+ if function_name in available_functions :
239
+ fuction_to_call = available_functions [function_name ]
240
+ else :
241
+ print ('Bad returned function name from OpenAI API' )
242
+ print (openai_response )
243
+ messages .append (openai_response )
244
+ messages .append ({"role" : "function" , "name" : function_name , "content" : func })
245
+ function_args = json .loads (openai_response ["function_call" ]["arguments" ].strip ())
246
+ console .print (Markdown (function_args .get ("content" ).strip ()))
247
+ put_session (messages )
248
+ exit ()
249
+
250
+ #print(openai_response["function_call"]["arguments"].strip())
251
+ function_args = json .loads (openai_response ["function_call" ]["arguments" ].strip ())
252
+ function_response = fuction_to_call (
253
+ heading = function_args .get ("heading" ),
254
+ content = function_args .get ("content" ).strip (),
255
+ )
256
+ messages .append (openai_response )
257
+ messages .append ({"role" : "function" , "name" : function_name , "content" : function_response })
258
+
259
+ if function_response != 'stop' :
260
+ openai_response = get_chat (messages )
261
+ else :
262
+ openai_response .content = function_args .get ("content" )
263
+ console .print (Markdown (openai_response .content .strip ()))
264
+ messages .append ({'role' :'assistant' , 'content' :openai_response .content .strip ()})
200
265
put_session (messages )
201
266
202
267
0 commit comments