Merge remote-tracking branch 'origin/master' into fix_agents

This commit is contained in:
Gershon Bialer
2023-04-15 10:04:15 -07:00
77 changed files with 2683 additions and 1992 deletions

60
tests/smoke_test.py Normal file
View File

@@ -0,0 +1,60 @@
import os
import subprocess
import sys
import unittest
from autogpt.file_operations import delete_file, read_file
env_vars = {
'MEMORY_BACKEND': 'no_memory',
'TEMPERATURE': "0"
}
class TestCommands(unittest.TestCase):
def test_write_file(self):
# Test case to check if the write_file command can successfully write 'Hello World' to a file
# named 'hello_world.txt'.
# Read the current ai_settings.yaml file and store its content.
ai_settings = None
if os.path.exists('ai_settings.yaml'):
with open('ai_settings.yaml', 'r') as f:
ai_settings = f.read()
os.remove('ai_settings.yaml')
try:
if os.path.exists('hello_world.txt'):
# Clean up any existing 'hello_world.txt' file before testing.
delete_file('hello_world.txt')
# Prepare input data for the test.
input_data = '''write_file-GPT
an AI designed to use the write_file command to write 'Hello World' into a file named "hello_world.txt" and then use the task_complete command to complete the task.
Use the write_file command to write 'Hello World' into a file named "hello_world.txt".
Use the task_complete command to complete the task.
Do not use any other commands.
y -5
EOF'''
command = f'{sys.executable} -m autogpt'
# Execute the script with the input data.
process = subprocess.Popen(command, stdin=subprocess.PIPE, shell=True, env={**os.environ, **env_vars})
process.communicate(input_data.encode())
# Read the content of the 'hello_world.txt' file created during the test.
content = read_file('hello_world.txt')
finally:
if ai_settings:
# Restore the original ai_settings.yaml file.
with open('ai_settings.yaml', 'w') as f:
f.write(ai_settings)
# Check if the content of the 'hello_world.txt' file is equal to 'Hello World'.
self.assertEqual(content, 'Hello World', f"Expected 'Hello World', got {content}")
# Run the test case.
if __name__ == '__main__':
unittest.main()

View File

@@ -1,7 +1,7 @@
import unittest
import tests.context
from autogpt.json_parser import fix_and_parse_json
from autogpt.json_fixes.parsing import fix_and_parse_json
class TestParseJson(unittest.TestCase):

View File

@@ -0,0 +1,61 @@
import unittest
import tests.context
from autogpt.token_counter import count_message_tokens, count_string_tokens
class TestTokenCounter(unittest.TestCase):
def test_count_message_tokens(self):
messages = [
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi there!"}
]
self.assertEqual(count_message_tokens(messages), 17)
def test_count_message_tokens_with_name(self):
messages = [
{"role": "user", "content": "Hello", "name": "John"},
{"role": "assistant", "content": "Hi there!"}
]
self.assertEqual(count_message_tokens(messages), 17)
def test_count_message_tokens_empty_input(self):
self.assertEqual(count_message_tokens([]), 3)
def test_count_message_tokens_invalid_model(self):
messages = [
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi there!"}
]
with self.assertRaises(KeyError):
count_message_tokens(messages, model="invalid_model")
def test_count_message_tokens_gpt_4(self):
messages = [
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi there!"}
]
self.assertEqual(count_message_tokens(messages, model="gpt-4-0314"), 15)
def test_count_string_tokens(self):
string = "Hello, world!"
self.assertEqual(count_string_tokens(string, model_name="gpt-3.5-turbo-0301"), 4)
def test_count_string_tokens_empty_input(self):
self.assertEqual(count_string_tokens("", model_name="gpt-3.5-turbo-0301"), 0)
def test_count_message_tokens_invalid_model(self):
messages = [
{"role": "user", "content": "Hello"},
{"role": "assistant", "content": "Hi there!"}
]
with self.assertRaises(NotImplementedError):
count_message_tokens(messages, model="invalid_model")
def test_count_string_tokens_gpt_4(self):
string = "Hello, world!"
self.assertEqual(count_string_tokens(string, model_name="gpt-4-0314"), 4)
if __name__ == '__main__':
unittest.main()

View File

@@ -2,7 +2,7 @@
import requests
from autogpt.browse import scrape_text
from autogpt.commands.web_requests import scrape_text
"""
Code Analysis

View File

@@ -1,4 +1,5 @@
from autogpt import commands
import autogpt.agent.agent_manager as agent_manager
from autogpt.app import start_agent, list_agents
import unittest
from unittest.mock import patch, MagicMock
@@ -9,9 +10,9 @@ class TestCommands(unittest.TestCase):
obj = MagicMock()
obj.response.choices[0].messages[0].content = "Test message"
mock.return_value = obj
commands.start_agent("Test Agent", "chat", "Hello, how are you?", "gpt2")
agents = commands.list_agents()
start_agent("Test Agent", "chat", "Hello, how are you?", "gpt2")
agents = list_agents()
self.assertEqual("List of agents:\n0: chat", agents)
commands.start_agent("Test Agent 2", "write", "Hello, how are you?", "gpt2")
agents = commands.list_agents()
start_agent("Test Agent 2", "write", "Hello, how are you?", "gpt2")
agents = list_agents()
self.assertEqual("List of agents:\n0: chat\n1: write", agents)