Got OpenAI Completions API to return JSON, how can I transform it to be read as a list

Options

I created some prompts in OpenAI's APIs that respond back with validated JSON.

However, since it is in the response as a text string, I cannot figure out how to get Xano to recognize it as JSON (and then enable me to work with it as such).

Example Output at response.result.choices.text:

[ { "keyword": "cute", "sentiment": "positive" }, { "keyword": "sound", "sentiment": "positive" }, { "keyword": "stay tuned", "sentiment": "negative" }, { "keyword": "well made", "sentiment": "positive" }, { "keyword": "looks great", "sentiment": "positive" }, { "keyword": "multi-hundred dollar instrument", "sentiment": "neutral" }, { "keyword": "good deal", "sentiment": "positive" } ]

What transformations (or other functions) to get Xano to recognize this at JSON?

Tagged:

Answers

  • jeffbuze
    jeffbuze Member
    Options

    My current cURL for creating this:
    curl https://api.openai.com/v1/completions -H "Content-Type: application/json" -H "Authorization: Bearer $OPENAI_API_KEY" -d '{
    "model": "text-davinci-003",
    "prompt": "Extract keywords from this text:\n\nThis is such a cute little instrument, my daughter loves it and the sound is good. The only downside is that it doesn't stay tuned for very long. We always have to go and re-tune it, which can be frustrating. But otherwise it seems well made, it looks great and when its tunes, the sound is good. Its not multi-hundred dollar instrument, but for kids and beginners its a good deal.\t\n\nFor each keyword, determine whether it is positive, negative or neutral in the context of the review. Format the text in the following JSON format: \n[\n \t{\n\t\t"keyword": "location",\n\t\t"sentiment": "positive"\n\t},\n\t{\n\t\t"keyword": "",\n\t\t"sentiment": ""\n\t}\n]",
    "temperature": 0,
    "max_tokens": 1000,
    "top_p": 1,
    "frequency_penalty": 0,
    "presence_penalty": 0
    }'

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    Xano has a filter called "json_decode" you can apply to your result from OpenAI that will generate the object you want to work with.

    So after your external api request,
    1) Add a "create variable" function (from data manipulation)
    2) Set the api_1.response.result (or whatever) as your input, and apply the "json_decode" filter to it.
    3) Add a stop-and-debug after that line to confirm it creates the object structure you're looking for!

    OpenAI integration can be a little tricky because the potential for variation in the results. AI integration is in the list of hardest 5% topics we work on overcoming in our daily office hours and forum at State Change Pro.

  • jeffbuze
    jeffbuze Member
    Options

    Thanks so much for the response, Ray! I had tried the json_decode, but wasn't having much luck.

    I think I have it giving results without variation (after turning the temperature down to 0).

    The error I get is: "Exception: json_decode(): Argument #1 ($json) must be of type string, array given"

    I'll play around more with formatting of the JSON. Is there a way to manually adjust the input text to the create variable to see what would work during debugging?

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    I was guessing a bit as to the shape of the result. A stop-and-debug after your external API call sharing what came back might clarify what you got from OpenAI and which piece of the result you want to parse.

  • jeffbuze
    jeffbuze Member
    edited April 2023
    Options

    Thanks again Ray. I'll play around with it. The response I got back is what I included in the initial message.

    [ { "keyword": "cute", "sentiment": "positive" }, { "keyword": "sound", "sentiment": "positive" }, { "keyword": "stay tuned", "sentiment": "negative" }, { "keyword": "well made", "sentiment": "positive" }, { "keyword": "looks great", "sentiment": "positive" }, { "keyword": "multi-hundred dollar instrument", "sentiment": "neutral" }, { "keyword": "good deal", "sentiment": "positive" } ]

    It's formatted as a JSON array, so I was hoping there was a way to just transform it directly into a JSON array by recognizing that it was JSON. Maybe that's not possible and instead I need to break it into each item first, decode to JSON, then add to an array one item at a time.

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    Huh! Can you show (screenshot or loom) where (presumably in a "create variable" call) you use the json_decode filter? devil could be in the detail of exactly how that gets called.

  • jeffbuze
    jeffbuze Member
    Options

    I figured out what I had done wrong! 🤦‍♂️ One of sub-paths (choices) was an array, even though with just one item.

    So I was putting this:

    When I should have been putting this:

    Thanks for asking about the way the variable was called!

    Now it works and is returning the JSON correctly.