Skip to content
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

OpenAI result_format JSON #720

Open
ryanpeach opened this issue Mar 26, 2024 · 4 comments
Open

OpenAI result_format JSON #720

ryanpeach opened this issue Mar 26, 2024 · 4 comments

Comments

@ryanpeach
Copy link
Contributor

ryanpeach commented Mar 26, 2024

Is your feature request related to a problem? Please describe.

OpenAI now provides a JSON response format! https://community.openai.com/t/how-do-i-use-the-new-json-mode/475890/14

Since OpenAI does not support enforced grammar, this is a big deal.

Describe the solution you'd like

The json grammar should trigger this functionality in the OpenAIChatEngine

@ryanpeach
Copy link
Contributor Author

Relevant lines of code:

generator = self.client.chat.completions.create(
model=self.model_name,
messages=messages,
max_tokens=self.max_streaming_tokens,
n=1,
top_p=1.0,# TODO: this should be controllable like temp (from the grammar)
temperature=temperature,
stream=True
)

@guidance(stateless=True)
def json(lm, json_schema: Mapping[str, Any], name: Optional[str] = None):
_DEFS_KEY = "$defs"
definitions = {}
if _DEFS_KEY in json_schema:
definitions = _build_definitions(json_schema[_DEFS_KEY])
return lm + guidance.capture(_gen_json(json_schema, definitions), name=name)

@ryanpeach
Copy link
Contributor Author

Need to upgrade openai to 1.1 based on openai/openai-python@v1.0.1...v1.1.0

@ryanpeach
Copy link
Contributor Author

@ryanpeach
Copy link
Contributor Author

Related PR's #699 #724

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant