-
Notifications
You must be signed in to change notification settings - Fork 44.7k
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
Fixed Json Error #285
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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. |
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] |
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Sketch | ||
InVision | ||
Figma | ||
Adobe XD | ||
Axure |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = """ | ||
{ | ||
|
@@ -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): | ||
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.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why get rid of this message about confusing the AI? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
There was a problem hiding this comment.
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?