add history to conversation last 10 messages

modified:   Dockerfile
	modified:   app.py
This commit is contained in:
Russell Ballestrini 2023-10-21 11:56:44 -04:00
parent 6909622c09
commit 546518c604
2 changed files with 23 additions and 5 deletions

View file

@ -1,4 +1,4 @@
FROM python:latest FROM python:3.11
COPY requirements.txt /opt/requirements.txt COPY requirements.txt /opt/requirements.txt

26
app.py
View file

@ -35,8 +35,8 @@ class Message(db.Model):
# Create the database and tables # Create the database and tables
# with app.app_context(): with app.app_context():
# db.create_all() db.create_all()
@app.route("/") @app.route("/")
@ -86,9 +86,27 @@ def handle_message(data):
def chat_gpt(username, room, message): def chat_gpt(username, room, message):
# Send user's message to ChatGPT API
with app.app_context():
last_messages = (
Message.query.filter_by(room=room)
.order_by(Message.id.desc())
.limit(10)
.all()
)
# Format these messages as a chat history, with each message being a dict with 'role' and 'content'.
chat_history = [
{"role": "system" if msg.username == "GPT-3.5" else "user", "content": msg.content}
for msg in reversed(last_messages)
]
# Append the new message
chat_history.append({"role": "user", "content": message})
response = openai.ChatCompletion.create( response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", messages=[{"role": "user", "content": message}] model="gpt-3.5-turbo",
messages=chat_history
) )
# Extract response from ChatGPT API # Extract response from ChatGPT API