Hey y’all,
The goal of this post is to show what I built, and welcome all feedback and constructive criticism. I’m always looking for ways to lean and improve. Please limit your criticism to the content of this post.
If you go to ChatGPT (GTP-4) and ask it:
What is the current time and date?
…it will reply with something like:
I'm sorry, but I can't provide real-time information like the current time and date.
…because GTP is stateless.
This isn’t a problem if you are asking:
How many sides does a square have?
…but let’s say you are building a bot that is going to have a real text message conversation with a human??? Consider the User sending this text message:
Hey, what's the closest McDonald's to you? How long do we have to get there before it closes?
Normal conversations are impossible without understanding when “now” is, and comprehending the age of old messages. Here’s how I solved that…
If you have only been using GPT through ChatGPT, you probably need to read up on the different types of messages you can send via the API, specifically the system prompt (docs here) that you don’t have access to with ChatGPT. You can use the Playground to get access to and start playing with system prompts.
The first thing I did was add this to the top of my 1st system prompt:
The current time now is the time of the last message: 0000-00-00 00:00:00
When a User sends a message in, that time/date stamp gets updated, and GPT reads that data first, setting the context of “now” for its reply, which is built a fraction of a second after.
Then I added a timestamp to the beginning of each and all individual messages, showing when the message was created. This equips GPT answer questions like:
What were we talking about last Friday night?
NOTE: I filter the timestamp out of the content the User sees with this RegEx:
str.gsub(/\[\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\]\s*/, '')
It now knows “now” and understands “history” - but I forgot to tell it WTF to do with this information… so I added to my system prompt:
Pay attention to the date/time stamps and adjust your responses based on the time that has passed between messages.
…which got me very close to what I wanted…
The problem was that it somehow didn’t understand the “relationship between all the timestamps”
Okay, so it’s a “language model” and I discovered it needed a “story” to understand the relationships between the messages… which I solved like this…
When a new message comes in from a User, I set a random number on my server between 11 and 17 (slight randomness creates believability). This number represents the AI’s current concept of at what point “now” becomes “past” in terms of minutes.
So if I send a new text message to the AI, that number may get set to “13 minutes” and if my previous message was sent more than 13 minutes ago, I have a narrative to create… I inject a new system prompt right before the newest User message that says the previous message happened X number of minutes ago, something like:
NOTE: It has been X minutes since Drawk's last text message.
I ended up using the Ruby on Rails time_ago_in_words() for this because “human readable” worked better than something like “217 minutes” in the end. Prompt Engineering is like talking to a child, and “217 minutes” is difficult for a young child to understand in terms of passage of time.
Now it has a “story” it can understand
The realism that gave is (and I’m choosing my next word very deliberately) FRIGHTENING!