Pull Request Review Time for Forked common.mjs?

LMK your thoughts on this :point_up_2:

To ensure the total length of the JSON-stringified array is no more than 4,000 characters and remove the last element if needed, you can modify the code snippet as follows: export default defineComponent({ props: { userEmail: { type: "string", label: "User Email", }, text: { type: "string", label: "Text", }, db: { type: "data_store", }, }, async run({ $ }) { const datastoreKey = this.userEmail; const newMessage = { role: "user", content: this.text }; // Retrieve the existing array of messages for the given email const existingMessages = await this.db.get(datastoreKey) || []; // Append the new message to the existing array const updatedMessages = [...existingMessages, newMessage]; // Check if the JSON-stringified array length is more than 4,000 characters let jsonString = JSON.stringify(updatedMessages); let truncated = false; while (jsonString.length > 4000) { // Remove the last element of the array updatedMessages.pop(); jsonString = JSON.stringify(updatedMessages); truncated = true; } // Save the updated array back to the data store await this.db.set(datastoreKey, updatedMessages); // Export the "truncated" property $.export("truncated", truncated); // Return the updated (and possibly truncated) array of messages return updatedMessages; }, }); This code checks the length of the JSON-stringified array and removes the last element until the length is under 4,000 characters. It also exports a “truncated” property using $.export, which is set to true if truncation occurred, and false otherwise.

to answer the question in the video: you’ll want to construct the user message in the place in the code where you’re adding the new user message to chat history:

const newMessage = { role: "user", content: this.text };

If you copy the latest code into a new Node.js code step and press Refresh Fields at the top of the code, you’ll see a Text field appear. You can enter the same prompt there, referencing the same variables you were. And you can do the same with system message.

then you can remove user message and system message in the Chat action, and just pass prior message history

basically, we’re just constructing an array of messages of the format that the Chat API expects. And since we’re constructing messages directly, we want to match that format. You can see that system, user, and assistant responses are all included in that example, so that’s what we’re trying to construct.

, I really appreciate your patience with me and the help you have provided. Based on what you are saying, I should scrap the chatgpt step all together and just use a node.js step? AM I understanding that correctly?

You’ll actually want to keep the Chat action. The code above shows you how to construct the array of messages. You’ll return those messages from that first step, and pass that reference to the Prior Message History field in the Chat action

I built some example workflows that show how this works, take a look at Integrate ChatGPT with 1,000 other apps

Thank you! I’ll check it out