Search code examples
openai-apichatgpt-api

How to remember messages from different ChatCompletion Instances


I'm trying to run some commands with the chatgpt api and its not running everything so I made another ChatCompletion instance. How can I get chatgpt to remember the prompts I sent in a previous instance? Here is my code:

data = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
        {"role": "user", "content": "Generate a title"},
        {"role": "user", "content": "Generate 30 chapters"},
    ]
)

data2 = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
          {"role": "user", "content": "Generate chapter 1"},
])

Solution

  • To be able to Keep the Model in context, You must send previous conversations along with the current prompt.

    [Note: Conversation History Must Be less than Token Limit in Order to Expect Proper Behaviour From the Model, To avoid this you can summarize the conversation after nth response or use text-embedding ]

    Here is a example code:

    import openai
    import time
    
    # Set your OpenAI API key
    openai.api_key = "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    
    # Define the engine and the query
    engine = "gpt-3.5-turbo"
    query = "Generate a title"
    
    # Start a new conversation with an empty list of messages
    messages = []
    
    # Call the Chat Completion API to generate a title
    data = openai.ChatCompletion.create(
      engine=engine,
      query=query,
      messages=messages
    )
    
    # Print the generated title
    title = data["message"]
    print(title)
    
    # Update the messages with the title
    messages.append({"role": "assistant", "content": title})
    
    # Define a function to generate chapters
    def generate_chapters(n):
      # Loop for n times
      for i in range(n):
        # Generate a query for each chapter
        query = f"Generate chapter {i+1}"
        # Call the Chat Completion API to generate a chapter
        data = openai.ChatCompletion.create(
          engine=engine,
          query=query,
          messages=messages
        )
        # Print the generated chapter
        chapter = data["message"]
        print(chapter)
        # Update the messages with the chapter
        messages.append({"role": "assistant", "content": chapter})
        # Wait for 10 seconds before generating the next chapter
        time.sleep(10)
    
    # Generate 30 chapters
    generate_chapters(30)