Create a Trigger
Description
Triggers enable users to define event-based actions. For example, if a table is updated, then run a query to update predictions.
Currently, you can create triggers on the following data sources:
- MongoDB (available for MongoDB Atlas Database),
- Slack,
- Solace,
- PostgreSQL (requires write access).
Syntax
Here is the syntax for creating a trigger:
By creating a trigger on a data source, every time this data source is updated or new data is inserted, the sql_code
provided in the statement will be executed.
You can create a trigger either on a table…
…or on one or more columns of a table.
Example
Firstly, connect Slack to MindsDB following this instruction and connect the Slack app to a channel.
Create a model that will be used to answer chat questions every time new messages arrive. Here we use the OpenAI engine, but you can use any other LLM.
Here is how to generate answers to Slack messages using the model:
Let’s analyze this query:
- We select the question from the Slack connection and the answer generated by the model.
- We join the model with the
messages
table. - In the
WHERE
clause:- We provide the channel name where the app/bot is integrated.
- We exclude the messages sent by the app/bot. You can find the user ID of the app/bot by querying the
mindsdb_slack.users
table. - We use the
LAST
keyword to ensure that the model generates answers only to the newly sent messages.
Finally, create a trigger that will insert an answer generated by the model every time when new messages are sent to the channel.
Let’s analyze this statement:
- We create a trigger named
slack_trigger
. - The trigger is created on the
mindsdb_slack.messages
table. Therefore, every time when data is added or updated, the trigger will execute its code. - We provide the code to be executed by the trigger every time the triggering event takes place.
- We insert an answer generated by the model into the
messages
table. - The
TABLE_DELTA
stands for the table on which the trigger has been created. - We exclude the messages sent by the app/bot. You can find the user ID of the app/bot by querying the
mindsdb_slack.users
table.
- We insert an answer generated by the model into the
Was this page helpful?