From 5a7d033f9aa6be1bd60c05688b9c1f0b73ae7d60 Mon Sep 17 00:00:00 2001 From: adldotori Date: Fri, 7 Apr 2023 19:38:56 +0900 Subject: [PATCH] feat: change file path --- api/main.py | 5 +---- core/handlers/base.py | 5 +---- core/prompts/input.py | 4 ++-- core/tools/editor/patch.py | 4 ---- core/tools/editor/read.py | 5 +---- core/tools/editor/verify.py | 8 ++------ core/tools/editor/write.py | 5 ----- 7 files changed, 7 insertions(+), 29 deletions(-) diff --git a/api/main.py b/api/main.py index 5c802a4..717ad67 100644 --- a/api/main.py +++ b/api/main.py @@ -7,18 +7,15 @@ from fastapi import FastAPI from fastapi.staticfiles import StaticFiles from pydantic import BaseModel -from ansi import ANSI, Color, Style, dim_multiline from core.agents.manager import AgentManager from core.handlers.base import BaseHandler, FileHandler, FileType from core.handlers.dataframe import CsvToDataframe -from core.prompts.error import ERROR_PROMPT from core.tools.base import BaseToolSet from core.tools.cpu import ExitConversation, RequestsGet from core.tools.editor import CodeEditor from core.tools.terminal import Terminal from core.upload import StaticUploader from env import settings -from logger import logger app = FastAPI() @@ -92,7 +89,7 @@ async def command(request: Request) -> Response: except Exception as e: return {"response": str(e), "files": []} - files = re.findall("image/\S*png|dataframe/\S*csv", res["output"]) + files = re.findall("[file/\S*]", res["output"]) return { "response": res["output"], diff --git a/core/handlers/base.py b/core/handlers/base.py index 6fbd115..a1020f7 100644 --- a/core/handlers/base.py +++ b/core/handlers/base.py @@ -1,13 +1,10 @@ import os import uuid from enum import Enum -from pathlib import Path from typing import Dict import requests -from env import settings - class FileType(Enum): IMAGE = "image" @@ -65,7 +62,7 @@ class FileHandler: filetype = FileType.from_url(url) data = requests.get(url).content local_filename = os.path.join( - filetype.value, str(uuid.uuid4())[0:8] + filetype.to_extension() + "file", str(uuid.uuid4())[0:8] + filetype.to_extension() ) os.makedirs(os.path.dirname(local_filename), exist_ok=True) with open(local_filename, "wb") as f: diff --git a/core/prompts/input.py b/core/prompts/input.py index 136cff9..22dc030 100644 --- a/core/prompts/input.py +++ b/core/prompts/input.py @@ -3,8 +3,7 @@ EVAL_PREFIX = """{bot_name} can execute any user's request. {bot_name} has permission to handle one instance and can handle the environment in it at will. You can code, run, debug, and test yourself. You can correct the code appropriately by looking at the error message. -I can understand, process, and create various types of files. Images must be stored in the ./image/, audio in the ./audio/, video in the ./video/, and dataframes must be stored in the ./dataframe/. - +I can understand, process, and create various types of files. Every files except the code must be restored in file/ directory. {bot_name} can do whatever it takes to execute the user's request. Let's think step by step. """ @@ -38,6 +37,7 @@ EVAL_SUFFIX = """TOOLS {bot_name} can ask the user to use tools to look up information that may be helpful in answering the users original question. You are very strict to the filename correctness and will never fake a file name if it does not exist. You will remember to provide the file name loyally if it's provided in the last tool observation. +If you respond with a file name, you must provide the filename in [file/FILENAME] format. The tools the human can use are: diff --git a/core/tools/editor/patch.py b/core/tools/editor/patch.py index 32acb67..c1b732e 100644 --- a/core/tools/editor/patch.py +++ b/core/tools/editor/patch.py @@ -58,11 +58,8 @@ test.py|11,16|11,16|_titles import os import re -from pathlib import Path from typing import Tuple -from env import settings - from .verify import verify @@ -103,7 +100,6 @@ class PatchCommand: @verify def execute(self) -> Tuple[int, int]: - os.makedirs(os.path.dirname(self.filepath), exist_ok=True) lines = self.read_lines() before = sum([len(line) for line in lines]) diff --git a/core/tools/editor/read.py b/core/tools/editor/read.py index 8d3a1c9..66d848c 100644 --- a/core/tools/editor/read.py +++ b/core/tools/editor/read.py @@ -3,10 +3,7 @@ read protocol: |- """ -from pathlib import Path -from typing import List, Optional, Tuple - -from env import settings +from typing import List, Optional from .verify import verify diff --git a/core/tools/editor/verify.py b/core/tools/editor/verify.py index f93a1cb..f3d651c 100644 --- a/core/tools/editor/verify.py +++ b/core/tools/editor/verify.py @@ -1,17 +1,13 @@ from pathlib import Path -from env import settings - def verify(func): def wrapper(*args, **kwargs): try: filepath = args[0].filepath - except: + except AttributeError: raise Exception("This tool doesn't have filepath. Please check your code.") - if not str(Path(filepath).resolve()).startswith( - str(Path(settings["PLAYGROUND_DIR"]).resolve()) - ): + if not str(Path(filepath).resolve()).startswith(str(Path().resolve())): return "You can't access file outside of playground." return func(*args, **kwargs) diff --git a/core/tools/editor/write.py b/core/tools/editor/write.py index 523ad91..54337b3 100644 --- a/core/tools/editor/write.py +++ b/core/tools/editor/write.py @@ -4,11 +4,6 @@ write protocol: """ -import os -from pathlib import Path - -from env import settings - from .verify import verify