forked from Significant-Gravitas/AutoGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I made the json parsing more forgivable. I improved the prompt, using things I learned from: Koobah/Auto-GPT
- Loading branch information
Showing
5 changed files
with
240 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import yaml | ||
import data | ||
|
||
class AIConfig: | ||
def __init__(self, ai_name="", ai_role="", ai_goals=[]): | ||
self.ai_name = ai_name | ||
self.ai_role = ai_role | ||
self.ai_goals = ai_goals | ||
|
||
# @classmethod | ||
# def create_from_user_prompts(cls): | ||
# ai_name = input("Name your AI: ") or "Entrepreneur-GPT" | ||
# ai_role = input(f"{ai_name} is: ") or "an AI designed to autonomously develop and run businesses with the sole goal of increasing your net worth." | ||
# print("Enter up to 5 goals for your AI: ") | ||
# print("For example: \nIncrease net worth, Grow Twitter Account, Develop and manage multiple businesses autonomously'") | ||
# print("Enter nothing to load defaults, enter nothing when finished.") | ||
# ai_goals = [] | ||
# for i in range(5): | ||
# ai_goal = input(f"Goal {i+1}: ") | ||
# if ai_goal == "": | ||
# break | ||
# ai_goals.append(ai_goal) | ||
# if len(ai_goals) == 0: | ||
# ai_goals = ["Increase net worth", "Grow Twitter Account", "Develop and manage multiple businesses autonomously"] | ||
# return cls(ai_name, ai_role, ai_goals) | ||
|
||
@classmethod | ||
def load(cls, config_file="config.yaml"): | ||
# Load variables from yaml file if it exists | ||
try: | ||
with open(config_file) as file: | ||
config_params = yaml.load(file, Loader=yaml.FullLoader) | ||
except FileNotFoundError: | ||
config_params = {} | ||
|
||
ai_name = config_params.get("ai_name", "") | ||
ai_role = config_params.get("ai_role", "") | ||
ai_goals = config_params.get("ai_goals", []) | ||
|
||
return cls(ai_name, ai_role, ai_goals) | ||
|
||
def save(self, config_file="config.yaml"): | ||
config = {"ai_name": self.ai_name, "ai_role": self.ai_role, "ai_goals": self.ai_goals} | ||
with open(config_file, "w") as file: | ||
documents = yaml.dump(config, file) | ||
|
||
def construct_full_prompt(self): | ||
prompt_start = """Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications.""" | ||
|
||
# Construct full prompt | ||
full_prompt = f"You are {self.ai_name}, {self.ai_role}\n{prompt_start}\n\nGOALS:\n\n" | ||
for i, goal in enumerate(self.ai_goals): | ||
full_prompt += f"{i+1}. {goal}\n" | ||
|
||
full_prompt += f"\n\n{data.load_prompt()}" | ||
return full_prompt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import unittest | ||
import os | ||
import sys | ||
# Probably a better way: | ||
sys.path.append(os.path.abspath('../scripts')) | ||
from json_parser import fix_and_parse_json | ||
|
||
class TestParseJson(unittest.TestCase): | ||
def test_valid_json(self): | ||
# Test that a valid JSON string is parsed correctly | ||
json_str = '{"name": "John", "age": 30, "city": "New York"}' | ||
obj = fix_and_parse_json(json_str) | ||
self.assertEqual(obj, {"name": "John", "age": 30, "city": "New York"}) | ||
|
||
def test_invalid_json_minor(self): | ||
# Test that an invalid JSON string can be fixed with gpt | ||
json_str = '{"name": "John", "age": 30, "city": "New York",}' | ||
self.assertEqual(fix_and_parse_json(json_str, try_to_fix_with_gpt=False), {"name": "John", "age": 30, "city": "New York"}) | ||
|
||
def test_invalid_json_major_with_gpt(self): | ||
# Test that an invalid JSON string raises an error when try_to_fix_with_gpt is False | ||
json_str = 'BEGIN: "name": "John" - "age": 30 - "city": "New York" :END' | ||
self.assertEqual(fix_and_parse_json(json_str, try_to_fix_with_gpt=True), {"name": "John", "age": 30, "city": "New York"}) | ||
|
||
def test_invalid_json_major_without_gpt(self): | ||
# Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False | ||
json_str = 'BEGIN: "name": "John" - "age": 30 - "city": "New York" :END' | ||
# Assert that this raises an exception: | ||
with self.assertRaises(Exception): | ||
fix_and_parse_json(json_str, try_to_fix_with_gpt=False) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |