I’m trying to figure out how to get Bubble to properly parse a response from OpenAI’s GPT API. Normally in Bubble a JSON response is automatically parsed and you can assign response contents to data types (things). In the GPT API, however, it lumps it as a single string.
I tried ChatGPT to help solve this and this is what it says:
The response you are receiving is a properly formatted JSON response from the OpenAI API. The response has an object called “body” that contains several fields, including “id”, “object”, “created”, “model”, and “choices”. The “choices” field is an array that contains a single object that represents the completed prompt.
The object within the “choices” array has several fields, including “text”, “index”, “logprobs”, and “finish_reason”. The “text” field contains the generated text response from the API in the form of a JSON string.
To convert the JSON string into a structured JSON object, you can use a JSON parser. The exact method for parsing the JSON will depend on the programming language you are using. Most modern programming languages have built-in JSON parsing libraries or modules that can be used to parse JSON strings into structured objects.
Anyone have an idea how to best parse the string and convert it to a structured JSON object to be able to save to the database?
As an update for anyone who is having similar issues, I was able to solve my issue using the following method.
Used regex to extract the JSON portion of the response string from OpenAI API (resulting in just the JSON, stripping away everything else).
Setup pythonanywhere web app with POST and GET api endpoints
In my bubble workflow, after receiving the OpenAI API response, cleaning it with regex, then sent the JSON payload to pythonanywhere endpoint. PA receives the JSON, stores it in a db, then my bubble workflow sends a GET to PA to return the structured JSON that bubble will properly recognize so I can work with it in bubble.
My python web app is also doing some manipulation of the data that is easier to do there. A real pain, probably not the absolute best way to do it, but it works.
not great but i have called openai type api’s returning ( JSON.toString(response)) directly from javascript widget then, just use the javascript itself to parse && string process the json before calling the bubble wrapper to return from js to the UI runtimes next
By doing the above, the simplified string returned to bubble can more easily be handled with regex to make sense of the components of the string expression passed to bubble_fn_Wrapper