-
Notifications
You must be signed in to change notification settings - Fork 1k
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 Schema Rewrite #651
JSON Schema Rewrite #651
Conversation
Tagging @hudson-ai for interest |
Are you open to including |
May I also suggest changing |
Also for |
@hudson-ai thanks for the Python 3.8 warning :-/ |
guidance/library/_json_schema.py
Outdated
from ._char_range import char_range | ||
from ._one_or_more import one_or_more | ||
from ._optional import optional | ||
from ._zero_or_more import zero_or_more | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can pull these imports straight from the guidance.library
namespace instead of pulling through the private files (https://github.com/guidance-ai/guidance/blob/main/guidance/library/__init__.py)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tweaked a bit. The _grammar
import isn't working, though
A bit annoying, but I recommend something like this for the
This is because as of |
@slundberg do you have thoughts on this? @hudson-ai has submitted a PR to my fork which avoids the problem by using a Python closure to do lazy evaluation. However, I suspect that that is not going to serialise well. I have a feeling that the previous tweak proposed for Can you comment on this? Also, do you want to wait for that to be sorted before merging this? I could make my test case be an XFAIL while the exact update for |
While closures can be a pain for serialization, there is no issue serializing the grammars generated using the approach in my PR (as far as I can tell). The code that generates the grammars has closures, but the grammars themselves don't. That being said, the closure approach feels like a "workaround" to play nicely with how the library currently uses |
Guidance handles what would otherwise be infinitely recursive calls to guidance functions with a little "placeholder" based lazy-evaluation. This works great when the guidance functions take no arguments, but it fails otherwise. Fixing that may be a nice PR in of itself, but in the meantime, we can put ourselves in the zero-arg setting using closures. From @hudson-ai
@hudson-ai I've just tested your change with the example server in guidance, and it does serialise correctly. I've added a test which shows this |
Sorry to be slow here. Infinite recursion is avoided by caching the function calls, however as you point out we just avoid caching all together if the function has an argument. We do this because it is challenging to correctly (and efficiently) check for equality over arbitrary input arguments. Good idea with closures. The alternative would be to add some basic argument equality checking to the guidance function caching (this would make writing these kinds of grammars easier in the future). |
@slundberg I think that adding more caching would be great, although this particular use-case wouldn't be very easy -- simply because the input is an un-hashable dictionary 😅 Off-topic to this PR -- will make an issue when I get the chance: |
@slundberg were there any other changes you wanted to this PR? I think it's ready to merge. |
Ping @slundberg ..... |
Okay took a pass through this and am set to merge once the tests run. Thank you again @riedgar-ms and sorry it took a bit to review. The only change I made was to shorten the name of the library function from Thanks! |
@riedgar-ms I went ahead and merged this, but there seems to be an unrelated build error on the new macOS tests....might be worth looking into. |
Rewriting the JSON Schema generator to be more remote from the actual grammar. @hudson-ai provided the code which allowed recursive structures (such as linked lists) to be created.