mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-23 00:44:22 +01:00
The unlooping and fixing of file execution. (#3368)
* The unlooping and fixing of file execution. * lint * Use static random seed during testing. remove unused import. * Fix bug * Actually fix bug. * lint * Unloop a bit more an fix json. * Fix another bug. * lint. --------- Co-authored-by: merwanehamadi <merwanehamadi@gmail.com>
This commit is contained in:
@@ -1,5 +1,13 @@
|
|||||||
|
import os
|
||||||
|
import random
|
||||||
|
import sys
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
if "pytest" in sys.argv or "pytest" in sys.modules or os.getenv("CI"):
|
||||||
|
print("Setting random seed to 42")
|
||||||
|
random.seed(42)
|
||||||
|
|
||||||
# Load the users .env file into environment variables
|
# Load the users .env file into environment variables
|
||||||
load_dotenv(verbose=True, override=True)
|
load_dotenv(verbose=True, override=True)
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import time
|
import time
|
||||||
|
from random import shuffle
|
||||||
|
|
||||||
from openai.error import RateLimitError
|
from openai.error import RateLimitError
|
||||||
|
|
||||||
@@ -80,12 +81,17 @@ def chat_with_ai(
|
|||||||
|
|
||||||
logger.debug(f"Token limit: {token_limit}")
|
logger.debug(f"Token limit: {token_limit}")
|
||||||
send_token_limit = token_limit - 1000
|
send_token_limit = token_limit - 1000
|
||||||
|
if len(full_message_history) == 0:
|
||||||
relevant_memory = (
|
relevant_memory = ""
|
||||||
""
|
else:
|
||||||
if len(full_message_history) == 0
|
recent_history = full_message_history[-5:]
|
||||||
else permanent_memory.get_relevant(str(full_message_history[-9:]), 10)
|
shuffle(recent_history)
|
||||||
|
relevant_memories = permanent_memory.get_relevant(
|
||||||
|
str(recent_history), 5
|
||||||
)
|
)
|
||||||
|
if relevant_memories:
|
||||||
|
shuffle(relevant_memories)
|
||||||
|
relevant_memory = str(relevant_memories)
|
||||||
|
|
||||||
logger.debug(f"Memory Stats: {permanent_memory.get_stats()}")
|
logger.debug(f"Memory Stats: {permanent_memory.get_stats()}")
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
"""Execute code in a Docker container"""
|
"""Execute code in a Docker container"""
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import docker
|
import docker
|
||||||
from docker.errors import ImageNotFound
|
from docker.errors import ImageNotFound
|
||||||
@@ -40,7 +41,6 @@ def execute_python_file(filename: str) -> str:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
client = docker.from_env()
|
client = docker.from_env()
|
||||||
|
|
||||||
# You can replace this with the desired Python image/version
|
# You can replace this with the desired Python image/version
|
||||||
# You can find available Python images on Docker Hub:
|
# You can find available Python images on Docker Hub:
|
||||||
# https://hub.docker.com/_/python
|
# https://hub.docker.com/_/python
|
||||||
@@ -60,10 +60,9 @@ def execute_python_file(filename: str) -> str:
|
|||||||
print(f"{status}: {progress}")
|
print(f"{status}: {progress}")
|
||||||
elif status:
|
elif status:
|
||||||
print(status)
|
print(status)
|
||||||
|
|
||||||
container = client.containers.run(
|
container = client.containers.run(
|
||||||
image_name,
|
image_name,
|
||||||
f"python {filename}",
|
f"python {Path(filename).relative_to(CFG.workspace_path)}",
|
||||||
volumes={
|
volumes={
|
||||||
CFG.workspace_path: {
|
CFG.workspace_path: {
|
||||||
"bind": "/workspace",
|
"bind": "/workspace",
|
||||||
|
|||||||
@@ -91,14 +91,33 @@ def fix_json_using_multiple_techniques(assistant_reply: str) -> Dict[Any, Any]:
|
|||||||
Returns:
|
Returns:
|
||||||
str: The fixed JSON string.
|
str: The fixed JSON string.
|
||||||
"""
|
"""
|
||||||
|
assistant_reply = assistant_reply.strip()
|
||||||
|
if assistant_reply.startswith("```json"):
|
||||||
|
assistant_reply = assistant_reply[7:]
|
||||||
|
if assistant_reply.endswith("```"):
|
||||||
|
assistant_reply = assistant_reply[:-3]
|
||||||
|
try:
|
||||||
|
return json.loads(assistant_reply) # just check the validity
|
||||||
|
except json.JSONDecodeError: # noqa: E722
|
||||||
|
pass
|
||||||
|
|
||||||
|
if assistant_reply.startswith("json "):
|
||||||
|
assistant_reply = assistant_reply[5:]
|
||||||
|
assistant_reply = assistant_reply.strip()
|
||||||
|
try:
|
||||||
|
return json.loads(assistant_reply) # just check the validity
|
||||||
|
except json.JSONDecodeError: # noqa: E722
|
||||||
|
pass
|
||||||
|
|
||||||
# Parse and print Assistant response
|
# Parse and print Assistant response
|
||||||
assistant_reply_json = fix_and_parse_json(assistant_reply)
|
assistant_reply_json = fix_and_parse_json(assistant_reply)
|
||||||
|
logger.debug("Assistant reply JSON: %s", str(assistant_reply_json))
|
||||||
if assistant_reply_json == {}:
|
if assistant_reply_json == {}:
|
||||||
assistant_reply_json = attempt_to_fix_json_by_finding_outermost_brackets(
|
assistant_reply_json = attempt_to_fix_json_by_finding_outermost_brackets(
|
||||||
assistant_reply
|
assistant_reply
|
||||||
)
|
)
|
||||||
|
|
||||||
|
logger.debug("Assistant reply JSON 2: %s", str(assistant_reply_json))
|
||||||
if assistant_reply_json != {}:
|
if assistant_reply_json != {}:
|
||||||
return assistant_reply_json
|
return assistant_reply_json
|
||||||
|
|
||||||
|
|||||||
@@ -71,6 +71,9 @@ def build_default_prompt_generator() -> PromptGenerator:
|
|||||||
"Every command has a cost, so be smart and efficient. Aim to complete tasks in"
|
"Every command has a cost, so be smart and efficient. Aim to complete tasks in"
|
||||||
" the least number of steps."
|
" the least number of steps."
|
||||||
)
|
)
|
||||||
|
prompt_generator.add_performance_evaluation(
|
||||||
|
"If you cannot think of a valid command to perform start or message an agent to determine the next command."
|
||||||
|
)
|
||||||
prompt_generator.add_performance_evaluation("Write all code to a file.")
|
prompt_generator.add_performance_evaluation("Write all code to a file.")
|
||||||
return prompt_generator
|
return prompt_generator
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from autogpt.logs import logger
|
||||||
|
|
||||||
|
|
||||||
class Workspace:
|
class Workspace:
|
||||||
"""A class that represents a workspace for an AutoGPT agent."""
|
"""A class that represents a workspace for an AutoGPT agent."""
|
||||||
@@ -112,8 +114,12 @@ class Workspace:
|
|||||||
if root is None:
|
if root is None:
|
||||||
return Path(relative_path).resolve()
|
return Path(relative_path).resolve()
|
||||||
|
|
||||||
|
logger.debug(f"Resolving path '{relative_path}' in workspace '{root}'")
|
||||||
|
|
||||||
root, relative_path = Path(root).resolve(), Path(relative_path)
|
root, relative_path = Path(root).resolve(), Path(relative_path)
|
||||||
|
|
||||||
|
logger.debug(f"Resolved root as '{root}'")
|
||||||
|
|
||||||
if relative_path.is_absolute():
|
if relative_path.is_absolute():
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Attempted to access absolute path '{relative_path}' in workspace '{root}'."
|
f"Attempted to access absolute path '{relative_path}' in workspace '{root}'."
|
||||||
@@ -121,6 +127,8 @@ class Workspace:
|
|||||||
|
|
||||||
full_path = root.joinpath(relative_path).resolve()
|
full_path = root.joinpath(relative_path).resolve()
|
||||||
|
|
||||||
|
logger.debug(f"Joined paths as '{full_path}'")
|
||||||
|
|
||||||
if restrict_to_root and not full_path.is_relative_to(root):
|
if restrict_to_root and not full_path.is_relative_to(root):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Attempted to access path '{full_path}' outside of workspace '{root}'."
|
f"Attempted to access path '{full_path}' outside of workspace '{root}'."
|
||||||
|
|||||||
Reference in New Issue
Block a user