Integrate OpenAI API Structured Generation #1142
Merged
+197
−289
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Rendered Docs
Improvements
Enables use of OpenAI's structured generation endpoint via
generate.choice
andgenerate.json
Enable use with arbitrary OpenAI-compatible endpoints
tokenizer
was removed as a requirement formodels.openai
because the logit bias filter no longer needs to be constructed.This has the side effect of fixing a number of issues users were having with using arbitrary OpenAI-compatible endpoints.
Verified by author of #1135 and in a brief smoke test at the bottom of this description.
Changes
models.openai
to support OpenAI standardresponse_format
+json_schema
outlines.generate.json
andoutlines.generate.choice
to usejson_schema
formodels.openai
generation.gpt-3.5
andgpt-4
.tiktoken
requirement inmodels.openai
, and remove dead code from oldgenerate.choice
handlingFix
main
Unrelated to this PR, but
pre-commit
was failing due tohome.html
andmain.html
.Smoke Testing
Testing via CI with real API keys isn't feasible. Mocked endpoint tests are implemented, but don't provide strong guarantees.
Here are smoke tests that I ran to verify behavior:
OpenAI Structured Generation
generate.text()
with any openai-compatible endpointpython3 examples/react.py
Updated this to use new interface. Works as expected.