Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for json_utils.json_fix_llm #2952

Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
a71e65d
config.py: make load_dotenv(override=True)
coditamar Apr 21, 2023
c83667e
Update Python version and benchmark file in benchmark.yml
waynehamadi Apr 18, 2023
f4db77a
Refactor main function and imports in cli.py
waynehamadi Apr 18, 2023
81d6ae4
Update import statement in ai_config.py
waynehamadi Apr 18, 2023
59c28fb
Add set_temperature and set_memory_backend methods in config.py
waynehamadi Apr 18, 2023
296fab3
Remove unused import in prompt.py
waynehamadi Apr 18, 2023
ca3a441
Add goal oriented tasks workflow
waynehamadi Apr 18, 2023
5653b52
Added agent_utils to create agent
waynehamadi Apr 18, 2023
3327e1f
added pytest and vcrpy
waynehamadi Apr 18, 2023
4a339e1
added write file cassette
waynehamadi Apr 18, 2023
9cc126c
created goal oriented task write file with cassettes to not pay opena…
waynehamadi Apr 18, 2023
35e91a0
solve conflicts
waynehamadi Apr 18, 2023
712a449
add ability set azure because github workflow needs it off
waynehamadi Apr 19, 2023
af0fef1
solve conflicts in cli.py
waynehamadi Apr 19, 2023
11234de
black because linter fails
waynehamadi Apr 19, 2023
baaf4d1
solve conflict
waynehamadi Apr 19, 2023
fb5e916
setup github action to v3
Apr 19, 2023
fdbfa7a
fix conflicts
Apr 22, 2023
8793f67
Merge pull request #2461 from merwanehamadi/feature/create-smoke-test
richbeales Apr 22, 2023
61600b7
Merge pull request #2931 from riensen/fix/multiple-plugins
ntindle Apr 22, 2023
5b06290
Plugins: debug line always printed in plugin load
richbeales Apr 22, 2023
11dd1ef
Merge pull request #2936 from Significant-Gravitas/richbeales-patch-1
BillSchumacher Apr 22, 2023
eea4267
Merge remote-tracking branch 'upstream/master'
coditamar Apr 22, 2023
7257e53
add test for fix_json_using_multiple_techniques
coditamar Apr 22, 2023
c2a2f23
style
coditamar Apr 22, 2023
5269a9c
style
coditamar Apr 22, 2023
0a18684
mocking try_ai_fix to avoid call_ai_function
coditamar Apr 22, 2023
d6d478c
black style
coditamar Apr 22, 2023
5d19be7
mock try_ai_fix to avoid calling the AI model
coditamar Apr 22, 2023
c033ac0
Merge remote-tracking branch 'upstream/master'
coditamar Apr 22, 2023
40744e5
Merge branch 'master' into test_fix_json_using_multiple_techniques
coditamar Apr 22, 2023
29aa989
removed mock, as we can add @requires_api_key("OPEN_API_KEY")
coditamar Apr 22, 2023
f8554f8
style
coditamar Apr 22, 2023
a5e9af2
reverse merge conflict related files and changes
coditamar Apr 23, 2023
4c63b16
Merge remote-tracking branch 'upstream/master'
coditamar Apr 23, 2023
4c6d94a
Merge branch 'master' into test_fix_json_using_multiple_techniques
coditamar Apr 23, 2023
eeeba22
Merge remote-tracking branch 'upstream/master' into test_fix_json_usi…
coditamar Apr 23, 2023
7051cd2
bring back the mock for try_ai_fix
coditamar Apr 23, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/goal_oriented_tasks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Goal Oriented Tasks

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
python-version: ['3.10']

steps:
- name: Check out repository
uses: actions/checkout@v3

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt

- name: Generate coverage report
run: |
coverage run --source=autogpt -m pytest -s -k tests/integration/goal_oriented
env:
OPENAI_API_KEY: 'dummy_api_key'
2 changes: 2 additions & 0 deletions tests/integration/goal_oriented/test_write_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import unittest

import pytest
import vcr

from autogpt.agent import Agent
Expand All @@ -27,6 +28,7 @@
CFG = Config()


@pytest.mark.integration_test
@requires_api_key("OPENAI_API_KEY")
def test_write_file() -> None:
# if file exist
Expand Down
117 changes: 117 additions & 0 deletions tests/unit/test_json_utils_llm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# Generated by CodiumAI
import pytest
from loguru import logger

