uses the openai syntax for the conversation dump including content & role

* user
* system

	modified:   app.py
	modified:   templates/base.html
	modified:   templates/chat.html
This commit is contained in:
Russell Ballestrini 2024-09-15 08:57:34 -04:00
parent e310217166
commit 6086d65f9b
3 changed files with 73 additions and 22 deletions

45
app.py
View file

@ -16,7 +16,14 @@ import random
import boto3
import tiktoken
import together
from flask import Flask, render_template, request, send_from_directory
from flask import (
Flask,
render_template,
request,
send_from_directory,
jsonify,
Response,
)
from flask_socketio import SocketIO, emit, join_room
@ -96,7 +103,7 @@ HELP_MESSAGE = """
- `gpt-4`: For GPT-4, send a message with `gpt-4` and include your prompt.
- `gpt-4o-2024-08-06`: For the cheapest version of GPT-4o, send a message with `gpt-4o-2024-08-06` and include your prompt.
- `gpt-mini`: For GPT-4o-mini, send a message with `gpt-mini` and include your prompt.
- `gpt-o1-mini`: For GPT-4o-mini, send a message with `gpt-mini` and include your prompt.
- `gpt-o1-mini`: For GPT-o1-mini, send a message with `gpt-o1-mini` and include your prompt.
- `claude-haiku`: For Claude-haiku, send a message with `claude-haiku` and include your prompt.
- `claude-sonnet`: For Claude-sonnet, send a message with `claude-sonnet` and include your prompt.
- `claude-opus`: For Claude-opus, send a message with `claude-opus` and include your prompt.
@ -272,6 +279,40 @@ def chat(room_name):
)
@app.route("/download_chat_history", methods=["GET"])
def download_chat_history():
room_name = request.args.get("room_name")
room = get_room(room_name)
if not room:
return jsonify({"error": "Room not found"}), 404
messages = Message.query.filter_by(room_id=room.id).all()
if not messages:
return jsonify({"error": "No messages found"}), 404
chat_history = [
{
"role": "system" if message.username in system_users else "user",
"content": message.content,
}
for message in messages
if not message.is_base64_image()
]
if not chat_history:
return jsonify({"error": "No valid messages found"}), 404
response = Response(
response=json.dumps(chat_history, indent=2),
status=200,
mimetype="application/json",
)
response.headers["Content-Disposition"] = f"attachment; filename={room.name}.json"
return response
@app.route("/search")
def search_page():
# Query all rooms so that newest is first.

View file

@ -163,6 +163,12 @@
display: block;
padding-right: 0.8em; /* Adjust the padding as needed */
}
#downloadButton {
position: fixed;
top: 10px;
right: 10px;
z-index: 1000;
}
</style>
</head>
@ -175,6 +181,7 @@
<input type="hidden" id="username" name="username" value="{{ username }}">
</form>
</div>
<!-- Chatroom list -->
<div class="main-container">
<div id="rooms-list">
@ -190,30 +197,31 @@
{% endfor %}
</ul>
</div>
{% block content %}{% endblock %}
</div>
<script>
// Function to perform the search
function performSearch() {
const keywords = document.getElementById("search-keywords").value;
const username = document.getElementById("username").value;
if (!keywords) {
alert("Please enter keywords to search.");
return;
}
<script>
// Function to perform the search
function performSearch() {
const keywords = document.getElementById("search-keywords").value;
const username = document.getElementById("username").value;
if (!keywords) {
alert("Please enter keywords to search.");
return;
}
// Navigate to the search results page with the keywords as a query parameter
window.location.href = `/search?keywords=${encodeURIComponent(keywords)}&username=${username}`;
}
// Navigate to the search results page with the keywords as a query parameter
window.location.href = `/search?keywords=${encodeURIComponent(keywords)}&username=${username}`;
}
// Add event listener for keyword search the "Enter" key
document.getElementById("search-keywords").addEventListener("keydown", function(event) {
if (event.key === "Enter") {
event.preventDefault();
performSearch();
}
});
</script>
// Add event listener for keyword search the "Enter" key
document.getElementById("search-keywords").addEventListener("keydown", function(event) {
if (event.key === "Enter") {
event.preventDefault();
performSearch();
}
});
</script>
</body>
</html>

View file

@ -3,6 +3,8 @@
{% block title %}Chatroom{% endblock %}
{% block content %}
<a id="downloadButton" href="/download_chat_history?room_name={{ room_name }}" download="chat_history.json">Download Chat History</a>
<div id="chat-container">
<!-- Chat area where messages will be displayed -->
<div id="chat"></div>