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

Json correction #26

Merged
merged 2 commits into from
Sep 17, 2023
Merged

Conversation

velocitatem
Copy link
Contributor

@velocitatem velocitatem commented Sep 16, 2023

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

  • Bug Fix: Implemented a new function correct_json() to ensure the JSON response from OpenAI is valid. This prevents potential errors during further processing.
  • Refactor: Updated get_response_data() function to use correct_json(), enhancing the reliability of data extraction.
  • Refactor: Modified get_response_data() to return a tuple (response_data, 200), improving the clarity of response status handling.

@yoheinakajima
Copy link
Owner

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!

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Sep 17, 2023

Walkthrough

The changes primarily focus on enhancing the handling of OpenAI's JSON response. A new function correct_json() is introduced to ensure the validity of the JSON data. The get_response_data() function now utilizes this correction mechanism and returns a tuple for better error handling.

Changes

File Summary
main.py Introduced correct_json() to validate and correct JSON responses from OpenAI. Updated get_response_data() to use this function and return a tuple (response_data, 200). Minor formatting updates and comments added for clarity.

🐇💻

In the land of code, where the brackets lie,

A rabbit hopped in, with an eagle's eye.

Fixed some JSON, made it shine,

Now the data flows, oh so fine!

With tuples returned, and errors caught,

A better script, the rabbit has wrought! 🎉


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • Mention @coderabbitai in any review comment for bot assistance.
  • Note: Review comments are made on code diffs or files, not on the PR overview.

Pause Incremental Reviews

  • Insert @coderabbitai: ignore in the PR description to halt the bot's ongoing reviews. Remove the line to resume.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

Commits Files that changed from the base of the PR and between bd9cc5a and 5b13698 commits.
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 function correct_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.

Comment on lines 149 to +150
response_data = completion.choices[0]["message"]["function_call"]["arguments"]
response_data = correct_json(response_data)
Copy link
Contributor

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
Copy link
Contributor

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

@yoheinakajima yoheinakajima merged commit d2a7e91 into yoheinakajima:main Sep 17, 2023
@shepherd-06 shepherd-06 mentioned this pull request Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants