guidance icon indicating copy to clipboard operation
guidance copied to clipboard

Unable to generate JSON with a chat model

Open dokluch opened this issue 2 years ago • 5 comments

The bug Chat models seemingly can't generate JSON as output.

To Reproduce I am using the same schema as with text-davinci, but wrapping it with the assistant role:

{{#assistant~}}
```json
{
    "title": "{{gen 'title'}}",
}```
{{~/assistant}}

The error I get is: AssertionError: When calling OpenAI chat models you must generate only directly inside the assistant role! The OpenAI API does not currently support partial assistant prompting.

It works without JSON, though.

System info (please complete the following information):

  • OS: macOS
  • Guidance Version: 0.0.61

dokluch avatar Jun 07 '23 02:06 dokluch

facing the same issue with text-davinci as well, it says

"""openAI dose not support creating assert not pattern, "The OpenAI API does not support Guidance pattern controls! Please either switch to an endpoint that does, or don't use the pattern argument to gen." AssertionError: The OpenAI API does not support Guidance pattern controls! Please either switch to an endpoint that does, or don't use the pattern argument to gen."""

Akhilkapoor12 avatar Jun 07 '23 07:06 Akhilkapoor12

This pattern of generation is only supported when using guidance.llms.Transformers

see the notebook

KMontag42 avatar Jun 08 '23 15:06 KMontag42

facing the same issue with text-davinci as well, it says

"""openAI dose not support creating assert not pattern, "The OpenAI API does not support Guidance pattern controls! Please either switch to an endpoint that does, or don't use the pattern argument to gen." AssertionError: The OpenAI API does not support Guidance pattern controls! Please either switch to an endpoint that does, or don't use the pattern argument to gen."""

For this case, I had to remove the pattern argument (as suggested in the error msg) in the handlebar to generate the json output. Seems like we have to rely on OpenAI model to deduce the right pattern for each field.

AlvinNg89 avatar Jun 13 '23 03:06 AlvinNg89

As a workaround, I've followed the prompt from here with a parser function (below) to just get the json object, just in case there are still explanations around the JSON. This seems to work pretty reliably.

def get_json(text):
    pattern = regex.compile(r'\{(?:[^{}]|(?R))*\}')

    return pattern.findall(text)[0]

imlasky avatar Jun 13 '23 15:06 imlasky

Looks like OpenAI released function calling yesterday in their latest gpt-3.5-turbo-0613 which allows native JSON rendering. Here's a good writeup about using it.

imlasky avatar Jun 15 '23 01:06 imlasky