Parse GPT-4-turbo Preview

Hello,

I spent a (lot, lot, lot,) lot of time making content with OPENAI before the LLM node came out. Now there is the GPT4 Turbo Model available (GPT4-1106-preview) but the big difference is that the output always has some extra stuff like this:

[
{
"index": 
0,
"message": 
{
"role": 
"assistant",
"content": 
"```json { "id_aid": "", "domaine_ref": "Tourisme", "domaines": [ "Tourisme", "Environnement", "Energie" ] } ```"
},
"logprobs": 
null,
"finish_reason": 
"stop"
}
]

I could not manage to solve it using the parser functionality of the LLM node so here are the code nodes I use to have a proper JSON or a proper markdown.

JSON cleaner and parser for GPT4 Turbo:

const content = items[0].json.message.content;
let jsonStringStart = content.indexOf('{');
let jsonStringEnd = content.lastIndexOf('}') + 1; // include the } character in the substring
let jsonString = content.substring(jsonStringStart, jsonStringEnd);
let jsonData = JSON.parse(jsonString);
return [{json: jsonData}];

Markdown cleaner and parser for GPT4 Turbo:

let processedMarkdown = '';
try {
  const content = items[0].json.message.content;
  
  // Removing back-ticks and "markdown" text from content
  let cleanContent = content.replace(/\`\`\`markdown\s?|\\n/g, '');
  cleanContent = cleanContent.replace(/\`\`\`/g, ''); // removing ending back-ticks
  
  // Replacing double line breaks with a single one
  processedMarkdown = cleanContent.replace(/\n\n/g, '\n');
  
  // Removing quotes
  processedMarkdown = processedMarkdown.replace(/^"|"$/g, '');
} catch (error) {
  processedMarkdown = 'markdownerror';
}

return [{"markdown": processedMarkdown}];

It’s probably obvious to devs, but since I’m not a dev, these snippets where very useful to me.

Have a great day,
Joachim

1 Like

If you’re using the LLM node instead of the OPENAI node, you will need to replace json.message.content with json.text.

1 Like