-
Notifications
You must be signed in to change notification settings - Fork 280
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
Json correction #26
Json correction #26
Conversation
Hey I love this! I'm reviewing from mobile which makes why merge conflicts hard to manage, but if you're able to fix happy to pull this in! |
75db3a9
to
5b13698
Compare
WalkthroughThe changes primarily focus on enhancing the handling of OpenAI's JSON response. A new function Changes
TipsChat with CodeRabbit Bot (
|
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.
Review Status
Actionable comments generated: 2
Files selected for processing (1)
- main.py (5 hunks)
Additional comments (Suppressed): 3
main.py (3)
3-3: The
re
module has been imported to handle and correct JSON formatting issues in the new functioncorrect_json()
.47-55: A new function
correct_json()
has been introduced to correct JSON formatting issues, specifically trailing commas. This function uses regular expressions to replace invalid JSON patterns with valid ones.+ def correct_json(response_data): + """ + Corrects the JSON response from OpenAI to be valid JSON + """ + response_data = re.sub( + r',\s*}', '}', + re.sub(r',\s*]', ']', + re.sub(r'(\w+)\s*:', r'"\1":', response_data))) + return response_data
- 172-172: The return type of
get_response_data()
has been modified to return a tuple(response_data, 200)
. Ensure that all calls to this function throughout the codebase have been updated to handle the new return type.
response_data = completion.choices[0]["message"]["function_call"]["arguments"] | ||
response_data = correct_json(response_data) |
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.
The get_response_data()
function now calls the new correct_json()
function to ensure that all response data is corrected for JSON formatting before further processing.
response_data = completion.choices[0]["message"]["function_call"]["arguments"]
+ response_data = correct_json(response_data)
@@ -158,7 +169,7 @@ def get_response_data(): | |||
except json.decoder.JSONDecodeError as jde: | |||
return jsonify({"error": "".format(jde)}), 500 |
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.
Error messages are not being formatted correctly due to missing arguments in the format method. This will result in error messages that simply say "{}".
- return jsonify({"error": "".format(e)}), 400
+ return jsonify({"error": str(e)}), 400
- return jsonify({"error": "".format(jde)}), 500
+ return jsonify({"error": str(jde)}), 500
Hi! Love the project! I observed that when dealing with larger inputs, the integration often struggles to properly parse the JSON data, usually due to trailing commas. To address this, I've implemented a function that automatically corrects the JSON formatting for those cases :)
Summary by CodeRabbit
Release Notes
correct_json()
to ensure the JSON response from OpenAI is valid. This prevents potential errors during further processing.get_response_data()
function to usecorrect_json()
, enhancing the reliability of data extraction.get_response_data()
to return a tuple(response_data, 200)
, improving the clarity of response status handling.