feat: change file path

pull/24/head
adldotori 1 year ago
parent e5165d6f90
commit 5a7d033f9a

@ -7,18 +7,15 @@ from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel from pydantic import BaseModel
from ansi import ANSI, Color, Style, dim_multiline
from core.agents.manager import AgentManager from core.agents.manager import AgentManager
from core.handlers.base import BaseHandler, FileHandler, FileType from core.handlers.base import BaseHandler, FileHandler, FileType
from core.handlers.dataframe import CsvToDataframe from core.handlers.dataframe import CsvToDataframe
from core.prompts.error import ERROR_PROMPT
from core.tools.base import BaseToolSet from core.tools.base import BaseToolSet
from core.tools.cpu import ExitConversation, RequestsGet from core.tools.cpu import ExitConversation, RequestsGet
from core.tools.editor import CodeEditor from core.tools.editor import CodeEditor
from core.tools.terminal import Terminal from core.tools.terminal import Terminal
from core.upload import StaticUploader from core.upload import StaticUploader
from env import settings from env import settings
from logger import logger
app = FastAPI() app = FastAPI()
@ -92,7 +89,7 @@ async def command(request: Request) -> Response:
except Exception as e: except Exception as e:
return {"response": str(e), "files": []} return {"response": str(e), "files": []}
files = re.findall("image/\S*png|dataframe/\S*csv", res["output"]) files = re.findall("[file/\S*]", res["output"])
return { return {
"response": res["output"], "response": res["output"],

@ -1,13 +1,10 @@
import os import os
import uuid import uuid
from enum import Enum from enum import Enum
from pathlib import Path
from typing import Dict from typing import Dict
import requests import requests
from env import settings
class FileType(Enum): class FileType(Enum):
IMAGE = "image" IMAGE = "image"
@ -65,7 +62,7 @@ class FileHandler:
filetype = FileType.from_url(url) filetype = FileType.from_url(url)
data = requests.get(url).content data = requests.get(url).content
local_filename = os.path.join( 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) os.makedirs(os.path.dirname(local_filename), exist_ok=True)
with open(local_filename, "wb") as f: with open(local_filename, "wb") as f:

@ -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. {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. 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. {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. {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 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. 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: The tools the human can use are:

@ -58,11 +58,8 @@ test.py|11,16|11,16|_titles
import os import os
import re import re
from pathlib import Path
from typing import Tuple from typing import Tuple
from env import settings
from .verify import verify from .verify import verify
@ -103,7 +100,6 @@ class PatchCommand:
@verify @verify
def execute(self) -> Tuple[int, int]: def execute(self) -> Tuple[int, int]:
os.makedirs(os.path.dirname(self.filepath), exist_ok=True)
lines = self.read_lines() lines = self.read_lines()
before = sum([len(line) for line in lines]) before = sum([len(line) for line in lines])

@ -3,10 +3,7 @@ read protocol:
<filepath>|<start line>-<end line> <filepath>|<start line>-<end line>
""" """
from pathlib import Path from typing import List, Optional
from typing import List, Optional, Tuple
from env import settings
from .verify import verify from .verify import verify

@ -1,17 +1,13 @@
from pathlib import Path from pathlib import Path
from env import settings
def verify(func): def verify(func):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
try: try:
filepath = args[0].filepath filepath = args[0].filepath
except: except AttributeError:
raise Exception("This tool doesn't have filepath. Please check your code.") raise Exception("This tool doesn't have filepath. Please check your code.")
if not str(Path(filepath).resolve()).startswith( if not str(Path(filepath).resolve()).startswith(str(Path().resolve())):
str(Path(settings["PLAYGROUND_DIR"]).resolve())
):
return "You can't access file outside of playground." return "You can't access file outside of playground."
return func(*args, **kwargs) return func(*args, **kwargs)

@ -4,11 +4,6 @@ write protocol:
<filepath> <filepath>
<content> <content>
""" """
import os
from pathlib import Path
from env import settings
from .verify import verify from .verify import verify

Loading…
Cancel
Save