minimaxir / simpleaichat

Python package for easily interfacing with chat apps, with robust features and minimal code complexity.
MIT License
3.43k stars 224 forks source link

Preserve OpenAI's response order #36

Closed keyboardAnt closed 1 year ago

keyboardAnt commented 1 year ago

In the schema_ttrpg.ipynb example, it's mentioned that "orjson.dumps preserves field order from the ChatGPT API". This order preservation could be beneficial for certain use-cases, where the order of fields could carry meaningful information or improve readability.

Considering this, I propose that we replace pydantic.BaseModel with a subclass that inherits from it but also ensures that field order is preserved by default. This change would ensure that field order is preserved without needing to rely on orjson.dumps.

To implement this, we could consider using collections.OrderedDict in our subclass to store model attributes.

minimaxir commented 1 year ago

Internally, the field order of the dictionary already is in the correct order since Python preserved order by default since 3.6, and orjson.loads does it correctly when taking in the structured response.

I added that note in that notebook because certain implementations of pretty printing reorder the field names lexigraphically which was annoying to demo. So nothing to change here.