Integrate Chat App Q&A with Database via workflow

Hi,

I am a retool newbie and I created a chat app on retool where users can ask questions and using vectors and Ai the chat answers back. This works fine.

But I want to create a database where the Q&A any user asks is recorded. So I was thinking of utilising a workflow to help me get it done, but seem to be having a problem firstly connecting to the app and second making sure the Q&A are captured and populated in a retool database.

Below is an example of me trying and failing to build a workflow.

Also FYI the chat app I am trying to connect with:

Annotation 2024-07-08 151335

Can someone please help me with how I can build this workflow out.

Thanks,

Sally

I see a couple problems in the workflow to start with:
image
when this block runs, the first thing it does is use the URL and request Method to send a REST Request. to do this, it has to first evaluate what the url is. the Resource you've made (TechBot API) sets the base url as ...Bot/.... which is probably something like https://api.openai.com/v1/. the 2nd half the URL, which describes the Endpoint, is provided by reading the value of {{ restQuery.data }}? and at this point in time the workflow block doesn't have the result from sending the GET request so restQuery.data evaluates to null or undefined 100% of the time. instead, you should check the docs for the API you're using and use one of those. for me, I'd use something like assistants or {{ "assistants" }} as the Endpoint, which would result in a GET request being sent to the URL of https://api.openai.com/v1/assistants.

the ? you have in the restQuery is probably not parsing/linting as valid syntax and if not, the result might not be what you expected. if you want to add a ? onto the end of a string I'd suggest either using .concat or the + operator:
{{ myPreviousBlock.data.concat('?') }} or
{{ myPreviousBlock.data + '?' }}

image
the same problem is occurring here. the block named code hasn't ran to completion yet so code and code.data don't exist yet. however, here I think you want to use const chatData = restQuery.data.

image
the last issue I can see right now is in the sqlQuery block. blocks only have 3 properties you can access (.data, .error and .metadata) all of which can only be used AFTER the block completes. this means instead of trying to reference the input directly with $BLOCK_INPUT_3.question try referencing the output of the block that has the value you want:

INSERT INTO chat_records (user_name, question, answer) 
  VALUES ({{ code.data.userData }}, {{ code.data.question }}, {{ code.data.answer }})

Hi @bobthebear thank you very much for your help. I have managed to make it the workflow work after following your instructions, see below:

I added a trigger in the app to link with the workflow, which also seems to work and now there seems to be a link between the workflow and app.

I also see an entry in the database every time the app is used, however I am getting null values in the question and answer columns in the database.


I am guessing the correct values are not being used to record the question and answer between the workflow and app.

Any idea what I need to update to ensure the Q&A are being recorded in the database. Below is the app set up if needed:
app set up

Thanks!

Hi @bobthebear or anyone else,

Any update or insight on my latest query?

Very much appreciated.

Thanks,

for the 3rd block named 'code' try:

return restQuery.data;

if it still doesn't work, question and answer are probably objects, not strings. to find out we can change 'code' to:

console.log("Typeof(restQuery):", typeof restQuery);
console.log("Typeof(restQuery.data):", typeof restQuery.data);
console.log("Typeof(restQuery.data.question):", typeof restQuery.data.question);
return restQuery.data;

don't forget to click 'deploy' in the top right corner before testing from an app or anything outside that workflow. the workflow still won't work, but if you open the browsers dev tools (in chrome its ctrl + shift + i) you should see 4 lines of text somewhere in the console... depending on when you call the workflow and how many queries/REST requests are sent after running it, you might need to scroll up a bit. if those 4 debug lines don't help point you in the right direction or the console shows null for any of those and are no help at all, just let me know what the console output was so i can try and narrow down what's going on

1 Like

Thanks @bobthebear, you helped me figure this out.

:+1:

1 Like