Spaces:
Build error
Build error
Update app.py
Browse files11 unmodified lines12131415161718192021222324252627282930313233343536373839404178 unmodified lines12012112212312412540 unmodified lines16616716816917017117217317417517617717810 unmodified lines1891901911921931941951961971981992002018 unmodified lines21021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424511 unmodified linesGPT_MODEL = "gpt-3.5-turbo-1106"openai.api_key = os.environ['OPENAI_API_KEY']messages=[]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# Now let's define the function specification:functions = [{"name": "exec_python","description": "run cell in ipython and return the execution result.","parameters": {"type": "object","properties": {78 unmodified lines# Set up the data for the API requestjson_data = {"model": model, "messages": messages}# If functions were provided, add them to the dataif functions is not None:40 unmodified linescost1 = openai_api_calculate_cost(usage)# Let's see what we got back before continuingreturn assistant_message, cost1def second_prompt_build(prompt, log):prompt_second = prompt.format(ans = log)return prompt_seconddef function_call_process(assistant_message):if assistant_message.get("function_call") != None:10 unmodified linesreturn result# print(result)def second_call(prompt, result, function_name = "exec_python"):# Add a new message to the conversation with the function resultmessages.append({"role": "function","name": function_name,"content": str(result), # 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, cost2def main_function(init_prompt, prompt, user_input):first_call_result, cost1 = 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, cost2def 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")cost = gr.Textbox(label="Cost 1")cost2 = gr.Textbox(label="Cost 2")iface = gr.Interface(fn=main_function,inputs=[init_prompt, prompt, user_input],outputs=[output_1st_call, output_fc_call, output_2nd_call, cost, cost2],title="Test",description="Accuracy",)iface.launch(share=True)if __name__ == "__main__":gradio_function()11 unmodified lines1213141516171819202122232425262728293031323334353637383940414278 unmodified lines12112212312412512612740 unmodified lines16816917017117217317417517610 unmodified lines1871881891901911921931941951961971981992002012022038 unmodified lines21221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125211 unmodified linesGPT_MODEL = "gpt-3.5-turbo-1106"from google.colab import userdataopenai.api_key = userdata.get('OPENAI_API_KEY')def exec_python(cell):# result = 0# print(cell)# print(type(cell))# code = json.loads(cell)# print(code)# exec(code["cell"])inputcode = cellprint(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 python code and return the execution result.","parameters": {"type": "object","properties": {78 unmodified lines# Set up the data for the API requestjson_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 dataif functions is not None:40 unmodified linescost1 = openai_api_calculate_cost(usage)# Let's see what we got back before continuingreturn assistant_message, cost1, messagesdef function_call_process(assistant_message):if assistant_message.get("function_call") != None:10 unmodified linesreturn result# print(result)def second_prompt_build(prompt, log):prompt_second = prompt.format(ans = log)# prompt_second = prompt % logreturn prompt_seconddef second_call(prompt, prompt_second, messages, function_name = "exec_python"):# Add a new message to the conversation with the function resultmessages.append({"role": "function","name": function_name,"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, messagesdef main_function(init_prompt, 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, finalmessages = second_call(second_prompt_build_result, function_call_process_result, messages)finalcostresult = cost1 + cost2finalcostrpresult = finalcostresult * 15000return first_call_result, function_call_process_result, second_prompt_build_result, second_call_result, cost1, cost2, finalmessages, finalcostresult, finalcostrpresultdef 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.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_fc_call_with_prompt, output_2nd_call, cost, cost2, finalmessages, finalcost, finalcostrp],title="Test",description="Accuracy",)iface.launch(share=True, debug=True)if __name__ == "__main__":gradio_function()
app.py+41-34