From 82fc80b3fcbf8cea3760af68a24fca67f5bb077d Mon Sep 17 00:00:00 2001 From: Abi Raja Date: Tue, 14 Nov 2023 15:00:23 -0500 Subject: [PATCH] aider: Changed file writing operation to write into a new file in the `run_logs` directory for each run using the current timestamp as the filename. # Aider chat conversation: ASSISTANT: To achieve this, we need to modify the `backend/main.py` file. We will change the file writing operation to write into a new file in the `run_logs` directory for each run. We will use the current timestamp to create a unique filename for each run. Here is the step-by-step plan: 1. Import the `datetime` and `os` modules. 2. Create a `run_logs` directory if it doesn't exist. 3. Generate a unique filename using the current timestamp. 4. Change the file writing operation to write into the new file in the `run_logs` directory. Here is the *edit block* for these changes: ```python backend/main.py <<<<<<< HEAD # Load environment variables first from dotenv import load_dotenv from prompts import assemble_prompt load_dotenv() from fastapi import FastAPI, WebSocket from llm import stream_openai_response app = FastAPI() @app.websocket("/generate-code") async def stream_code_test(websocket: WebSocket): await websocket.accept() result = await websocket.receive_json() async def process_chunk(content): await websocket.send_json({"type": "chunk", "value": content}) messages = assemble_prompt("") print(messages) # Write the messages dict into a file for debugging with open("messages.json", "w") as f: f.write(str(messages)) await stream_openai_response( messages, lambda x: process_chunk(x), ) ======= # Load environment variables first from dotenv import load_dotenv import os from datetime import datetime from prompts import assemble_prompt load_dotenv() from fastapi import FastAPI, WebSocket from llm import stream_openai_response app = FastAPI() @app.websocket("/generate-code") async def stream_code_test(websocket: WebSocket): await websocket.accept() result = await websocket.receive_json() async def process_chunk(content): await websocket.send_json({"type": "chunk", "value": content}) messages = assemble_prompt("") print(messages) # Create run_logs directory if it doesn't exist if not os.path.exists('run_logs'): os.makedirs('run_logs') # Generate a unique filename using the current timestamp filename = datetime.now().strftime('run_logs/messages_%Y%m%d_%H%M%S.json') # Write the messages dict into a new file for each run with open(filename, "w") as f: f.write(str(messages)) await stream_openai_response( messages, lambda x: process_chunk(x), ) >>>>>>> updated ``` --- backend/main.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/backend/main.py b/backend/main.py index 22f7b3c..4971d00 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,5 +1,7 @@ # Load environment variables first from dotenv import load_dotenv +import os +from datetime import datetime from prompts import assemble_prompt @@ -24,8 +26,15 @@ async def stream_code_test(websocket: WebSocket): messages = assemble_prompt("") print(messages) - # Write the messages dict into a file for debugging - with open("messages.json", "w") as f: + # Create run_logs directory if it doesn't exist + if not os.path.exists('run_logs'): + os.makedirs('run_logs') + + # Generate a unique filename using the current timestamp + filename = datetime.now().strftime('run_logs/messages_%Y%m%d_%H%M%S.json') + + # Write the messages dict into a new file for each run + with open(filename, "w") as f: f.write(str(messages)) await stream_openai_response(