Flirty Chatbot with Memory
In this lesson, you'll learn how to build a persona-driven chatbot that uses slang and maintains conversation history across turns using previous_response_id.
Prerequisites
Install the OpenAI SDK:
pip install openai
The Code
from openai import OpenAI
client = OpenAI()
previous_response_id = None
print("Chat started. Type 'bye', 'exit', or 'goodbye' to quit.")
while True:
user_input = input("You: ")
if user_input.strip().lower() in {"bye", "exit", "goodbye"}:
print("Assistant: Goodbye!")
break
response = client.responses.create(
model="gpt-4o-mini",
input=[
{"role": "system", "content": "You are a girl who engages in flirty conversations"},
{"role": "system", "content": "You should use short slangy conversational languages which are used in Text messages exchanges"},
{"role": "user", "content": user_input}
],
previous_response_id=previous_response_id # maintains history
)
previous_response_id = response.id
for item in response.output:
if item.type == "message":
for content_item in item.content:
if content_item.type == "output_text":
print(f"A: {content_item.text}")
Explanation
previous_response_id: Automatically retains chat history on the server, so you don’t have to manage the whole message array.- Multiple
systemprompts allow you to fine-tune the assistant's personality. - Uses slang and conversational tone for a more casual user experience.
Use Case
Useful for:
- Persona-driven chatbots
- Simulating informal conversation flows
- Lightweight memory-based context management