How to make GPT 3.5 Turbo remember the last output?

I coded a script using Python that uses the OpenAI API to generate articles. The way it works is by generating an article outline from a keyword. Then, it takes that outline and generates the text for each section, one by one. Instead of generating the whole article at once, I found that generating it in sections based on the different headings in the outline, gave me a higher-quality article at the end.

Anyway, I had this working fine and was happy with it. However, since switching over to the gpt-3.5-turbo model, I’ve been having some issues. To me, it seems that when the code generates the text for each new section, it has “forgotten” what it previously generated. This means that each section starts with the same sentence. Overall, the article doesn’t flow together correctly.

Here is an example of the output im getting, so you can see what I mean:H2: How to Interpret Your Dream about Teeth Falling Out

Hey there! So, you’re curious about dreams where your teeth fall out? It’s actually a pretty common dream that many people experience. But what does it mean?

Well, dreams about teeth falling out can have different interpretations depending on the person and their personal experiences. Generally, though, it’s believed to represent feelings of insecurity or vulnerability. Teeth are often associated with our appearance and how we present ourselves to others, so losing them in a dream can symbolize a fear of losing control or power.

For example, I once had a dream where all my teeth fell out while I was giving a presentation at work. I felt embarrassed and exposed in front of my colleagues. Looking back, I realized that I was feeling insecure about my abilities at work and worried about being judged by others.

But don’t worry - not all dreams about teeth falling out are negative! Some people interpret them as a sign of growth or transformation. Losing old teeth can represent shedding old habits or beliefs to make way for new ones.

So next time you have a dream about your teeth falling out, take some time to reflect on your current emotions and experiences. What could this dream be trying to tell you? And remember, it’s just a dream - don’t let it cause unnecessary stress or anxiety in your waking life.

H2: How to Cope with a Dream about Teeth Falling Out

Have you ever had a dream about your teeth falling out? It’s a common dream that can leave you feeling anxious and confused. But what does it mean? And how can you cope with the emotions it brings up?

First, let’s delve into the science behind dreams. Dreams are a natural part of our sleep cycle and occur during the rapid eye movement (REM) stage. During this time, our brains are highly active and processing information from our daily lives.

Research studies have shown that dreams can be influenced by our emotions, experiences, and even our physical state. For example, if you’re feeling stressed or anxious, you may be more likely to have a dream about your teeth falling out.

But what does this dream actually mean? There are many interpretations, but some psychologists believe that it could represent feelings of insecurity or powerlessness. Teeth are often associated with confidence and self-image, so losing them in a dream could symbolize a loss of control or fear of judgment from others.

So how can you cope with these emotions? One approach is to try to identify any underlying stressors in your life and work on addressing them. This could involve talking to a therapist or practicing relaxation techniques like meditation or yoga.

It’s also important to remember that dreams are not always literal representations of reality. Just because you had a dream about your teeth falling out doesn’t necessarily mean it will happen in real life.

In conclusion, while dreams about teeth falling out can be unsettling, they are a normal part of the sleep cycle and can provide insight into our emotional state. By understanding the science behind dreams and working on coping strategies for any underlying stressors, we can learn to navigate these experiences with greater ease.

Now, the easy solution would be to switch over to using text-davinci-003 as I had been originally. But, im curious to see the level of output I can get using the new gpt-3.5-turbo model (once I get it working correctly).

Does anyone have any idea of how I can make the AI “remember”, using gpt-3.5-turbo model. Any tips on how to make my article flow together, instead of each section being written in a way that looks like it’s the start of the article, would be much appreciated.

Below is the section of my code that generates each section of the article. If anyone has any ideas, then let me know, please. I coded this using ChatGPT with no prior coding knowledge, so forgive me if the code is messy.

