import os import openai import gradio as gr from langchain import OpenAI from langchain.chains import PALChain import datetime gpt_only_prompt = "Calculate the following, giving only the final answer:\n" prompt = "" # os.environ["OPENAI_API_KEY"] = "" def set_openai_api_key(api_key, openai_api_key, pal_chain): if api_key: openai_api_key = api_key os.environ["OPENAI_API_KEY"] = api_key llm = OpenAI(model_name='code-davinci-002', temperature=0, max_tokens=512) os.environ["OPENAI_API_KEY"] = "" pal_chain = PALChain.from_math_prompt(llm, verbose=True) return openai_api_key, pal_chain def openai_create(prompt, openai_api_key): print("prompt: " + prompt) # We use temperature of 0.0 because it gives the most predictable, factual answer (i.e. avoids hallucination). os.environ["OPENAI_API_KEY"] = openai_api_key response = openai.Completion.create( model="text-davinci-003", prompt=prompt, temperature=0.0, max_tokens=300, top_p=1, frequency_penalty=0, presence_penalty=0 ) os.environ["OPENAI_API_KEY"] = "" return response.choices[0].text def calc_gpt_only(math_problem, openai_api_key): if not openai_api_key or openai_api_key == "": return "
Please paste your OpenAI API key" answer = openai_create(gpt_only_prompt + math_problem + "\n", openai_api_key) print("\n==== date/time: " + str(datetime.datetime.now()) + " ====") print("calc_gpt_only math problem: " + math_problem) print("calc_gpt_only answer: " + answer) html = "
" + answer + "" return html def calc_gpt_pal(math_problem, pal_chain): if not pal_chain: return "
Please paste your OpenAI API key" answer = pal_chain.run(math_problem) print("\n==== date/time: " + str(datetime.datetime.now()) + " ====") print("calc_gpt_pal math problem: " + math_problem) print("calc_gpt_pal answer: " + answer) html = "
" + answer + "" return html block = gr.Blocks(css=".gradio-container {background-color: lightgray}") with block: with gr.Row(): title = gr.Markdown("""