fix: self healing response parser

This commit is contained in:
Florian Hönicke
2023-05-26 17:35:00 +02:00
parent 70b37f7d14
commit 43bcfc31e2
5 changed files with 80 additions and 10 deletions

View File

@@ -1,6 +1,8 @@
import json
import re
from dev_gpt.options.generate.chains.fix_based_on_error import fix_based_on_error_chain
def identity_parser(x):
return x
@@ -16,6 +18,10 @@ def boolean_parser(x):
def json_parser(x):
if '```' in x:
pattern = r'```(.+)```'
x = re.findall(pattern, x, re.DOTALL)[-1]
pattern = r'```(json)?(.+)```'
x = re.findall(pattern, x, re.DOTALL)[-1][-1]
return json.loads(x)
def self_healing_json_parser(original_json_string):
return fix_based_on_error_chain('I want to load my JSON string using json.loads(x) but get the following error:', 'JSON',
original_json_string, parser=json_parser)