From 5d8e8c7fcc642bde1e016d71b7e2533d9e00c121 Mon Sep 17 00:00:00 2001 From: Saifeddine ALOUI Date: Sun, 20 Aug 2023 00:12:08 +0200 Subject: [PATCH] Added code execution --- api/__init__.py | 24 ++++- app.py | 33 ++++++- presets/presets.json | 20 ++-- tests/end_point_tests/endpoints.http | 9 ++ .../{index-e5fd08e6.js => index-31e94240.js} | 99 ++++++++++++------- web/dist/index.html | 2 +- web/src/components/MarkdownRenderer.vue | 58 +++++++++++ web/src/views/PlayGroundView.vue | 53 ++++++---- 8 files changed, 235 insertions(+), 63 deletions(-) rename web/dist/assets/{index-e5fd08e6.js => index-31e94240.js} (87%) diff --git a/api/__init__.py b/api/__init__.py index 662c943e..f604beb4 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -517,6 +517,28 @@ class LoLLMsAPPI(LollmsApplication): self.socketio.sleep(0) self.busy = False + @self.socketio.on('execute_python_code') + def execute_python_code(data): + """Executes Python code and returns the output.""" + client_id = request.sid + code = data["code"] + # Import the necessary modules. + import io + import sys + import time + + # Create a Python interpreter. + interpreter = io.StringIO() + sys.stdout = interpreter + + # Execute the code. + start_time = time.time() + exec(code) + end_time = time.time() + + # Get the output. + output = interpreter.getvalue() + self.socketio.emit("execution_output", {"output":output,"execution_time":end_time - start_time}, room=client_id) # A copy of the original lollms-server generation code needed for playground @self.socketio.on('generate_text') @@ -935,7 +957,7 @@ class LoLLMsAPPI(LollmsApplication): if self.config["debug"]: ASCIIColors.yellow(discussion_messages) - ASCIIColors.info(f"prompt size:{tokens.shape} tokens") + ASCIIColors.info(f"prompt size:{len(tokens)} tokens") return discussion_messages, message.content, tokens diff --git a/app.py b/app.py index 48218f1e..7f282229 100644 --- a/app.py +++ b/app.py @@ -285,6 +285,7 @@ class LoLLMsWebUI(LoLLMsAPPI): self.add_endpoint("/", "", self.index, methods=["GET"]) self.add_endpoint("/settings/", "", self.index, methods=["GET"]) + self.add_endpoint("/playground/", "", self.index, methods=["GET"]) self.add_endpoint("/", "serve_static", self.serve_static, methods=["GET"]) self.add_endpoint("/user_infos/", "serve_user_infos", self.serve_user_infos, methods=["GET"]) @@ -415,7 +416,37 @@ class LoLLMsWebUI(LoLLMsAPPI): self.add_endpoint( "/save_presets", "save_presets", self.save_presets, methods=["POST"] - ) + ) + + self.add_endpoint( + "/execute_python_code", "execute_python_code", self.execute_python_code, methods=["POST"] + ) + + + def execute_python_code(self): + """Executes Python code and returns the output.""" + data = request.get_json() + code = data["code"] + # Import the necessary modules. + import io + import sys + import time + + # Create a Python interpreter. + interpreter = io.StringIO() + sys.stdout = interpreter + + # Execute the code. + start_time = time.time() + try: + exec(code) + # Get the output. + output = interpreter.getvalue() + except Exception as ex: + output = str(ex) + end_time = time.time() + + return jsonify({"output":output,"execution_time":end_time - start_time}) def get_presets(self): presets_file = self.lollms_paths.personal_databases_path/"presets.json" diff --git a/presets/presets.json b/presets/presets.json index 4a56307c..84c8e581 100644 --- a/presets/presets.json +++ b/presets/presets.json @@ -1,9 +1,15 @@ { - "Writing a Book in Latex": "\n```latex\n\\documentclass[12pt]{book}\n\\usepackage{url}\n\\begin{document}\n\\title{}\n\\author{} % Author\n\\date{\\today} % Date\n\\maketitle\n\\tableofcontents\n\\chapter{Introduction}\n\n", - "Simple Book writing":"Once apon a time", - "Simple Question Answer":"User:\nAssistant:", - "Question Answer with conditionning":"Assistant is a highly developed AI capable of answering any question about any subject.\nUser:\nAssistant:", - "Instruct mode": "Instructions:\n\nAnswer:", - "Make a python function": "Here is a python function that :\n```python\ndef", - "Make a c++ function": "Here is a python function that :\n```c++\n/*" + "Build a Latex Book": "@@\n```latex\n\\documentclass[12pt]{book}\n\\usepackage{url}\n\\begin{document}\n\\title{@@}\n\\author{@@} % Author\n\\date{\\today} % Date\n\\maketitle\n\\tableofcontents\n\\chapter{Introduction}\n@@\n\\end{document}\n```", "Simple Book writing":"Once apon a time", + "Simple Question Answer":"User:@@\nAssistant:", + "Question Answer with conditionning":"Assistant is a highly developed AI capable of answering any question about any subject.\nUser:@\nAssistant:", + "Instruct mode": "Instructions:\n@@\nAnswer:", + "Make a python function": "Here is a python function that @@:\n```python\ndef", + "Make a c++ function": "Here is a c++ function that @@:\n```c++\n/*", + "Make a c# function": "Here is a c# function that @@:\n```c#\n/*", + "Make a objective-c function": "Here is a objective-c function that @@:\n```objective-c\n/*", + "Make a java function": "Here is a java function that @@:\n```java\n/*", + "Make a visual basic.net function": "Here is a visual basic function that @:\n```vb.net\n/*", + "Make a vue.js ui": "Here is a vue.js template that @@:\n```vue.js\n