RickyIG commited on
Commit
59871eb
·
1 Parent(s): 8d86fff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -13
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import os
2
  import openai
3
  import sys
4
- import sympy
5
 
6
  import gradio as gr
7
  from IPython import get_ipython
@@ -10,14 +9,16 @@ import requests
10
  from tenacity import retry, wait_random_exponential, stop_after_attempt
11
  from IPython import get_ipython
12
  # from termcolor import colored # doesn't actually work in Colab ¯\_(ツ)_/¯
 
13
 
14
  GPT_MODEL = "gpt-3.5-turbo-1106"
15
 
16
- openai.api_key = os.environ['OPENAI_API_KEY']
 
17
 
18
  def exec_python(cell):
19
  # result = 0
20
- # print(cell)
21
  # print(type(cell))
22
  # code = json.loads(cell)
23
  # print(code)
@@ -27,13 +28,19 @@ def exec_python(cell):
27
  code = inputcode
28
  # code_string = code["cell"]
29
  local_namespace = {}
30
- exec(code, globals(), local_namespace)
 
 
 
31
  print(local_namespace)
32
- theanswers = local_namespace.values()
33
- local_ans = list(theanswers)[-1]
34
- print(theanswers)
35
- print(local_ans)
36
- return local_ans
 
 
 
37
 
38
  # Now let's define the function specification:
39
  functions = [
@@ -45,7 +52,7 @@ functions = [
45
  "properties": {
46
  "cell": {
47
  "type": "string",
48
- "description": "Valid Python cell to execute.",
49
  }
50
  },
51
  "required": ["cell"],
@@ -124,6 +131,7 @@ def chat_completion_request(messages, functions=None, function_call=None, model=
124
 
125
  # Set up the data for the API request
126
  json_data = {"model": model, "messages": messages}
 
127
  # json_data = {"model": model, "messages": messages, "temperature": 0.2, "top_p": 0.1}
128
 
129
  # If functions were provided, add them to the data
@@ -157,7 +165,7 @@ def first_call(init_prompt, user_input):
157
 
158
  # Generate a response
159
  chat_response = chat_completion_request(
160
- messages, functions=functions
161
  )
162
 
163
 
@@ -173,6 +181,12 @@ def first_call(init_prompt, user_input):
173
  # Let's see what we got back before continuing
174
  return assistant_message, cost1, messages
175
 
 
 
 
 
 
 
176
 
177
  def function_call_process(assistant_message):
178
  if assistant_message.get("function_call") != None:
@@ -182,11 +196,24 @@ def function_call_process(assistant_message):
182
 
183
  # Retrieve the arguments to send the function
184
  # function_args = json.loads(assistant_message["function_call"]["arguments"], strict=False)
185
- arg_dict = {'cell': assistant_message["function_call"]["arguments"]}
 
 
 
 
 
186
  # print(function_args)
187
 
 
 
 
 
 
 
 
 
188
  # Look up the function and call it with the provided arguments
189
- result = functions_dict[function_name](**arg_dict)
190
  return result
191
 
192
  # print(result)
 
1
  import os
2
  import openai
3
  import sys
 
4
 
5
  import gradio as gr
6
  from IPython import get_ipython
 
9
  from tenacity import retry, wait_random_exponential, stop_after_attempt
10
  from IPython import get_ipython
11
  # from termcolor import colored # doesn't actually work in Colab ¯\_(ツ)_/¯
12
+ import ast
13
 
14
  GPT_MODEL = "gpt-3.5-turbo-1106"
15
 
16
+ from google.colab import userdata
17
+ openai.api_key = userdata.get('OPENAI_API_KEY')
18
 
19
  def exec_python(cell):
20
  # result = 0
21
+ print(cell)
22
  # print(type(cell))
23
  # code = json.loads(cell)
24
  # print(code)
 
28
  code = inputcode
29
  # code_string = code["cell"]
30
  local_namespace = {}
31
+ try:
32
+ exec(code, globals(), local_namespace)
33
+ except Exception as e:
34
+ return "Please figure and solve it step by step with explanation by yourself. Remember, you must give the correct answer!"
35
  print(local_namespace)
36
+ if not local_namespace:
37
+ return "Please figure and solve it step by step with explanation by yourself. Remember, you must give the correct answer!"
38
+ else:
39
+ theanswers = local_namespace.values()
40
+ print(theanswers)
41
+ local_ans = list(theanswers)[-1]
42
+ print(local_ans)
43
+ return local_ans
44
 
45
  # Now let's define the function specification:
46
  functions = [
 
52
  "properties": {
53
  "cell": {
54
  "type": "string",
55
+ "description": "Valid Python code to execute.",
56
  }
57
  },
58
  "required": ["cell"],
 
131
 
132
  # Set up the data for the API request
133
  json_data = {"model": model, "messages": messages}
134
+ # json_data = {"model": model, "messages": messages, "response_format":{"type": "json_object"}}
135
  # json_data = {"model": model, "messages": messages, "temperature": 0.2, "top_p": 0.1}
136
 
137
  # If functions were provided, add them to the data
 
165
 
166
  # Generate a response
167
  chat_response = chat_completion_request(
168
+ messages, functions=functions, function_call='auto'
169
  )
170
 
171
 
 
181
  # Let's see what we got back before continuing
182
  return assistant_message, cost1, messages
183
 
184
+ def is_valid_dict_string(s):
185
+ try:
186
+ ast.literal_eval(s)
187
+ return True
188
+ except (SyntaxError, ValueError):
189
+ return False
190
 
191
  def function_call_process(assistant_message):
192
  if assistant_message.get("function_call") != None:
 
196
 
197
  # Retrieve the arguments to send the function
198
  # function_args = json.loads(assistant_message["function_call"]["arguments"], strict=False)
199
+
200
+ # if isinstance(assistant_message["function_call"]["arguments"], dict):
201
+ # arg_dict = json.loads(r"{jsonload}".format(jsonload=assistant_message["function_call"]["arguments"]), strict=False)
202
+ # else:
203
+ # arg_dict = {'cell': assistant_message["function_call"]["arguments"]}
204
+ # arg_dict = assistant_message["function_call"]["arguments"]
205
  # print(function_args)
206
 
207
+ if is_valid_dict_string(assistant_message["function_call"]["arguments"])==True:
208
+ arg_dict = json.loads(r"{jsonload}".format(jsonload=assistant_message["function_call"]["arguments"]), strict=False)
209
+ arg_dict = arg_dict['cell']
210
+ print("arg_dict : " + arg_dict)
211
+ else:
212
+ arg_dict = assistant_message["function_call"]["arguments"]
213
+ print(arg_dict)
214
+
215
  # Look up the function and call it with the provided arguments
216
+ result = functions_dict[function_name](arg_dict)
217
  return result
218
 
219
  # print(result)