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

Fix: json decoder issue(s) #4104

Closed

Conversation

exy02
Copy link

@exy02 exy02 commented May 11, 2023

Background

This PR helps address multiple issues regarding json decode issues (e.g. #1407 , #4091, #4052, etc)

Changes

  1. Cleaned up update_running_summary() logic
  • Pre-filter new_events for only non-'user' events
  • Each event was not being manipulated at the list level, adjusted logic to modify each event within new_events
  • Added try/except logic to handle json load errors and log said errors appropriately
  • Implemented .pop() in lieu of .remove(), index-based removal ensures consistent deletion of each target event
  • NOTE: Need a double check to confirm '"thoughts":' is the correct identifier when scanning event["content"]
  1. Minor change to improve get_newly_trimmed_messages() (I can move this to a separate PR if needed)
  • Index slicing seems more appropriate here rather than list comprehension

Documentation

Added in-code comments and left existing in-code unchanged

Test Plan

Tested locally by isolating the affected portion of code (used existing test scripts to help generate sample data and verify logic)

PR Quality Checklist

  • My pull request is atomic and focuses on a single change.
  • I have thoroughly tested my changes with multiple different prompts.
  • I have considered potential risks and mitigations for my changes.
  • I have documented my changes clearly and comprehensively.
  • I have not snuck in any "extra" small tweaks changes

exy02 added 2 commits May 10, 2023 22:18
- For loop now uses enumerate to identify event by index (cleaner)
- Each event was not being manipulated at the list level, adjusted logic to modify each event within new_events
- Added try/except logic to handle json load errors and log said errors appropriately
- Implemented .pop() in lieu of .remove(), index-based removal ensures consistent deletion of each target event

NOTE: Need a double check to confirm '"thoughts":' is the correct identifier when scanning event["content"]
Index slicing seems more appropriate here rather than list comprehension. Also len(full_message_history) is O(1)
@vercel
Copy link

vercel bot commented May 11, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
docs ⬜️ Ignored (Inspect) May 13, 2023 1:06am

@exy02
Copy link
Author

exy02 commented May 11, 2023

NOTE: Maybe worth moving the for-loop to a new function? This will make unit testing much easier in the long-run.
Example:

def cleanup_new_events(new_events:List[Dict[str,str]]) -> List[Dict[str,str]]:
    """ """
    new_events = [event for event in new_events if event["role"].lower() != "user"]
    # Replace "assistant" with "you". This produces much better first person past tense results.
    for i, event in enumerate(new_events):
        if event["role"].lower() == "assistant":
            new_events[i]["role"] = "you"

            # Remove "thoughts" dictionary from "content"
            #TODO: Need a double check here to confirm '"thoughts":' is the correct identifier.
            if '"thoughts":' in event["content"]:
                try:
                    content_dict = json.loads(event["content"])
                    del content_dict["thoughts"]
                    new_events[i]["content"] = json.dumps(content_dict)
                except:
                    error_str = f"ERROR: invalid json format when attempting to remove 'thoughts' content from event: {event}"
                    logger.error(error_str)
                    continue
        elif event["role"].lower() == "system":
            new_events[i]["role"] = "your computer" 
    return new_events

Copy link
Contributor

@Slowly-Grokking Slowly-Grokking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems to have fixed the jsondecoder error i was receiving.
would on average fail within 20 steps before, have done y -100 now without error.

@anonhostpi
Copy link

@anonhostpi
Copy link

anonhostpi commented May 12, 2023

Definitely needs to be fixed. Just referencing other similar PRs

@exy02
Copy link
Author

exy02 commented May 12, 2023

Duplicate efforts: #3996, #3923

You can find more on https://github.com/anonhostpi/AUTOGPT.TRACKERS/blob/main/TOPICS/0017.BUGS/JSON.md

Thanks @anonhostpi for linking the related/relevant PRs;

Note:
Post-review, it looks like both #3996 and #3923 attempt to resolve the error via try/except. However, both PRs only resolve part of the issue.

Local testing suggests elif event["role"] == "user": new_events.remove(event) was causing unanticipated effects on the function as whole (hence why I removed .remove() logic and reverted .pop() logic). Instead list comprehension was implemented to pre-filter the events and exclude all "user" events.

Though I do like the use of except json.decoder.JSONDecodeError observed in both related PRs, which can be added this PR if needed.

@github-actions github-actions bot added the conflicts Automatically applied to PRs with merge conflicts label May 12, 2023
@github-actions
Copy link
Contributor

This pull request has conflicts with the base branch, please resolve those so we can evaluate the pull request.

exy02 added 2 commits May 12, 2023 17:36
Logic moved to separate function (for building tests). Logic also updated to match approved PR while fixing remaining issues.
@Pwuts
Copy link
Member

Pwuts commented Jun 14, 2023

@erik-megarad

@Pwuts
Copy link
Member

Pwuts commented Jul 14, 2023

Thanks for this, we definitely had some issues with this component in the beginning. The "summary memory" was re-implemented by #4208, and as far as I can see that also addresses the issues fixed by your PR.
As such, I'm closing this. With a big thanks for the submission, and an apology for not taking it in earlier!

@Pwuts Pwuts closed this Jul 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conflicts Automatically applied to PRs with merge conflicts size/l
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants