Prompt engineering tutorial

Add memory to GPT-4

Teemu Maatta
4 min readMay 31


I integrate memory to GPT-4 in JSON-format.

Photo by Samsung Memory on Unsplash


In this tutorial, I will create a Large Language Model (LLM) application with an integrated memory¹.

  • Internal memory improves factual accuracy.

Humans write text with ability to ground knowledge from our memories, such as lived experiences.

I will use OpenAI API to feed and retrieve information using an external JSON-memory file.

Following this tutorial dos not require any additional packages beside OpenAI API python package.

Let’s get started!

Memory with GPT-4

Let´s start by adding the required packages and the OpenAI API key.

!pip install --upgrade openai #to upgrade latest OpenAI API-Python package
import openai
openai.api_key = os.getenv("OPENAI_API_KEY")

I next define the prompts to be used:

#Input processing
system_instruction = "You are a world-class prompt engineer producing right prompts to LLM in JSON format only to support programming anything. Only one overall objective. Prompts solve correctly anything by mapping up to 5, short, precise, measurable task/sub-task descriptions, task output types & task role to perform them & necessary concrete resource and/or memory name & use only status messages: Completed, In progress, Error, Rework. Use only single world to define the resource: for example Twitter, Github, Medium. Add the task description specific output type."
system_instruction_example = "print only the solution as a json:"
# Memory
define_memory = "Print only the task memory from the json: {}."
define_memory_system = f"You are a task memory selector. You only print memory name of the task. You only pick one memory, which is in the first task not completed yet and in status in progresss. In case no memory required: print No memory"

I next define the functions used:

# ALL API calls made usinng
def gpt_program(gpt_program_input, gpt_program_system, gpt_program_system_example, gpt_model, max_length):
---Generic GPT Program---
gpt_program_input: what is the input…



Teemu Maatta

Author (+200k views) in Artificial General Intelligence. Prompt Engineer. Madrid.