ju-bezdek / langchain-decorators

syntactic sugar 🍭 for langchain
MIT License
228 stars 11 forks source link

Suggestion on how to improve your code using json_repair #11

Closed mangiucugna closed 11 months ago

mangiucugna commented 11 months ago

Hi, I am the maintainer of json_repair and noticed that you can refactor this code and make it faster: https://github.com/ju-bezdek/langchain-decorators/blob/539bec41bffdf3a893ba0762788326dee2d7c21f/src/langchain_decorators/output_parsers.py#L133C5-L145C5 like so:

from json_repair import loads
try:
  json_dict = loads(json_str)
except ValueError
  raise OutputParserExceptionWithOriginal(msg, text, error_code=ErrorCodes.INVALID_JSON)

this version makes your code dependent on json_repair so you might not want that, in that case you still use the options return_objects=True and skip_json_loads=True to make your code faster since those will avoid the additional json.loads() and return an object and not a string (as you want in your code)

Cheers

ju-bezdek commented 11 months ago

Thanks for the feedback !

I'll add it to the next release

... yeah, i didnt want the dependency, since its really rare these days that LLM wont produce valid JSON, (depends on the model ofc) ... but it still happens.

mangiucugna commented 11 months ago

great to hear, if you encounter any bug or have any feature request just open an issue!