I’ve using a Python Dat to make an API call to ChatGPT, then using json.dumps to get the response into a JSON dat. Unfortunately the formatting of the ChatGPT response is causing an error that the object type is not JSON serializable.
I can see that the ChatGPT response does contain a section of properly formatted JSON text within {}, but also some extra non-formatted text at the start and end.
I’m a total novice with Python and JSON, but is it simply a case of removing the superfluous text? Could anyone guide me on this?
can you append a file that has a response in it - might be easier to give you hints seeing the actual data.
Not sure if this is the case, just make sure to sanitize the json from any personal data if any.
if you would be using a Web Client DAT to make the API calls, you could parse the message in the DAT’s callbacks. it looks like currently you get a larger object (‘ChatCompletionMessage’) but still need to get to it’s json content. You also would want to get rid of all the \n newlines…
So, basicly you get a responseObject abck which contains more information then you need, and what you printed is basicly just a representation of this ChatCompletionMessage Object.
From what it looks like the ChatCompletionMessage Object as a “content” attribute, which you can access via a simple dot-access.
So instead of just planting the responseObject in to a dat
op("text1").text = ChatCompletionMessage
try the following
op("text1").text = ChatCompletionMessage.content
Then, the JSON-Parser will take care of all the newLines n stuff. No need to take hand in this.
I tried both options. Simply adding .content to the end was the key.
I really appreciate the help! As this is only one step in a bigger project (where I’ve got a lot to learn) I’ll probably be back with more questions soon.