python execution of most recent code block /python

modified:   app.py
This commit is contained in:
Russell Ballestrini 2023-12-07 12:14:26 -05:00
parent 65e904b286
commit 6f99508898
3 changed files with 59 additions and 4 deletions

View file

@ -100,7 +100,7 @@ The system will process your message and provide a response from the selected la
Commands
--------
The application supports special commands for interacting with the chatroom:
The application supports special commands for interacting with the chatroom:
- ``/s3 load <file_path>``: Loads a file from S3 and displays its content in the chatroom.
- ``/s3 save <file_path>``: Saves the most recent code block from the chatroom to S3.
@ -109,13 +109,17 @@ The application supports special commands for interacting with the chatroom:
- ``/cancel``: Cancel the most recent chat completion from streaming into the chatroom.
- ``/python``: Executes the most recent Python code block sent in the chatroom and returns the output or any errors.
---
The ``/s3 ls`` command can be used to list files in the connected S3 bucket. You can specify a pattern to filter the files listed. For example:
- ``/s3 ls *`` will list all files in the bucket.
- ``/s3 ls *.py`` will list all Python files.
- ``/s3 ls README.*`` will list files starting with "README." and any extension.
The command will return the file name, size in bytes, and the last modified timestamp for each file that matches the pattern.
- ``/python``: Executes the most recent Python code block sent in the chatroom and returns the output or any errors. This command allows users to run Python code snippets in real-time and is useful for quick debugging, learning, or collaborative coding sessions. Use this command with caution, as executing arbitrary code can pose security risks.
Please note that the ``/python`` command should be used responsibly and with consideration of the security implications of executing arbitrary code. It is recommended to implement additional security measures if this command is to be used in a production environment.
Structure
---------

52
app.py
View file

@ -228,6 +228,8 @@ def handle_message(data):
if command.startswith("/cancel"):
# Cancel the most recent generation request
eventlet.spawn(cancel_generation, room_name, data["username"])
if command.startswith("/python"):
eventlet.spawn(handle_python_command, room.name, room.id, data["username"])
if "dall-e-3" in data["message"]:
# Use the entire message as the prompt for DALL-E 3
@ -1042,6 +1044,56 @@ def cancel_generation(room_name, username):
)
def handle_python_command(room_name, room_id, username):
# Find the most recent code block
with app.app_context():
code_block_content = find_most_recent_code_block(room_name)
if code_block_content:
# Execute the code block content in a background task
output = execute_python_code(code_block_content)
output = f"```\n{output}\n```"
# Create a new message with the output
new_message = Message(
username="Python Interpreter", content=output, room_id=room_id
)
# Save the new message to the database
db.session.add(new_message)
db.session.commit()
# Send the output back to the chatroom with the new message ID
socketio.emit(
"message",
{
"id": new_message.id,
"username": "Python Interpreter",
"content": output,
},
room=room_name,
)
def execute_python_code(code):
import sys
import io
from contextlib import redirect_stdout
# Print the code block for debugging purposes
print("Executing the following code block:")
print(code)
print("-" * 50) # Separator for clarity
# Redirect stdout to capture the output
output = io.StringIO()
with redirect_stdout(output):
try:
# Execute the code
exec(code, {})
except Exception as e:
# Capture any errors
return f"Error executing code: {e}"
# Return the captured output
return output.getvalue()
if __name__ == "__main__":
import argparse

View file

@ -32,8 +32,7 @@ def upgrade():
for message in messages:
message.count_tokens()
session.add(message)
session.commit()
session.commit()
def downgrade():