Skip to content

Commit

Permalink
Code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AndresCdo committed Apr 9, 2023
1 parent 54cbf1c commit 011699e
Show file tree
Hide file tree
Showing 18 changed files with 28 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ scripts/auto_gpt_workspace/*
*.mpeg
.env
last_run_ai_settings.yaml
outputs/*
outputs/*
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM python:3.11

WORKDIR /app
COPY scripts/ /app
COPY requirements.txt /app/requirements.txt

RUN pip install -r requirements.txt

CMD ["python", "main.py"]
2 changes: 0 additions & 2 deletions scripts/ai_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def __init__(self, ai_name="", ai_role="", ai_goals=[]):
# Soon this will go in a folder where it remembers more stuff about the run(s)
SAVE_FILE = "last_run_ai_settings.yaml"


@classmethod
def load(cls, config_file=SAVE_FILE):
"""Load variables from yaml file if it exists, otherwise use defaults."""
Expand All @@ -29,7 +28,6 @@ def load(cls, config_file=SAVE_FILE):

return cls(ai_name, ai_role, ai_goals)


def save(self, config_file=SAVE_FILE):
"""Save variables to yaml file."""
config = {"ai_name": self.ai_name, "ai_role": self.ai_role, "ai_goals": self.ai_goals}
Expand Down
6 changes: 3 additions & 3 deletions scripts/ai_functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import List, Optional
from json import dumps
import json
from config import Config
from call_ai_function import call_ai_function
from json_parser import fix_and_parse_json
Expand All @@ -23,7 +23,7 @@ def improve_code(suggestions: List[str], code: str) -> str:
function_string = (
"def generate_improved_code(suggestions: List[str], code: str) -> str:"
)
args = [dumps(suggestions), code]
args = [json.dumps(suggestions), code]
description_string = """Improves the provided code based on the suggestions provided, making no other changes."""

result_string = call_ai_function(function_string, args, description_string)
Expand All @@ -36,7 +36,7 @@ def write_tests(code: str, focus: List[str]) -> str:
function_string = (
"def create_test_cases(code: str, focus: Optional[str] = None) -> str:"
)
args = [code, dumps(focus)]
args = [code, json.dumps(focus)]
description_string = """Generates test cases for the existing code, focusing on specific areas if required."""

result_string = call_ai_function(function_string, args, description_string)
Expand Down
12 changes: 3 additions & 9 deletions scripts/browse.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from requests import get
import requests
from bs4 import BeautifulSoup
from config import Config
from llm_utils import create_chat_completion

cfg = Config()


def scrape_text(url):
"""Scrape text from a webpage"""
response = get(url)
response = requests.get(url)

# Check if the response contains an HTTP error
if response.status_code >= 400:
Expand All @@ -30,26 +29,22 @@ def scrape_text(url):
def extract_hyperlinks(soup):
"""Extract hyperlinks from a BeautifulSoup object"""
hyperlinks = []

for link in soup.find_all('a', href=True):
hyperlinks.append((link.text, link['href']))

return hyperlinks


def format_hyperlinks(hyperlinks):
"""Format hyperlinks into a list of strings"""
formatted_links = []

for link_text, link_url in hyperlinks:
formatted_links.append(f"{link_text} ({link_url})")

return formatted_links


def scrape_links(url):
"""Scrape hyperlinks from a webpage"""
response = get(url)
response = requests.get(url)

# Check if the response contains an HTTP error
if response.status_code >= 400:
Expand All @@ -72,7 +67,6 @@ def split_text(text, max_length=8192):
current_chunk = []

for paragraph in paragraphs:

if current_length + len(paragraph) + 1 <= max_length:
current_chunk.append(paragraph)
current_length += len(paragraph) + 1
Expand Down
4 changes: 2 additions & 2 deletions scripts/call_ai_function.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from config import Config
from llm_utils import create_chat_completion
cfg = Config()

cfg = Config()

from llm_utils import create_chat_completion
# This is a magic function that can do anything with no-code. See
# https://github.com/Torantulino/AI-Functions for more info.
def call_ai_function(function, args, description, model=cfg.smart_llm_model):
Expand Down
14 changes: 4 additions & 10 deletions scripts/chat.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from time import sleep
import time
import openai
from dotenv import load_dotenv
from config import Config
import token_counter
from llm_utils import create_chat_completion
cfg = Config()

cfg = Config()

def create_chat_message(role, content):
"""
Expand Down Expand Up @@ -48,10 +48,8 @@ def chat_with_ai(
"""
model = cfg.fast_llm_model # TODO: Change model from hardcode to argument
# Reserve 1000 tokens for the response

if debug:
print(f"Token limit: {token_limit}")

print(f"Token limit: {token_limit}")
send_token_limit = token_limit - 1000

current_context = [
Expand All @@ -73,7 +71,6 @@ def chat_with_ai(
message_to_add = full_message_history[next_message_to_add_index]

tokens_to_add = token_counter.count_message_tokens([message_to_add], model)

if current_tokens_used + tokens_to_add > send_token_limit:
break

Expand All @@ -99,16 +96,13 @@ def chat_with_ai(
print(f"Send Token Count: {current_tokens_used}")
print(f"Tokens remaining for response: {tokens_remaining}")
print("------------ CONTEXT SENT TO AI ---------------")

for message in current_context:
# Skip printing the prompt

if message["role"] == "system" and message["content"] == prompt:
continue
print(
f"{message['role'].capitalize()}: {message['content']}")
print()

print("----------- END OF CONTEXT ----------------")

# TODO: use a model defined elsewhere, so that model can contain temperature and other settings we care about
Expand All @@ -130,4 +124,4 @@ def chat_with_ai(
except openai.error.RateLimitError:
# TODO: WHen we switch to langchain, this is built in
print("Error: ", "API Rate Limit Reached. Waiting 10 seconds...")
sleep(10)
time.sleep(10)
4 changes: 2 additions & 2 deletions scripts/commands.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import browse
import json
import memory as mem
from datetime import datetime
import datetime
import agent_manager as agents
import speak
from config import Config
Expand Down Expand Up @@ -110,7 +110,7 @@ def execute_command(command_name, arguments):
def get_datetime():
"""Return the current date and time"""
return "Current date and time: " + \
datetime.now().strftime("%Y-%m-%d %H:%M:%S")
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")


def google_search(query, num_results=8):
Expand Down
12 changes: 1 addition & 11 deletions scripts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,52 +44,42 @@ def __init__(self):
# Initialize the OpenAI API client
openai.api_key = self.openai_api_key


def set_continuous_mode(self, value: bool):
"""Set the continuous mode value."""
self.continuous_mode = value


def set_speak_mode(self, value: bool):
"""Set the speak mode value."""
self.speak_mode = value


def set_fast_llm_model(self, value: str):
"""Set the fast LLM model value."""
self.fast_llm_model = value


def set_smart_llm_model(self, value: str):
"""Set the smart LLM model value."""
self.smart_llm_model = value


def set_fast_token_limit(self, value: int):
"""Set the fast token limit value."""
self.fast_token_limit = value


def set_smart_token_limit(self, value: int):
"""Set the smart token limit value."""
self.smart_token_limit = value


def set_openai_api_key(self, value: str):
"""Set the OpenAI API key value."""
self.openai_api_key = value


def set_elevenlabs_api_key(self, value: str):
"""Set the ElevenLabs API key value."""
self.elevenlabs_api_key = value


def set_google_api_key(self, value: str):
"""Set the Google API key value."""
self.google_api_key = value


def set_custom_search_engine_id(self, value: str):
"""Set the custom search engine ID value."""
self.custom_search_engine_id = value
self.custom_search_engine_id = value
4 changes: 2 additions & 2 deletions scripts/data.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from os import path
import os
from pathlib import Path
SRC_DIR = Path(__file__).parent

def load_prompt():
"""Load the prompt from data/prompt.txt"""
try:
# get directory of this file:
file_dir = Path(path.dirname(path.realpath(__file__)))
file_dir = Path(os.path.dirname(os.path.realpath(__file__)))
data_dir = file_dir / "data"
prompt_file = data_dir / "prompt.txt"
# Load the promt from data/prompt.txt
Expand Down
6 changes: 3 additions & 3 deletions scripts/execute_code.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import docker
from os import path
import os


def execute_python_file(file):
Expand All @@ -11,9 +11,9 @@ def execute_python_file(file):
if not file.endswith(".py"):
return "Error: Invalid file type. Only .py files are allowed."

file_path = path.join(workspace_folder, file)
file_path = os.path.join(workspace_folder, file)

if not path.isfile(file_path):
if not os.path.isfile(file_path):
return f"Error: File '{file}' does not exist."

try:
Expand Down
2 changes: 0 additions & 2 deletions scripts/file_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ def write_to_file(filename, text):
try:
filepath = safe_join(working_directory, filename)
directory = os.path.dirname(filepath)

if not os.path.exists(directory):
os.makedirs(directory)

with open(filepath, "w") as f:
f.write(text)
return "File written to successfully."
Expand Down
6 changes: 0 additions & 6 deletions scripts/json_parser.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
from call_ai_function import call_ai_function
from config import Config

cfg = Config()

def fix_and_parse_json(json_str: str, try_to_fix_with_gpt: bool = True):
Expand Down Expand Up @@ -38,18 +37,15 @@ def fix_and_parse_json(json_str: str, try_to_fix_with_gpt: bool = True):
json_str = json_str[:last_brace_index+1]
return json.loads(json_str)
except Exception as e:

if try_to_fix_with_gpt:
print(f"Warning: Failed to parse AI output, attempting to fix.\n If you see this warning frequently, it's likely that your prompt is confusing the AI. Try changing it up slightly.")
# Now try to fix this up using the ai_functions
ai_fixed_json = fix_json(json_str, json_schema, False)

if ai_fixed_json != "failed":
return json.loads(ai_fixed_json)
else:
print(f"Failed to fix ai output, telling the AI.") # This allows the AI to react to the error message, which usually results in it correcting its ways.
return json_str

else:
raise e

Expand All @@ -63,11 +59,9 @@ def fix_json(json_str: str, schema: str, debug=False) -> str:
# If it doesn't already start with a "`", add one:
if not json_str.startswith("`"):
json_str = "```json\n" + json_str + "\n```"

result_string = call_ai_function(
function_string, args, description_string, model=cfg.fast_llm_model
)

if debug:
print("------------ JSON FIX ATTEMPT ---------------")
print(f"Original JSON: {json_str}")
Expand Down
1 change: 0 additions & 1 deletion scripts/llm_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import openai
from config import Config

cfg = Config()

openai.api_key = cfg.openai_api_key
Expand Down
Loading

0 comments on commit 011699e

Please sign in to comment.