Remembering the previous conversation of a chatbot

Question:

I have created a basic ChatBot using OpenAI with the following code:

import openai
openai.api_key = "sk-xxx"
while True:
    prompt = input("User:")
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt=prompt,
        max_tokens=50,
        temperature=0,
    )
    print(response.choices[0].text)

This is the input and output:

enter image description here

And in text form:

User:What is python
Python is a high-level, interpreted, general-purpose programming language. It is a powerful and versatile language that is used for a wide range of applications, from web development and software development to data science and machine learning.`
User:What is the Latest Version of it? 
The latest version of Microsoft Office is Microsoft Office 2019. 

As you can see i am asking questions related to python and asking version of it gives answer related to Microsoft Office, whereas when asking the same question to ChatGpt it remembers the previous conservation and acts according to it.

Is there any solution for remembering the conversation?

Asked By: Andrea Bency

||

Answers:

One possibility would be to store the inputs and outputs somewhere, and then include them in subsequent inputs. This is very rudimentary but you could do something like the following:

inputs, outputs = [], []

while True:
    prompt = input("Enter input (or 'quit' to exist):")
    if prompt == 'quit':
        break

    inputs.append(prompt)
    if len(inputs) > 0:
        last_input, last_output = inputs[-1], outputs[-1]
        prompt = f"{prompt} (based on my previous question: {last_input}, and your previous answer {last_output}"

    response = openai.Completion.create(
        model="text-davinci-003",
        prompt=prompt,
        max_tokens=200,
        temperature=0,
    )
    
    output = response.choices[0].text
    outputs.append(output)
    
    print(output)

This program would be able to recall the last input and output, and provide that information along with the current prompt. You could include more lines of input and output depending on how much "memory" you want your program to have, and there is also a text limit (max_tokens), so you may need to adjust the wording so that the entire prompt makes sense.

And to avoid an infinite loop, we can have a condition to exit the while loop.

Answered By: Derek O
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.