from autogpt.json_utils.json_fix_llm import (
fix_and_parse_json,
fix_json_using_multiple_techniques,
)
from tests.utils import requires_api_key

"""
Code Analysis

Objective:
- The objective of the function is to fix a given JSON string to make it parseable and fully compliant with two techniques.

Inputs:
- The function takes in a string called 'assistant_reply', which is the JSON string to be fixed.

Flow:
- The function first calls the 'fix_and_parse_json' function to parse and print the Assistant response.
- If the parsed JSON is an empty dictionary, the function calls the 'attempt_to_fix_json_by_finding_outermost_brackets' function to fix the JSON string.
- If the parsed JSON is not an empty dictionary, the function returns the parsed JSON.
- If the parsed JSON is an empty dictionary and cannot be fixed, the function logs an error and returns an empty dictionary.

Outputs:
- The main output of the function is a dictionary containing the fixed JSON string.

Additional aspects:
- The function uses two techniques to fix the JSON string: parsing and finding outermost brackets.
- The function logs an error if the JSON string cannot be fixed and returns an empty dictionary.
- The function uses the 'CFG' object to determine whether to speak the error message or not.
"""


class TestFixJsonUsingMultipleTechniques:
# Tests that the function successfully fixes and parses a JSON string that is already compliant with both techniques.
def test_fix_and_parse_json_happy_path(self):
# Happy path test case where the JSON string is already compliant with both techniques
json_string = '{"text": "Hello world", "confidence": 0.9}'
expected_output = {"text": "Hello world", "confidence": 0.9}
assert fix_json_using_multiple_techniques(json_string) == expected_output

# Tests that the function successfully fixes and parses a JSON string that contains only whitespace characters.
@requires_api_key("OPEN_API_KEY")
def test_fix_and_parse_json_whitespace(self, mocker):
# Happy path test case where the JSON string contains only whitespace characters
json_string = " \n\t "

# mock try_ai_fix to avoid calling the AI model:
# mocker.patch("autogpt.json_utils.json_fix_llm.try_ai_fix", return_value={})

expected_output = {}
assert fix_json_using_multiple_techniques(json_string) == expected_output

# Tests that the function successfully converts a string with arrays to an array
def test_fix_and_parse_json_array(self):
# Happy path test case where the JSON string contains an array of string
json_string = '[ "Add type hints", "Move docstrings", "Consider using" ]'
expected_output = ["Add type hints", "Move docstrings", "Consider using"]
assert fix_json_using_multiple_techniques(json_string) == expected_output

# Tests that the function returns an empty dictionary when the JSON string is not parseable and cannot be fixed using either technique.
@requires_api_key("OPEN_API_KEY")
def test_fix_and_parse_json_can_not(self, mocker):
# Edge case test case where the JSON string is not parseable and cannot be fixed using either technique
json_string = "This is not a JSON string"

# mock try_ai_fix to avoid calling the AI model:
# mocker.patch("autogpt.json_utils.json_fix_llm.try_ai_fix", return_value={})

expected_output = {}

# Use the actual function name in the test
result = fix_json_using_multiple_techniques(json_string)

assert result == expected_output

# Tests that the function returns an empty dictionary when the JSON string is empty.
@requires_api_key("OPEN_API_KEY")
def test_fix_and_parse_json_empty_string(self, mocker):
# Arrange
json_string = ""

# Act
# mock try_ai_fix to avoid calling the AI model:
# mocker.patch("autogpt.json_utils.json_fix_llm.try_ai_fix", return_value={})

result = fix_and_parse_json(json_string)

# Assert
assert result == {}

# Tests that the function successfully fixes and parses a JSON string that contains escape characters.
def test_fix_and_parse_json_escape_characters(self):
# Arrange
json_string = '{"text": "This is a \\"test\\" string."}'

# Act
result = fix_json_using_multiple_techniques(json_string)

# Assert
assert result == {"text": 'This is a "test" string.'}

# Tests that the function successfully fixes and parses a JSON string that contains nested objects or arrays.
def test_fix_and_parse_json_nested_objects(self):
# Arrange
json_string = '{"person": {"name": "John", "age": 30}, "hobbies": ["reading", "swimming"]}'

# Act
result = fix_json_using_multiple_techniques(json_string)

# Assert
assert result == {
"person": {"name": "John", "age": 30},
"hobbies": ["reading", "swimming"],
}