-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Error occurs in structured data extraction using deeply nested Pydantic models #1871
Comments
I see that |
Seems quite interesting! |
@okhat This is the Script to process the Pydantic JSON schema. I hope the community will help to test and improve it. |
The error is due to the following part in the try:
provider = lm.model.split('/', 1)[0] or "openai"
if 'response_format' in litellm.get_supported_openai_params(model=lm.model, custom_llm_provider=provider):
outputs = lm(**inputs, **lm_kwargs, response_format={ "type": "json_object" })
else:
outputs = lm(**inputs, **lm_kwargs)
except litellm.UnsupportedParamsError:
outputs = lm(**inputs, **lm_kwargs) The above fails for OpenAI compatible API hosted using LM Studio in my case |
Thanks so much @rohitgarud !! This does seem elegant to me, but we'll need to test out whether it's more portable/reliable than the current approach or not. Tagging @dbczumar who is thinking about how to best handle types in adapters for future reference to this. |
Thank you, @okhat I am working on a custom JSONAdapter for data extraction using smaller models/APIs for which |
I have updated the ProcessSchema script to directly output JSON-like schema as a string without quotes, which further reduces the number of tokens. Works really well with even smaller local models. Defining the Pydantic models well is crucial for avoiding Pydantic validation errors on the extracted JSONs. I am also working on getting output JSON without quotes and newline characters for formatting, and |
This is the CustomJSONAdapter using the ProcessSchema. @okhat I believe that the Basic Chat and JSON adapters could benefit from some restructuring to facilitate easier customization of their individual components |
Can anybody help me write tests for this? There are a lot of scenarios. |
This is an example of a deeply nested JSON schema generated due to Pydantic type hint on Signature outputfield, which is huge and causes smaller models like Llama 3.18b with Openai compatibility API to fail with error:
While experimenting I came across BAML and tried to call the LLM directly by modifying the JSON schema to something similar to BAML representation and it worked really well:
Example of JSON schema from Pydantic, which is directly injected in the DSPy prompt:
I have written a script to convert it into something like the following:
This is less verbose, less number of tokens and concise and gives good results after some postprocessing. Something similar to BAML can be implemented to parse the output into a desired format.
Please let me know your thoughts about this approach.
The text was updated successfully, but these errors were encountered: