merged master and resolved conflicts

This commit is contained in:
cs0lar
2023-04-15 06:51:04 +01:00
69 changed files with 3219 additions and 1376 deletions

View File

@@ -1,5 +1,6 @@
import sys
import os
import sys
sys.path.insert(0, os.path.abspath(
os.path.join(os.path.dirname(__file__), '../scripts')))
sys.path.insert(
0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../scripts"))
)

View File

@@ -1,18 +1,16 @@
import unittest
import random
import string
import sys
import unittest
from pathlib import Path
# Add the parent directory of the 'scripts' folder to the Python path
sys.path.append(str(Path(__file__).resolve().parent.parent.parent / 'scripts'))
from config import Config
from memory.local import LocalCache
from autogpt.config import Config
from autogpt.memory.local import LocalCache
class TestLocalCache(unittest.TestCase):
def random_string(self, length):
return ''.join(random.choice(string.ascii_letters) for _ in range(length))
return "".join(random.choice(string.ascii_letters) for _ in range(length))
def setUp(self):
cfg = cfg = Config()
@@ -21,10 +19,10 @@ class TestLocalCache(unittest.TestCase):
# Add example texts to the cache
self.example_texts = [
'The quick brown fox jumps over the lazy dog',
'I love machine learning and natural language processing',
'The cake is a lie, but the pie is always true',
'ChatGPT is an advanced AI model for conversation'
"The quick brown fox jumps over the lazy dog",
"I love machine learning and natural language processing",
"The cake is a lie, but the pie is always true",
"ChatGPT is an advanced AI model for conversation",
]
for text in self.example_texts:
@@ -47,5 +45,5 @@ class TestLocalCache(unittest.TestCase):
self.assertIn(self.example_texts[1], relevant_texts)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

View File

@@ -7,10 +7,9 @@ from weaviate import Client
from weaviate.util import get_valid_uuid
from uuid import uuid4
sys.path.append(os.path.abspath('./scripts'))
from config import Config
from memory.weaviate import WeaviateMemory
from memory.base import get_ada_embedding
from autogpt.config import Config
from autogpt.memory.weaviate import WeaviateMemory
from autogpt.memory.base import get_ada_embedding
@mock.patch.dict(os.environ, {
"WEAVIATE_HOST": "127.0.0.1",

View File

@@ -1,21 +1,23 @@
import os
import sys
# Probably a better way:
sys.path.append(os.path.abspath('../scripts'))
from memory.local import LocalCache
from autogpt.memory.local import LocalCache
def MockConfig():
return type('MockConfig', (object,), {
'debug_mode': False,
'continuous_mode': False,
'speak_mode': False,
'memory_index': 'auto-gpt',
})
return type(
"MockConfig",
(object,),
{
"debug_mode": False,
"continuous_mode": False,
"speak_mode": False,
"memory_index": "auto-gpt",
},
)
class TestLocalCache(unittest.TestCase):
def setUp(self):
self.cfg = MockConfig()
self.cache = LocalCache(self.cfg)
@@ -50,5 +52,5 @@ class TestLocalCache(unittest.TestCase):
self.assertEqual(stats, (1, self.cache.data.embeddings.shape))
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,99 @@
# Import the required libraries for unit testing
import os
import sys
import unittest
from autogpt.promptgenerator import PromptGenerator
# Create a test class for the PromptGenerator, subclassed from unittest.TestCase
class promptgenerator_tests(unittest.TestCase):
# Set up the initial state for each test method by creating an instance of PromptGenerator
def setUp(self):
self.generator = PromptGenerator()
# Test whether the add_constraint() method adds a constraint to the generator's constraints list
def test_add_constraint(self):
constraint = "Constraint1"
self.generator.add_constraint(constraint)
self.assertIn(constraint, self.generator.constraints)
# Test whether the add_command() method adds a command to the generator's commands list
def test_add_command(self):
command_label = "Command Label"
command_name = "command_name"
args = {"arg1": "value1", "arg2": "value2"}
self.generator.add_command(command_label, command_name, args)
command = {
"label": command_label,
"name": command_name,
"args": args,
}
self.assertIn(command, self.generator.commands)
# Test whether the add_resource() method adds a resource to the generator's resources list
def test_add_resource(self):
resource = "Resource1"
self.generator.add_resource(resource)
self.assertIn(resource, self.generator.resources)
# Test whether the add_performance_evaluation() method adds an evaluation to the generator's performance_evaluation list
def test_add_performance_evaluation(self):
evaluation = "Evaluation1"
self.generator.add_performance_evaluation(evaluation)
self.assertIn(evaluation, self.generator.performance_evaluation)
# Test whether the generate_prompt_string() method generates a prompt string with all the added constraints, commands, resources and evaluations
def test_generate_prompt_string(self):
constraints = ["Constraint1", "Constraint2"]
commands = [
{
"label": "Command1",
"name": "command_name1",
"args": {"arg1": "value1"},
},
{
"label": "Command2",
"name": "command_name2",
"args": {},
},
]
resources = ["Resource1", "Resource2"]
evaluations = ["Evaluation1", "Evaluation2"]
# Add all the constraints, commands, resources, and evaluations to the generator
for constraint in constraints:
self.generator.add_constraint(constraint)
for command in commands:
self.generator.add_command(
command["label"], command["name"], command["args"]
)
for resource in resources:
self.generator.add_resource(resource)
for evaluation in evaluations:
self.generator.add_performance_evaluation(evaluation)
# Generate the prompt string and verify its correctness
prompt_string = self.generator.generate_prompt_string()
self.assertIsNotNone(prompt_string)
for constraint in constraints:
self.assertIn(constraint, prompt_string)
for command in commands:
self.assertIn(command["name"], prompt_string)
# Check for each key-value pair in the command args dictionary
for key, value in command["args"].items():
self.assertIn(f'"{key}": "{value}"', prompt_string)
for resource in resources:
self.assertIn(resource, prompt_string)
for evaluation in evaluations:
self.assertIn(evaluation, prompt_string)
self.assertIn("constraints", prompt_string.lower())
self.assertIn("commands", prompt_string.lower())
self.assertIn("resources", prompt_string.lower())
self.assertIn("performance evaluation", prompt_string.lower())
# Run the tests when this script is executed
if __name__ == "__main__":
unittest.main()

View File

@@ -1,9 +1,9 @@
import unittest
from scripts.config import Config
from autogpt.config import Config
class TestConfig(unittest.TestCase):
def test_singleton(self):
config1 = Config()
config2 = Config()
@@ -55,5 +55,5 @@ class TestConfig(unittest.TestCase):
self.assertTrue(config.debug_mode)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

View File

@@ -1,7 +1,7 @@
import unittest
import tests.context
from scripts.json_parser import fix_and_parse_json
import tests.context
from autogpt.json_parser import fix_and_parse_json
class TestParseJson(unittest.TestCase):
@@ -21,7 +21,7 @@ class TestParseJson(unittest.TestCase):
# 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'
with self.assertRaises(Exception):
fix_and_parse_json(json_str, try_to_fix_with_gpt=False)
fix_and_parse_json(json_str, try_to_fix_with_gpt=False)
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
@@ -51,23 +51,22 @@ class TestParseJson(unittest.TestCase):
}
}"""
good_obj = {
"command": {
"name": "browse_website",
"args": {
"url": "https://github.com/Torantulino/Auto-GPT"
}
},
"thoughts":
{
"text": "I suggest we start browsing the repository to find any issues that we can fix.",
"reasoning": "Browsing the repository will give us an idea of the current state of the codebase and identify any issues that we can address to improve the repo.",
"plan": "- Look through the repository to find any issues.\n- Investigate any issues to determine what needs to be fixed\n- Identify possible solutions to fix the issues\n- Open Pull Requests with fixes",
"criticism": "I should be careful while browsing so as not to accidentally introduce any new bugs or issues.",
"speak": "I will start browsing the repository to find any issues we can fix."
}
}
"command": {
"name": "browse_website",
"args": {"url": "https://github.com/Torantulino/Auto-GPT"},
},
"thoughts": {
"text": "I suggest we start browsing the repository to find any issues that we can fix.",
"reasoning": "Browsing the repository will give us an idea of the current state of the codebase and identify any issues that we can address to improve the repo.",
"plan": "- Look through the repository to find any issues.\n- Investigate any issues to determine what needs to be fixed\n- Identify possible solutions to fix the issues\n- Open Pull Requests with fixes",
"criticism": "I should be careful while browsing so as not to accidentally introduce any new bugs or issues.",
"speak": "I will start browsing the repository to find any issues we can fix.",
},
}
# Assert that this raises an exception:
self.assertEqual(fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj)
self.assertEqual(
fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj
)
def test_invalid_json_leading_sentence_with_gpt(self):
# Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False
@@ -90,24 +89,23 @@ class TestParseJson(unittest.TestCase):
}
}"""
good_obj = {
"command": {
"name": "browse_website",
"args": {
"url": "https://github.com/Torantulino/Auto-GPT"
"command": {
"name": "browse_website",
"args": {"url": "https://github.com/Torantulino/Auto-GPT"},
},
"thoughts": {
"text": "Browsing the repository to identify potential bugs",
"reasoning": "Before fixing bugs, I need to identify what needs fixing. I will use the 'browse_website' command to analyze the repository.",
"plan": "- Analyze the repository for potential bugs and areas of improvement",
"criticism": "I need to ensure I am thorough and pay attention to detail while browsing the repository.",
"speak": "I am browsing the repository to identify potential bugs.",
},
}
},
"thoughts":
{
"text": "Browsing the repository to identify potential bugs",
"reasoning": "Before fixing bugs, I need to identify what needs fixing. I will use the 'browse_website' command to analyze the repository.",
"plan": "- Analyze the repository for potential bugs and areas of improvement",
"criticism": "I need to ensure I am thorough and pay attention to detail while browsing the repository.",
"speak": "I am browsing the repository to identify potential bugs."
}
}
# Assert that this raises an exception:
self.assertEqual(fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj)
self.assertEqual(
fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj
)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

View File

@@ -1,9 +1,6 @@
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
from autogpt.json_parser import fix_and_parse_json
class TestParseJson(unittest.TestCase):
@@ -16,12 +13,18 @@ class TestParseJson(unittest.TestCase):
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"})
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"})
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
@@ -51,23 +54,22 @@ class TestParseJson(unittest.TestCase):
}
}"""
good_obj = {
"command": {
"name": "browse_website",
"args": {
"url": "https://github.com/Torantulino/Auto-GPT"
}
},
"thoughts":
{
"text": "I suggest we start browsing the repository to find any issues that we can fix.",
"reasoning": "Browsing the repository will give us an idea of the current state of the codebase and identify any issues that we can address to improve the repo.",
"plan": "- Look through the repository to find any issues.\n- Investigate any issues to determine what needs to be fixed\n- Identify possible solutions to fix the issues\n- Open Pull Requests with fixes",
"criticism": "I should be careful while browsing so as not to accidentally introduce any new bugs or issues.",
"speak": "I will start browsing the repository to find any issues we can fix."
}
}
"command": {
"name": "browse_website",
"args": {"url": "https://github.com/Torantulino/Auto-GPT"},
},
"thoughts": {
"text": "I suggest we start browsing the repository to find any issues that we can fix.",
"reasoning": "Browsing the repository will give us an idea of the current state of the codebase and identify any issues that we can address to improve the repo.",
"plan": "- Look through the repository to find any issues.\n- Investigate any issues to determine what needs to be fixed\n- Identify possible solutions to fix the issues\n- Open Pull Requests with fixes",
"criticism": "I should be careful while browsing so as not to accidentally introduce any new bugs or issues.",
"speak": "I will start browsing the repository to find any issues we can fix.",
},
}
# Assert that this raises an exception:
self.assertEqual(fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj)
self.assertEqual(
fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj
)
def test_invalid_json_leading_sentence_with_gpt(self):
# Test that a REALLY invalid JSON string raises an error when try_to_fix_with_gpt is False
@@ -90,24 +92,23 @@ class TestParseJson(unittest.TestCase):
}
}"""
good_obj = {
"command": {
"name": "browse_website",
"args": {
"url": "https://github.com/Torantulino/Auto-GPT"
"command": {
"name": "browse_website",
"args": {"url": "https://github.com/Torantulino/Auto-GPT"},
},
"thoughts": {
"text": "Browsing the repository to identify potential bugs",
"reasoning": "Before fixing bugs, I need to identify what needs fixing. I will use the 'browse_website' command to analyze the repository.",
"plan": "- Analyze the repository for potential bugs and areas of improvement",
"criticism": "I need to ensure I am thorough and pay attention to detail while browsing the repository.",
"speak": "I am browsing the repository to identify potential bugs.",
},
}
},
"thoughts":
{
"text": "Browsing the repository to identify potential bugs",
"reasoning": "Before fixing bugs, I need to identify what needs fixing. I will use the 'browse_website' command to analyze the repository.",
"plan": "- Analyze the repository for potential bugs and areas of improvement",
"criticism": "I need to ensure I am thorough and pay attention to detail while browsing the repository.",
"speak": "I am browsing the repository to identify potential bugs."
}
}
# Assert that this raises an exception:
self.assertEqual(fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj)
self.assertEqual(
fix_and_parse_json(json_str, try_to_fix_with_gpt=False), good_obj
)
if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

