Editing data returned from an AI Assistant before inserting into MYSQL

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 ?

Cheers

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:
  • n8n version: 1.57.0
  • Database (default: SQLite): MYSQL
  • n8n EXECUTIONS_PROCESS setting (default: own, main): ???
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Installed on Render.com
  • Operating system: Windows 10 / Chrome

I have added the code node. But I cannot drag any of the fields into the javascript code.

How do I reference the ‘output’ value in the javascript.
ie

const content = ‘output’ value

when I change to the JSON output I have

Node Chat Memory 1

and the following

[

{

“lc”: 1,

“type”: “constructor”,

“id”: [

“langchain_core”,

“messages”,

“HumanMessage”

],

“kwargs”: {

“content”: “using ladders”,

“additional_kwargs”: {

},

“response_metadata”: {

}

}

},

{

“lc”: 1,

“type”: “constructor”,

“id”: [

“langchain_core”,

“messages”,

“AIMessage”

],

“kwargs”: {

“content”: "Let’s create a

I need the date in

“kwargs”: {

“content”:

so const content = $input.item.json.content;

is

const content = Node Chat Memory 1 / “kwargs”: {“content”:

What is the correct syntax please?

I am getting closer…

// 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…

I have the following working in test mode

// 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 ?



In the executions it says

Workflow execution had an error

Method Map.prototype.get called on incompatible receiver [object Map]

Hi @Steve_Warburton ,

Thanks for sharing so much detail!! :raised_hands:
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 </>

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.