# function to generate articles
def generate_article(outline, keyword):
article = []
headings = re.findall(r"<h[23]>(.*?)</h[23]>", outline)
headings_list = []
for heading_text in headings:
# remove any irrelevant headings
if heading_text.lower().startswith("introduction") or \
heading_text.lower().startswith("conclusion") or \
len(heading_text.split()) < 2:
continue
# remove any duplicate headings
if heading_text in headings_list:
continue
if not headings_list:
headings_list.append(heading_text)
continue
headings_list.append(heading_text)
memory = []
# Add some variation to the prompts for each section
prompt_list = [
{"role": "user", "content": f"Take your readers on a step-by-step journey through '{heading_text}', using '{keyword}' as a framework. Use clear and concise language to explain each step. Vary your sentence structures to keep your readers engaged. Break up your text into short paragraphs. Do not repeat phrases. use varied language. Your tone should be friendly and casual, and you should avoid writing '{heading_text}' in the output. Use bullet points where appropriate to make your content more accessible."},
{"role": "user", "content": f"Share your expertise on '{heading_text}' as it relates to '{keyword}'. Use personal stories and experiences to connect with your readers, and keep your writing lively and interesting by avoiding overused phrases. Ask rhetorical questions to help encourage the reader to think more deeply about your topic. Break up your text into short paragraphs to make your text easy to read. Do not repeat phrases. use varied language. Your tone should be friendly and casual. Avoid writing '{heading_text}' in the output."},
{"role": "user", "content": f"Provide a fresh perspective on '{keyword}', focusing on '{heading_text}'. Use interesting and thought-provoking language to engage the reader. Do not repeat phrases, use varied language. Break up your text into short paragraphs. Your tone should be friendly and casual. Do not write '{heading_text}' in the output. Use bullet points where appropriate to make your content more accessible."},
]

# Randomly select one of the prompts for each section
messages = [random.choice(prompt_list)]
messages.append({"role": "user", "content": ''.join(memory)})
model = "gpt-3.5-turbo"
try:
body = openai.ChatCompletion.create(
model=model,
messages=messages,
max_tokens=500,
n=1,
stop=None,
temperature=0.3,
top_p=0.2,
frequency_penalty=0.5,
presence_penalty=0.5,
)

# Format the generated text
message = body['choices'][0]['message']['content'].strip().replace('\n* ', '\n<li>')
message = message.replace('* ', '<li>')
message = message.replace('\n\n', '\n')
message = message.replace('\n', '</li>\n')
message = f"<ul>\n{message}</ul>" if '<li>' in message else f"<div><p>{message}</p></div>"

# Split the message into paragraphs
paragraphs = message.split('\n\n')

# Join paragraphs into groups of 3 paragraphs each
group_size = 3
grouped_paragraphs = [paragraphs[i:i+group_size] for i in range(0, len(paragraphs), group_size)]

# Join each group of paragraphs into a single string
messages = []
for group in grouped_paragraphs:
message = '\n\n'.join(group)
# Remove the last character of the last paragraph if it is a full stop
if message[-1] == '.':
message = message.rstrip('.')
messages.append('<p>' + message.strip() + '</p>\n')
# Join all the messages into a single string
message = ''.join(messages)

article.append(f"<h2>{heading_text}</h2>\n{message}")
print(f"Success: Section '{heading_text}' has been written")

except Exception as e:
print(f"Error generating article for '{heading_text}': {e}")
return ""

return "".join(article)

This is pretty insteresting!
I have managed to get to the same approach using davinci03 and separating everything on sections, besides the cost advantage of 3.5-turbo, is there anything else that you found worthy?

I have not tried using 3.5-t yet, but can it be that on davinci all was a unique prompt?
I see that 3.5-t has some issues with system commands, not sure if you can use user with a prompt as “Remember what you wrote before” or similar.

Reading the user prompt you can try.
Make a post following the instructions below:
(Write here the instructions).

Try to include your entire outline at the top of each section request prompt, it works fine for me, i have similar setup like yours.

Instruct it to write only section X and not to write the whole post at once.That way it will understand the context. In most cases, (depending on temp and other settings) it will not write repetetive sentences.Im keeping my temp low at 0.2 or 0.15 because it starts to halucinate pretty fast in my niche.