Live data from Hacker News

Show HN: GPT-JSON – Structured and typehinted GPT responses in Python

github.com

21–30 of 76 posts

Re: Show HN: GPT-JSON – Structured and typehinted GPT responses in Python

#22
I built a toy[0] for Typescript that works similarly to this.

It takes the expected return type of a function, translates it into a JSON Schema, queries OpenAI, validates the response, and then magically returns the response as typed output.

[0]https://github.com/jumploops/magic

Re: Show HN: GPT-JSON – Structured and typehinted GPT responses in Python

#23
I asked ChatGPT (GPT-4) about the idea of integrating GPT-JSON with CUE language (https://cuelang.org/) for potential improvements and the answers are quite impressive:

"Yes, integrating the CUE language with GPT-JSON can potentially lead to improvements in its capability. CUE, short for Configuration, Unification, and Environment, is an open-source data constraint language designed to simplify the definition, generation, and validation of data in structured formats, like JSON, YAML, and others.

Integrating CUE with GPT-JSON can bring several benefits:

1) Validation: By using CUE, you can define schemas for the input and output JSON, which helps in validating the data structure and ensuring it adheres to the expected format. This can help reduce errors and improve the quality of the interaction with the GPT model.

2) Simplification: CUE can help simplify the process of defining and generating JSON inputs and outputs for GPT-JSON. By providing a more concise and expressive syntax, CUE can make it easier to craft structured inputs and outputs, which can be especially helpful when working with complex data structures.

3) Interoperability: Integrating CUE can improve the interoperability between GPT-JSON and other systems or applications. With CUE's ability to work with multiple data formats (such as JSON, YAML, or HCL), it becomes easier to integrate GPT-JSON with various tools and platforms.

To integrate CUE with GPT-JSON, you would need to follow these general steps:

1) Define CUE schemas for the input and output JSON structures used by GPT-JSON.

2) Use the CUE language to validate and generate input JSON for GPT-JSON.

3) Process the output JSON generated by GPT-JSON using CUE to ensure it adheres to the defined schema and transform it as needed.

Integrating CUE with GPT-JSON can improve the overall robustness and ease of use of the library, making it more accessible and useful for a wider range of applications."

Re: Show HN: GPT-JSON – Structured and typehinted GPT responses in Python

#24
You can use simpler types. Instead of :

        messages=[
            GPTMessage(
                role=GPTMessageRole.SYSTEM,
                content=SYSTEM_PROMPT,
            ),
            GPTMessage(
                role=GPTMessageRole.USER,
                content="Text: I love this product. It's the best thing ever!",
            )
        ]
Try:

        messages=(
            ("system", SYSTEM_PROMPT),
            ("user", "Text: I love this product. It's the best thing ever!")
        )
Or:

        messages=(
            SystemMsg(SYSTEM_PROMPT),
            UserMsg("Text: I love this product. It's the best thing ever!")
        )
This is still Python, not Java.

Re: Show HN: GPT-JSON – Structured and typehinted GPT responses in Python

#25
post #7

Here's 40 lines of python code that I've found to be unreasonably effective at accomplishing something similar: https://github.com/jiggy-ai/pydantic-chatcompletion/blob/mas...

Thanks! I have something similar that I've been using and struggling to keep consistent. I like that this is a relatively small package, I'll probably end up using it to play around with.

Re: Show HN: GPT-JSON – Structured and typehinted GPT responses in Python

#27
post #7

Here's 40 lines of python code that I've found to be unreasonably effective at accomplishing something similar: https://github.com/jiggy-ai/pydantic-chatcompletion/blob/mas...

Very cool. Out of curiosity, for the retries why are the errors appended as system messages as opposed to appending to the user message? And in either case, would it help to prepend the error with something like “Be sure to avoid outputting something that would cause this error:”?

Re: Show HN: GPT-JSON – Structured and typehinted GPT responses in Python

#28

I like the idea, but I think a library that focuses on producing requests and parsing responses according to schema is better. Sending requests to the server is orthogonal to the purpose. What we've found useful in practice in dealing with similar problems: - Use json5 instead of json when parsing. It allows trailing commas. - Don't let it respond in true/false. Instead, ask it for a short sentence explaining whether…

> Don't let it respond in true/false. Instead, ask it for a short sentence explaining whether it is true or false. Afterwards, use a small embedding model such as sbert to extract true/false from the sentence. We've found that GPT is able to reason better in this case, and it is much more robust.

Have you tried just getting it to do both? It reasons far better given some space to think, so I often have it explain things first then give the answer. You're effectively then using gpt for the extraction too.

This hugely improved the class hierarchies it was creating for me, significantly improving the reuse of classes and using better classes for fields too.

Re: Show HN: GPT-JSON – Structured and typehinted GPT responses in Python

#30

You can use simpler types. Instead of : messages=[ GPTMessage( role=GPTMessageRole.SYSTEM, content=SYSTEM_PROMPT, ), GPTMessage( role=GPTMessageRole.USER, content="Text: I love this product. It's the best thing ever!", ) ] Try: messages=( ("system", SYSTEM_PROMPT), ("user", "Text: I love this product. It's the best thing ever!") ) Or: messages=( SystemMsg(SYSTEM_PROMPT), UserMsg("Text: I love this product. It's the b…

python newbie here, why did messages change from [ to (
Post reply on HN