Send Retool AI query response to Chat when triggering AI from a query

Hi,

I have built an ai agent that receives one of two inputs:

  1. A question with queried data
  2. a follow-up without the data

currently the logic for LLMprompt (input for my retoolAI)

// Check if it's a follow-up prompt
const isFollowUp = IsFollowUp.value;

// Compose prompt
const prompt = isFollowUp
  ? `Follow-up Question: ${chat1.lastMessage}\n\nAnswer the question based only on the prior conversation.`
  : `You will be provided a prompt, Customer Name, datasource(s), and metadata providing descriptions of each column. Follow the prompt exactly as outlined with an emphasis on the format and only using the provided knowledge. Do not hallucinate.

Customer Name: ${customer_selector.selectedLabel}
Prompt to answer: ${chat1.lastMessage}
Data to use:
${JSON.stringify(dataTable.data)}

Column Descriptions for Context:
${MetadataMarkdown.value}
`;

// ✅ Log the prompt to the console for debugging
console.log("📤 Prompt sent to IntelligenceEngine:", prompt);

// Return the prompt
return prompt;

Now my issue is, the only way to get the Retool AI response in chat window is if you trigger the AI directly on send of chat. I need to trigger a script that triggers LLMPrompt to construct the new prompt with chat1.lastmessage and on success triggers retool AI query after.

When I attempt this, my issue is that the response is not sent to Chat1 window.

Anyone have any suggestions?

Hi @Aydan_McNulty,

Thanks for reaching out!

I took some inspiration from this post here to come up with a solution (it's a bit hacky though)

You could trigger a submit message event that posts the llm response as a chat from __retoolAI:

*Please note, simply adding this event caused an endless loop, so you'll want to add some logic to prevent this query from running if the latest message was from __retoolAI. Something like:
{{ llmChat1.data[llmChat1.data.length-1]?.sender =='__retoolAI' }}

There could be better ways to solve this, but hopefully this gives you some ideas :crossed_fingers: I haven't done a ton of testing, so I'd recommend testing against different edge cases to make sure it works as you expect

Given your use case, you might be interested in checking out our new Agents product :slightly_smiling_face: 🚀 Launch Day: Retool Agents is here