-
Notifications
You must be signed in to change notification settings - Fork 279
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
Use OpenAI's Structured Outputs feature to prevent validation errors #514
base: main
Are you sure you want to change the base?
Conversation
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.
interesting, are their any docs on why this might work better?
openai_messages = list(chain(*(self._map_message(m) for m in messages))) | ||
|
||
model_settings = model_settings or {} | ||
|
||
return await self.client.chat.completions.create( | ||
response = await self.client.chat.completions.create( |
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.
you don't need this change.
else: | ||
tool_choice = 'auto' | ||
|
||
tool_choice = 'auto' if self.tools else None |
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 have to respect allow_text_result
, I think right now you're not.
allow_text_result, | ||
tools, | ||
) | ||
response_format = self._map_response_format(result_tools[0]) if result_tools else None |
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.
If there are multiple result tools (which I believe is the case when the result type is a union), we would definitely need to ensure that all the tool calls are present in the final response_format
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.
Also
response_format = self._map_response_format(result_tools[0]) if result_tools else None | |
response_format = self._map_response_format(result_tools[0]) if result_tools and not allow_text_result else None |
(Or, find a way for the response format to allow raw strings)
Used the result_tools parameter to generate a json_schema which is passed to the OpenAI client as response_format. This guides the model to format it's responses according to the given schema, preventing validation errors.