Automatic repair of json for langchains agent (#444)

* Added json_repair to Pipfile

* Automatic repair of json for langchains agent
This commit is contained in:
Erik Nilsson 2024-04-01 04:52:59 +02:00 committed by GitHub
parent 0e3c86ad59
commit 4404b9af24
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 1 deletions

View File

@ -25,6 +25,7 @@ llama-index-embeddings-azure-openai = "*"
llama-index-embeddings-ollama = "*"
google-generativeai = "*"
toml = "*"
json_repair = "*"
[dev-packages]

View File

@ -1,4 +1,5 @@
import json
from json_repair import repair_json
def my_encoder(obj):
if hasattr(obj, "to_dict"):
@ -8,5 +9,11 @@ def dumps(obj, **kwargs):
return json.dumps(obj, default=my_encoder, **kwargs)
def loads(s, **kwargs):
return json.loads(s, **kwargs)
s_repaired = repair_json(s)
if s_repaired != s:
print(f"Repaired JSON: {s_repaired}")
print(f"Original JSON: {s}")
return json.loads(s_repaired, **kwargs)