-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JSON validation and error handling in agent.py and chatgpt2api.py
- Loading branch information
Showing
3 changed files
with
40 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import json | ||
|
||
json_data = '{"prompt":"\n\n' | ||
def check_json(json_data): | ||
while True: | ||
try: | ||
json.loads(json_data) | ||
break | ||
except json.decoder.JSONDecodeError as e: | ||
print("JSON error:", e) | ||
print("JSON body", repr(json_data)) | ||
if "Invalid control character" in str(e): | ||
json_data = json_data.replace("\n", "\\n") | ||
if "Unterminated string starting" in str(e): | ||
json_data += '"}' | ||
return json_data | ||
print(json.loads(check_json(json_data))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters