Skip to content

Instantly share code, notes, and snippets.

@Samin100
Created July 11, 2021 01:02
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Samin100/6cec8c3f9e5d68e0776fcac6e5ba86aa to your computer and use it in GitHub Desktop.
Save Samin100/6cec8c3f9e5d68e0776fcac6e5ba86aa to your computer and use it in GitHub Desktop.
import os
import openai
from math import *
import math
import numpy
openai.api_key = os.getenv("OPENAI_API_KEY")
def get_prompt(question):
# Building the prompt from a question
return f"""This file only contains one function, and it's purpose is to answer the following question:
Question: "{question}"
\"\"\"
def answer_question():
\"\"\"
This function returns the answer to the question asked in the docstring above.
\"\"\""""
def get_response(question):
response = openai.Completion.create(
engine="davinci",
prompt=get_prompt(question),
temperature=0.6,
max_tokens=512,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=["def", "answer_question()", "if __name__", "print"]
)
return response["choices"][0]["text"]
if __name__ == "__main__":
while True:
question = input("\nGPT-Calc: ")
code = get_response(question)
eval_function = f"""def answer_question():{code}"""
# print(eval_function)
exec(eval_function)
answer = answer_question()
print("Answer: \033[1;32;40m" + str(answer) + "\033[39m \033[0;0m")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment