Spaces:
Build error
Build error
Update app.py
Browse files11 unmodified lines12131415161715161718202122232425262728293031321920212223242526272829303132333435363738383940414278 unmodified lines12112212312412512612740 unmodified lines16816917016917117217317217317417517417517610 unmodified lines1871881891901911921931941931951961971981991982002012022038 unmodified lines21221321421321521621721821721922022122022122222322422522622722822923022722822923123223323423523623223723823924024124224323724424524624724824224925025125211 unmodified lines
GPT_MODEL = "gpt-3.5-turbo-1106"
openai.api_key = os.environ['OPENAI_API_KEY']
messages=[]from google.colab import userdataopenai.api_key = userdata.get('OPENAI_API_KEY')
def exec_python(cell): ipython = get_ipython() print(ipython) result = ipython.run_cell(cell) log = str(result.result) if result.error_before_exec is not None: log += f"\n{result.error_before_exec}" if result.error_in_exec is not None: log += f"\n{result.error_in_exec}" prompt = """You are a genius math tutor, Python code expert, and a helpful assistant. answer = {ans} Please answer user questions very well with explanations and match it with the multiple choices question. """.format(ans = log) return log # result = 0 # print(cell) # print(type(cell)) # code = json.loads(cell) # print(code) # exec(code["cell"]) inputcode = cell print(inputcode) code = inputcode # code_string = code["cell"] local_namespace = {} exec(code, globals(), local_namespace) print(local_namespace.values())
return result
# Now let's define the function specification:functions = [ { "name": "exec_python", "description": "run cell in ipython and return the execution result.", "description": "run python code and return the execution result.", "parameters": { "type": "object", "properties": {78 unmodified lines
# Set up the data for the API request json_data = {"model": model, "messages": messages} # json_data = {"model": model, "messages": messages, "temperature": 0.2, "top_p": 0.1}
# If functions were provided, add them to the data if functions is not None:40 unmodified lines cost1 = openai_api_calculate_cost(usage)
# Let's see what we got back before continuing return assistant_message, cost1 return assistant_message, cost1, messages
def second_prompt_build(prompt, log): prompt_second = prompt.format(ans = log) return prompt_second
def function_call_process(assistant_message): if assistant_message.get("function_call") != None:
10 unmodified lines return result
# print(result)def second_prompt_build(prompt, log): prompt_second = prompt.format(ans = log) # prompt_second = prompt % log return prompt_second
def second_call(prompt, result, function_name = "exec_python"):def second_call(prompt, prompt_second, messages, function_name = "exec_python"): # Add a new message to the conversation with the function result messages.append({ "role": "function", "name": function_name, "content": str(result), # Convert the result to a string "content": str(prompt_second), # Convert the result to a string })
# Call the model again to generate a user-facing message based on the function result8 unmodified lines
# Print the final conversation # pretty_print_conversation(messages) return assistant_message, cost2 return assistant_message, cost2, messages
def main_function(init_prompt, prompt, user_input): first_call_result, cost1 = first_call(init_prompt, user_input) first_call_result, cost1, messages = first_call(init_prompt, user_input) function_call_process_result = function_call_process(first_call_result) second_prompt_build_result = second_prompt_build(prompt, function_call_process_result) second_call_result, cost2 = second_call(second_prompt_build_result, function_call_process_result) return first_call_result, function_call_process_result, second_call_result, cost1, cost2 second_call_result, cost2, finalmessages = second_call(second_prompt_build_result, function_call_process_result, messages) finalcostresult = cost1 + cost2 finalcostrpresult = finalcostresult * 15000 return first_call_result, function_call_process_result, second_prompt_build_result, second_call_result, cost1, cost2, finalmessages, finalcostresult, finalcostrpresult
def gradio_function(): init_prompt = gr.Textbox(label="init_prompt (for 1st call)") prompt = gr.Textbox(label="prompt (for 2nd call)") user_input = gr.Textbox(label="User Input") output_1st_call = gr.Textbox(label="output_1st_call") output_fc_call = gr.Textbox(label="output_fc_call") output_2nd_call = gr.Textbox(label="output_2nd_call") output_1st_call = gr.JSON(label="Assistant (output_1st_call)") output_fc_call = gr.Textbox(label="Function Call (exec_python) Result (output_fc_call)") output_fc_call_with_prompt = gr.Textbox(label="Building 2nd Prompt (output_fc_call_with_2nd_prompt)") output_2nd_call = gr.JSON(label="Assistant (output_2nd_call_buat_user)") cost = gr.Textbox(label="Cost 1") cost2 = gr.Textbox(label="Cost 2")
finalcost = gr.Textbox(label="Final Cost ($)") finalcostrp = gr.Textbox(label="Final Cost (Rp)") finalmessages = gr.JSON(label="Final Messages")
iface = gr.Interface( fn=main_function, inputs=[init_prompt, prompt, user_input], outputs=[output_1st_call, output_fc_call, output_2nd_call, cost, cost2], outputs=[output_1st_call, output_fc_call, output_fc_call_with_prompt, output_2nd_call, cost, cost2, finalmessages, finalcost, finalcostrp], title="Test", description="Accuracy", )
iface.launch(share=True) iface.launch(share=True, debug=True)
if __name__ == "__main__": gradio_function()app.py+41-34