View File

@@ -1,4 +1,3 @@
# Generated by CodiumAI
# Dependencies:
@@ -39,7 +38,6 @@ requests and parse HTML content, respectively.
class TestScrapeLinks:
# Tests that the function returns a list of formatted hyperlinks when
# provided with a valid url that returns a webpage with hyperlinks.
def test_valid_url_with_hyperlinks(self):
@@ -54,8 +52,10 @@ class TestScrapeLinks:
# Mock the requests.get() function to return a response with sample HTML containing hyperlinks
mock_response = mocker.Mock()
mock_response.status_code = 200
mock_response.text = "<html><body><a href='https://www.google.com'>Google</a></body></html>"
mocker.patch('requests.get', return_value=mock_response)
mock_response.text = (
"<html><body><a href='https://www.google.com'>Google</a></body></html>"
)
mocker.patch("requests.get", return_value=mock_response)
# Call the function with a valid URL
result = scrape_links("https://www.example.com")
@@ -68,7 +68,7 @@ class TestScrapeLinks:
# Mock the requests.get() function to return an HTTP error response
mock_response = mocker.Mock()
mock_response.status_code = 404
mocker.patch('requests.get', return_value=mock_response)
mocker.patch("requests.get", return_value=mock_response)
# Call the function with an invalid URL
result = scrape_links("https://www.invalidurl.com")
@@ -82,7 +82,7 @@ class TestScrapeLinks:
mock_response = mocker.Mock()
mock_response.status_code = 200
mock_response.text = "<html><body><p>No hyperlinks here</p></body></html>"
mocker.patch('requests.get', return_value=mock_response)
mocker.patch("requests.get", return_value=mock_response)
# Call the function with a URL containing no hyperlinks
result = scrape_links("https://www.example.com")
@@ -105,7 +105,7 @@ class TestScrapeLinks:
</body>
</html>
"""
mocker.patch('requests.get', return_value=mock_response)
mocker.patch("requests.get", return_value=mock_response)
# Call the function being tested
result = scrape_links("https://www.example.com")

View File

@@ -1,9 +1,8 @@
# Generated by CodiumAI
import requests
from scripts.browse import scrape_text
from autogpt.browse import scrape_text
"""
Code Analysis
@@ -35,7 +34,6 @@ Additional aspects:
class TestScrapeText:
# Tests that scrape_text() returns the expected text when given a valid URL.
def test_scrape_text_with_valid_url(self, mocker):
# Mock the requests.get() method to return a response with expected text
@@ -74,7 +72,7 @@ class TestScrapeText:
# Tests that the function returns an error message when the response status code is an http error (>=400).
def test_http_error(self, mocker):
# Mock the requests.get() method to return a response with a 404 status code
mocker.patch('requests.get', return_value=mocker.Mock(status_code=404))
mocker.patch("requests.get", return_value=mocker.Mock(status_code=404))
# Call the function with a URL
result = scrape_text("https://www.example.com")