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

Fixed Json Error #285

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions .idea/Auto-GPT.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 61 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions auto_gpt_workspace/communication_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This is a Python file for our proof of concept communication application.
12 changes: 12 additions & 0 deletions auto_gpt_workspace/fibonacci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
def fibonacci(n, memo={}):
if n < 0:
raise ValueError('Input must be a non-negative integer')
elif n == 0:
return 0
elif n == 1 or n == 2:
return 1
elif n in memo:
return memo[n]
else:
memo[n] = fibonacci(n-1, memo) + fibonacci(n-2, memo)
return memo[n]
5 changes: 5 additions & 0 deletions auto_gpt_workspace/goals.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1. Design and deploy a system that I can communicate and provide feedback to you.
2. Provide recommendations for improving our effectiveness to work as a team.
3. Generate further autonomous agents to streamline processes and accomplish all tasks that I communicate to you.
4. Design and code a working application that accomplishes all of the above, then deploy it.
5. Execute all goals in this order then terminate because the improved application has been deployed and we will continue working together on that application.
5 changes: 5 additions & 0 deletions auto_gpt_workspace/recommended_tools.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Sketch
InVision
Figma
Adobe XD
Axure
50 changes: 28 additions & 22 deletions scripts/json_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from config import Config
cfg = Config()


def fix_and_parse_json(json_str: str, try_to_fix_with_gpt: bool = True):
json_schema = """
{
Expand All @@ -23,30 +24,35 @@ def fix_and_parse_json(json_str: str, try_to_fix_with_gpt: bool = True):
}
"""

try:
return json.loads(json_str)
except Exception as e:
# Let's do something manually - sometimes GPT responds with something BEFORE the braces:
# "I'm sorry, I don't understand. Please try again."{"text": "I'm sorry, I don't understand. Please try again.", "confidence": 0.0}
# So let's try to find the first brace and then parse the rest of the string
def clean_input(json_str):
brace_index = json_str.index("{")
json_str = json_str[brace_index:]
last_brace_index = json_str.rindex("}")
json_str = json_str[:last_brace_index+1]
return json_str

def attempt_parse(json_str):
Copy link

Choose a reason for hiding this comment

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

Why not un-nest these functions while you're at it?

try:
brace_index = json_str.index("{")
json_str = json_str[brace_index:]
last_brace_index = json_str.rindex("}")
json_str = json_str[:last_brace_index+1]
return json.loads(json_str)
return json.loads(json_str)
except Exception as e:
if try_to_fix_with_gpt:
print(f"Warning: Failed to parse AI output, attempting to fix.\n If you see this warning frequently, it's likely that your prompt is confusing the AI. Try changing it up slightly.")
Copy link

Choose a reason for hiding this comment

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

Why get rid of this message about confusing the AI?

Copy link
Member

Choose a reason for hiding this comment

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

Agreed, please add this back in.

# Now try to fix this up using the ai_functions
ai_fixed_json = fix_json(json_str, json_schema, False)
if ai_fixed_json != "failed":
return json.loads(ai_fixed_json)
else:
print(f"Failed to fix ai output, telling the AI.") # This allows the AI to react to the error message, which usually results in it correcting its ways.
return json_str
else:
raise e
return None
Copy link

Choose a reason for hiding this comment

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

Why not keep the try/except error handling paradigm?


cleaned_json_str = clean_input(json_str)
parsed_json = attempt_parse(cleaned_json_str)

if parsed_json is not None:
return parsed_json
elif try_to_fix_with_gpt:
print("Warning: Failed to parse AI output, attempting to fix.")
ai_fixed_json = fix_json(cleaned_json_str, json_schema, False)

if ai_fixed_json != "failed":
return json.loads(ai_fixed_json)
else:
print("Failed to fix ai output, telling the AI.")
return json_str

raise ValueError("Failed to parse JSON: '{}'".format(json_str))

def fix_json(json_str: str, schema: str, debug=False) -> str:
# Try to fix the JSON using gpt:
Expand Down
Binary file added speech.mp3
Binary file not shown.