refac: code intepreter

This commit is contained in:
Timothy Jaeryang Baek
2025-02-10 13:12:05 -08:00
parent 610f9d039a
commit a273cba0fb
2 changed files with 50 additions and 23 deletions

View File

@@ -1723,6 +1723,38 @@ async def process_chat_response(
)
output["stdout"] = "\n".join(stdoutLines)
result = output.get("result", "")
if result:
resultLines = result.split("\n")
for idx, line in enumerate(resultLines):
if "data:image/png;base64" in line:
id = str(uuid4())
# ensure the path exists
os.makedirs(
os.path.join(CACHE_DIR, "images"),
exist_ok=True,
)
image_path = os.path.join(
CACHE_DIR,
f"images/{id}.png",
)
with open(image_path, "wb") as f:
f.write(
base64.b64decode(
line.split(",")[1]
)
)
resultLines[idx] = (
f"![Output Image {idx}](/cache/images/{id}.png)"
)
output["result"] = "\n".join(resultLines)
except Exception as e:
output = str(e)