Created
May 9, 2023 14:19
-
-
Save simonmesmith/c4c0315b513ddf9d989859ac9a8d4e5c to your computer and use it in GitHub Desktop.
"SmartGPT" experiment inspired by "GPT 4 is Smarter than You Think" video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import openai | |
from tqdm import tqdm | |
import os | |
def gpt(prompt: str) -> str: | |
""" | |
This function uses the OpenAI API to generate a response to a given prompt. | |
Args: | |
prompt (str): Input string to generate a response for. | |
Returns: | |
str: Response generated by GPT-4. | |
""" | |
completion = openai.ChatCompletion.create( | |
model="gpt-4", messages=[{"role": "user", "content": prompt}] | |
) | |
return completion.choices[0].message["content"] | |
def generate_answers(question: str, number_of_answers: int = 3) -> str: | |
""" | |
This function generates multiple answers for a given question. | |
Args: | |
question (str): The question to generate answers for. | |
number_of_answers (int, optional): The number of answers to generate. Defaults to 3. | |
Returns: | |
str: A string containing all generated answers. | |
""" | |
print("Generating answers:") | |
answers = [] | |
for i in tqdm(range(1, number_of_answers + 1)): | |
answer = generate_answer(question) | |
answers.append(f"Answer {i}:\n{answer}\n") | |
return "\n".join(answers) | |
def generate_answer(question: str) -> str: | |
""" | |
This function generates an answer for a given question using the gpt function. | |
Args: | |
question (str): The question to generate an answer for. | |
Returns: | |
str: Generated answer. | |
""" | |
return gpt( | |
f"Question: {question}\n\n" | |
"Answer: Let's work this out in a step by step way to be sure we have the right answer." | |
) | |
def critique_answers(question: str, generated_answers: str) -> str: | |
""" | |
This function critiques the generated answers for a given question. | |
Args: | |
question (str): The question that was answered. | |
generated_answers (str): The answers that were generated for the question. | |
Returns: | |
str: Critique of the generated answers. | |
""" | |
print("Critiquing answers.") | |
return gpt( | |
f"Question: {question}\n\n" | |
f"Answers:\n\n{generated_answers}\n\n" | |
"You are a researcher tasked with critiquing the answers to the question provided above. List the flaws and faulty logic of each answer option. Let's work this out in a step by step way to be sure we have all the errors." | |
) | |
def finalize_answer(question: str, generated_answers: str, critique: str) -> str: | |
""" | |
This function finalizes an answer based on the answers and critiques generated. | |
Args: | |
question (str): The question that was answered. | |
generated_answers (str): The answers that were generated for the question. | |
critique (str): The critique of the generated answers. | |
Returns: | |
str: The selected answer. | |
""" | |
print("Finalizing answer.") | |
return gpt( | |
f"Question: {question}\n\n" | |
f"Answers:\n\n{generated_answers}\n\n" | |
f"Critique:\n\n{critique}\n\n" | |
"You are the final decision maker tasked with producing a final answer to the specified question using the generated answers and criticism of them as input. Let's work this out in a step by step way to be sure we have the right answer:" | |
) | |
if __name__ == "__main__": | |
openai.api_key = os.environ["OPENAI_API_KEY"] | |
number_of_answers = 3 # Change this to increase or decrease the number of answers given to the reviewer | |
verbose = True # Change this to False to hide the generated answers and review, and only seee the final answer | |
question = input("Enter your question: ") | |
generated_answers = generate_answers(question, number_of_answers) | |
if verbose: | |
print("Generated answers:") | |
print(generated_answers) | |
critique = critique_answers(question, generated_answers) | |
if verbose: | |
print("Critique:") | |
print(critique) | |
final_answer = finalize_answer(question, generated_answers, critique) | |
print(final_answer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is some simple code inspired by the video GPT 4 is Smarter than You Think: Introducing SmartGPT
. It follows the general idea, though not the wording exactly. Unfortunately, when I ran it on the question "I left 5 clothes to dry out in the sun. It took them 5 hours to dry completely. How long would it take to dry 30 clothes?" it still got the answer very wrong.