Hi I am using the sample ‘Chat with OpenAI Assistant (by adding a memory)’. I have added a MYSQL node and insert the data from the chat into various fields.
The results contains (usually) the HTML tables and some comment before and after these.
Using chatgpt it gave me some javascript code to get extract just the tables.
const content = $input.item.json.content; // This assumes 'content' field contains the full HTML
// Extract the part between <table> and </table> tags
const tableContent = content.match(/<table[\s\S]*?<\/table>/g).join("\n");
// Return the result to be posted into your MySQL node
return {
json: {
tableContent: tableContent
}
};
My question is what node do I use to be able to use this code and how please ?
// Access the output of the "Chat Memory Manager1" node
const nodeOutput = $('Chat Memory Manager1');
// Access the content from the first object in the array (HumanMessage)
const humanMessageContent = nodeOutput.json[0]?.kwargs?.content || "No HumanMessage content found";
// Return the extracted content
return {
json: {
humanMessageContent: humanMessageContent
}
};
But I get “No HumanMessage content found”;
I see there is content so it must be a syntax error…
// Access the output of the "OpenAI Assistant" node
const nodeOutput = $('OpenAI Assistant').item.json.output; // the chat output
const nodeRunID = $('OpenAI Assistant').item.json.runId;
const nodeThreadID = $('OpenAI Assistant').item.json.threadId;
const nodeContent = $('OpenAI Assistant').item.json;
//const nodeQuestion1 = $('Chat Memory Manager1').last().json.kwargs.content;
let nodeQuestion1 = "No question found"; // Default if not found
const chatMemoryItems = $('Chat Memory Manager1').all(); // Get all items from Chat Memory Manager
if (Array.isArray(chatMemoryItems) && chatMemoryItems.length > 0) {
// Iterate through the items to find the first instance of `kwargs.content`
for (let i = 0; i < chatMemoryItems.length; i++) {
if (chatMemoryItems[i].json.kwargs && chatMemoryItems[i].json.kwargs.content) {
nodeQuestion1 = chatMemoryItems[i].json.kwargs.content; // Assign the first occurrence
break; // Exit loop after finding the first match
}
}
}
// Check if the output contains any markdown tables (formatted with pipes "|")
let tableReplies = "No tables found";
const markdownTableRegex = /\|(?:.*\|)+/g; // Simple regex to match markdown table rows
const matches = nodeOutput.match(markdownTableRegex);
if (matches && matches.length > 0) {
tableReplies = matches.join("\n"); // Extract and join the tables if they exist
}
// Return the result to be posted into your MySQL node
return {
json: {
replies: tableReplies,
runID: nodeRunID,
question: nodeQuestion1,
threadID: nodeThreadID,
content: nodeContent
}
};
I see the test results okay.
When I run it the code node just stays in a loop.
How do I trace this ?
Thanks for sharing so much detail!!
Would you be able to post the workflow as well so I can test it out?
You can do it by following these steps - Export and import workflows | n8n Docs - and pasting it between the </>