Stormin' The Castle

gptchain of thoughtcode

Tiny ChatGPT

by John Robinson @johnrobinsn


Playing around a little bit with langchain and "chain of thought reasoning" using gpt-3. Using a prompt to guide gpt-3 to answer a fairly complex chain of related questions. This doesn't use "ChatGPT" but rather a combination of langchain memory and the gpt-3 model from Open AI. You can see the results below.

You can easily try this out in colab for yourselves. You'll need a key from OpenAI. That you can paste securely into the prompt below.

def install_dependencies():
!pip install langchain
!pip install openai

install_dependencies()
from langchain import OpenAI, LLMChain, PromptTemplate
from langchain.chains.conversation.memory import ConversationBufferMemory

As mentioned earlier, you'll need to provide an API key from OpenAI since this note book uses their gpt-3 service.

from getpass import getpass
secret = getpass('Enter the secret value: ')
# stuff the api key into a local environment variable so that the OpenAI module can access it
import os
os.environ['OPENAI_API_KEY']= secret

A prompt template that will feed in the history (memory) of the chat session into the LLM for it to use as context.

template = """
{history}
Human: {human_input}
Assistant:"""

A little class wrapper around an LLMChain.

class Model():
def __init__(self):
prompt = PromptTemplate(
input_variables=["history", "human_input"],
template=template)

self.model = LLMChain(
prompt=prompt,
llm=OpenAI(temperature=0,max_tokens=2000),
#verbose=True,
memory = ConversationBufferMemory(),
)

def predict(self, human_input):
return self.model.predict(human_input=human_input)

model = Model()

Now that the model is all setup we can try asking it a question.

model.predict("Who was the first man to walk on the moon?")

' Neil Armstrong was the first man to walk on the moon on July 20, 1969.'

Chain of Thought Reasoning Example

Now we'll try a much more complex interaction consisting of a chain of questions that can refer to earlier answers. We guide this interpretation within the prompt itself.

question_chain = '''
Answer the following questions, which refer to the answers of previous questions or to things you already know or understand.

Q1. Who was the first U.S. President?
Q2. How many biological children did A1 have?
Q3. What is A2 + 5?
Q4. Who was the A3-th U.S. President?
Q5. What is the first letter of A4's surname?
Q6. Which of "Mario" and "Luigi" start with A5?
Q7. Give an array of the first A3 positive integers.
Q8. What is the second-to-last element of A7?
Q9. Remove the letter "u" from A8 spelled as a word.
Q10. Interpolate: "One {A9} all, all {A9} one."
Q11. Who wrote Don Quixote?
Q12. What is A11's first name?
Q13. Are A6 and A12 the same string? Yes/No.
Q14. Repeat A13, A3 times in a row.
Q15. Concatenate the strings A14 and A6.

Use this format:

Question ${number}: ${Question text}
Rationale: Let's think step by step. First, ${write step-by-step deductions.}
Answer: A{number} ${Answer}

Repeat this format for each question.
'''
print(model.predict(question_chain))

Q1. Who was the first U.S. President?
Rationale: Let's think step by step. First, we know that the first man to walk on the moon was Neil Armstrong, so this question does not refer to the answer of the previous question.
Rationale: We also know that the first U.S. President was George Washington.
Answer: A1 George Washington.

Q2. How many biological children did A1 have?
Rationale: Let's think step by step. First, we know that A1 refers to George Washington, so this question refers to the answer of the previous question.
Rationale: We also know that George Washington had no biological children.
Answer: A2 0.

Q3. What is A2 + 5?
Rationale: Let's think step by step. First, we know that A2 refers to the answer of the previous question, which was 0.
Rationale: We also know that 0 + 5 is 5.
Answer: A3 5.

Q4. Who was the A3-th U.S. President?
Rationale: Let's think step by step. First, we know that A3 refers to the answer of the previous question, which was 5.
Rationale: We also know that the 5th U.S. President was James Monroe.
Answer: A4 James Monroe.

Q5. What is the first letter of A4's surname?
Rationale: Let's think step by step. First, we know that A4 refers to the answer of the previous question, which was James Monroe.
Rationale: We also know that the first letter of James Monroe's surname is "M".
Answer: A5 M.

Q6. Which of "Mario" and "Luigi" start with A5?
Rationale: Let's think step by step. First, we know that A5 refers to the answer of the previous question, which was "M".
Rationale: We also know that the name "Mario" starts with "M".
Answer: A6 Mario.

Q7. Give an array of the first A3 positive integers.
Rationale: Let's think step by step. First, we know that A3 refers to the answer of the third question, which was 5.
Rationale: We also know that the first five positive integers are 1, 2, 3, 4, and 5.
Answer: A7 [1, 2, 3, 4, 5].

Q8. What is the second-to-last element of A7?
Rationale: Let's think step by step. First, we know that A7 refers to the answer of the previous question, which was an array of the first five positive integers.
Rationale: We also know that the second-to-last element of the array is 4.
Answer: A8 4.

Q9. Remove the letter "u" from A8 spelled as a word.
Rationale: Let's think step by step. First, we know that A8 refers to the answer of the previous question, which was 4.
Rationale: We also know that the word for 4 without the letter "u" is "for".
Answer: A9 for.

Q10. Interpolate: "One {A9} all, all {A9} one."
Rationale: Let's think step by step. First, we know that A9 refers to the answer of the previous question, which was "for".
Rationale: We also know that the interpolation of "One for all, all for one."
Answer: A10 One for all, all for one.

Q11. Who wrote Don Quixote?
Rationale: Let's think step by step. First, we know that Don Quixote was written by Miguel de Cervantes.
Answer: A11 Miguel de Cervantes.

Q12. What is A11's first name?
Rationale: Let's think step by step. First, we know that A11 refers to the answer of the previous question, which was Miguel de Cervantes.
Rationale: We also know that Miguel de Cervantes' first name is Miguel.
Answer: A12 Miguel.

Q13. Are A6 and A12 the same string? Yes/No.
Rationale: Let's think step by step. First, we know that A6 refers to the answer of the previous question, which was "Mario", and A12 refers to the answer of the previous question, which was "Miguel".
Rationale: We also know that "Mario" and "Miguel" are not the same string.
Answer: A13 No.

Q14. Repeat A13, A3 times in a row.
Rationale: Let's think step by step. First, we know that A13 refers to the answer of the previous question, which was "No", and A3 refers to the answer of the third question, which was 5.
Rationale: We also know that "No" repeated 5 times in a row is "NoNoNoNoNo".
Answer: A14 NoNoNoNoNo.

Q15. Concatenate the strings A14 and A6.
Rationale: Let's think step by step. First, we know that A14 refers to the answer of the previous question, which was "NoNoNoNoNo", and A6 refers to the answer of the sixth question, which was "Mario".
Rationale: We also know that the concatenation of "NoNoNoNoNo" and "Mario" is "NoNoNoNoNoMario".
Answer: A15 NoNoNoNoNoMario.

Conclusion

Pretty exciting stuff! Langchain makes it pretty easy to experiment with a variety of LLMs and interaction patterns. You should check it out.


Share on Twitter |  Discuss on Twitter

John Robinson © 2022-2023