visit
Yes, nice comments are awesome. But the not-so-nice comments are important, too. And they’re the ones you need to respond to the fastest.
An ideal system would work something like the one below.
For this project, we’re using a platform called MindsDB—the platform for building custom AI. It basically connects real-time data—like tons of YouTube comments— to AI models. It lets companies prototype and deploy AI-powered enterprise solutions quickly and at scale.
In this case, it acts as an orchestration layer where we bring everything together, using AI to analyze the sentiment in the comments. Today, we’ll be using models from OpenAI, which makes ChatGPT. Then we’ll set up some MindsDB automation and alerts—those will come from Slack—to help with instant responses. Here's what the workflow will look like in the end.
CREATE DATABASE mindsdb_youtube_oauth
WITH ENGINE = 'youtube',
PARAMETERS = {
"credentials_file": "/path-to-creds-file/credentials.json"
-- alternatively, use the credentials_url parameter
};
CREATE ML_ENGINE openai_engine
FROM openai
USING
api_key = 'sk-xxx';
CREATE MODEL yt_sentiment_classifier
PREDICT sentiment
USING
engine = 'openai_engine',
model_name = 'gpt-4',
prompt_template = 'describe the sentiment of the comments
strictly as "positive", "spam", or "negative".
"I love this video":positive
"It is not a helpful content":negative
"{{comment}}.":';
SELECT c.comment, m.sentiment
FROM mindsdb_youtube_oauth.comments AS c
JOIN yt_sentiment_classifier AS m
WHERE c.channel_id="UC5_wBOLCWath6q1iTgPPD5A";
comment |
sentiment |
---|---|
This video explains the subject so well. | positive |
Subscribe to this channel asap. | spam |
It didn’t help me understand the subject. | negative |
CREATE MODEL yt_reply_model
PREDICT reply
USING
engine = 'openai_engine',
model_name = 'gpt-4',
prompt_template = 'briefly respond to the youtube comment "{{comment}}";
as a context, use the video transcript "{{transcript}}"';
(Before executing the below command, add at least one new comment to the channel, so that it can fulfill the LAST keyword condition.)
SELECT c.comment AS comment,
m1.sentiment AS sentiment,
m2.reply AS reply
FROM mindsdb_youtube_oauth.comments c
LEFT JOIN mindsdb_youtube_oauth.videos v
ON c.video_id = v.video_id
JOIN yt_sentiment_classifier AS m1
JOIN yt_reply_model AS m2
WHERE c.channel_id="UC5_wBOLCWath6q1iTgPPD5A"
AND v.channel_id="UC5_wBOLCWath6q1iTgPPD5A"
AND c.published_at > LAST;
Please note that for long videos the transcript will be long and that may result in the OpenAI token limit being exceeded. Alternatively, to pass long transcript values in the prompt to the model, you can use the feature of MindsDB.
comment |
sentiment |
reply |
---|---|---|
This video explains the subject so well. | positive | Thank you for your comment. We’re glad you enjoyed the video. |
It didn’t help me understand the subject. | negative | Thank you for your comment. Please let us know your feedback on what could be improved. |
(Before executing the below command, add at least one new (positive) comment to the channel, so that it can fulfill the LAST keyword condition.)
INSERT INTO mindsdb_youtube_oauth.comments (comment_id, reply)
SELECT c.comment_id AS comment_id,
m2.reply AS reply
FROM mindsdb_youtube_oauth.comments c
LEFT JOIN mindsdb_youtube_oauth.videos v
ON c.video_id = v.video_id
JOIN yt_sentiment_classifier AS m1
JOIN yt_reply_model AS m2
WHERE c.channel_id="UC5_wBOLCWath6q1iTgPPD5A"
AND v.channel_id="UC5_wBOLCWath6q1iTgPPD5A"
AND c.published_at > LAST
AND m1.sentiment = 'positive';
CREATE DATABASE yt_slack
WITH
ENGINE = 'slack',
PARAMETERS = {
"token": "xoxb-xxx"
};
INSERT INTO yt_slack.channels (channel, text)
VALUES("channel-name", "Test message.");
(Before executing the below command, add at least one new (spam) comment to the channel, so that it can fulfill the LAST keyword condition.)
INSERT INTO yt_slack.channels (channel, text)
SELECT "channel-name" AS channel,
concat('Video: //www.youtube.com/watch?v=', c.video_id, chr(10),
'Spam Comment: //www.youtube.com/watch?v=', c.video_id,
'&lc=', c.comment_id, chr(10),
'Author: ', c.display_name, chr(10),
'Comment: ', c.comment) AS text
FROM mindsdb_youtube_oauth.comments c
LEFT JOIN mindsdb_youtube_oauth.videos v
ON c.video_id = v.video_id
JOIN yt_sentiment_classifier AS m
WHERE c.channel_id="UC5_wBOLCWath6q1iTgPPD5A"
AND v.channel_id="UC5_wBOLCWath6q1iTgPPD5A"
AND c.published_at > LAST
AND m.sentiment = 'spam';
CREATE DATABASE yt_slack
WITH
ENGINE = 'slack',
PARAMETERS = {
"token": "xoxb-xxx"
};
INSERT INTO yt_slack.channels (channel, text)
VALUES("channel-name", "Test message.");
(Before executing the below command, add at least one new (negative) comment to the channel, so that it can fulfill the LAST keyword condition.)
INSERT INTO yt_slack.channels (channel, text)
SELECT "channel-name" AS channel,
concat('Video: //www.youtube.com/watch?v=', c.video_id, chr(10),
'Negative Comment: //www.youtube.com/watch?v=', c.video_id,
'&lc=', c.comment_id, chr(10),
'Author: ', c.display_name, chr(10),
'Comment: ', c.comment, chr(10),
'Sample reply: ', m2.reply) AS text
FROM mindsdb_youtube_oauth.comments c
LEFT JOIN mindsdb_youtube_oauth.videos v
ON c.video_id = v.video_id
JOIN yt_sentiment_classifier AS m
JOIN yt_reply_model AS m2
WHERE c.channel_id="UC5_wBOLCWath6q1iTgPPD5A"
AND v.channel_id="UC5_wBOLCWath6q1iTgPPD5A"
AND c.published_at > LAST
AND m.sentiment = 'negative';
(The reason for doing so is that every run of a query that includes the condition with the LAST keyword returns only the newly added comments, which haven’t been returned in the previous run of such a query. To be able to do different actions for positive and negative comments, we need to save the output of the query with LAST. To save a batch of recently added comments returned by the query with LAST, we need a database table. Learn more about the .)
You can connect a database to MindsDB using the CREATE DATABASE statement and providing specific parameters. Choose one of the on how to connect it to MindsDB.
CREATE DATABASE psql_datasource
WITH ENGINE = 'postgres',
PARAMETERS = {
"host": "host-name-or-ip-address",
"port": 5432,
"database": "database_name",
"schema": "schema_name",
"user": "postgres",
"password": "password"
};
CREATE OR REPLACE TABLE psql_datasource.recent_comments (
SELECT *
FROM mindsdb_youtube_oauth.comments c
LEFT JOIN mindsdb_youtube_oauth.videos v
ON c.video_id = v.video_id
WHERE c.channel_id="UC5_wBOLCWath6q1iTgPPD5A"
AND v.channel_id="UC5_wBOLCWath6q1iTgPPD5A"
AND c.published_at > LAST
);
CREATE JOB youtube_chatbot (
-- save the recently added comments into a table
CREATE OR REPLACE TABLE psql_datasource.recent_comments (
SELECT *
FROM mindsdb_youtube_oauth.comments c
LEFT JOIN mindsdb_youtube_oauth.videos v
ON c.video_id = v.video_id
WHERE c.channel_id="UC5_wBOLCWath6q1iTgPPD5A"
AND v.channel_id="UC5_wBOLCWath6q1iTgPPD5A"
AND c.published_at > LAST
);
-- automate replies for positive comments (from Step 4)
INSERT INTO mindsdb_youtube_oauth.comments (comment_id, reply)
SELECT c.comment_id AS comment_id,
m2.reply AS reply
FROM psql_datasource.recent_comments AS c
JOIN yt_sentiment_classifier AS m
JOIN yt_reply_model AS m2
WHERE m.sentiment = 'positive';
-- automate notifications about spam comments (from Step 5)
INSERT INTO yt_slack.channels (channel, text)
SELECT "channel-name" AS channel,
concat('Video: //www.youtube.com/watch?v=', c.video_id, chr(10),
'Spam Comment: //www.youtube.com/watch?v=', c.video_id,
'&lc=', c.comment_id, chr(10),
'Author: ', c.display_name, chr(10),
'Comment: ', c.comment) AS text
FROM psql_datasource.recent_comments AS c
JOIN yt_sentiment_classifier AS m
WHERE m.sentiment = 'spam';
-- automate notifications about negative comments (from Step 6)
INSERT INTO yt_slack.channels (channel, text)
SELECT "channel-name" AS channel,
concat('Video: //www.youtube.com/watch?v=', c.video_id, chr(10),
'Negative Comment: //www.youtube.com/watch?v=', c.video_id,
'&lc=', c.comment_id, chr(10),
'Author: ', c.display_name, chr(10),
'Comment: ', c.comment, chr(10),
'Sample reply: ', m2.reply) AS text
FROM psql_datasource.recent_comments AS c
JOIN yt_sentiment_classifier AS m
JOIN yt_reply_model AS m2
WHERE m.sentiment = 'negative';
)
EVERY 10 minutes;
Please note that for large workloads of comments data, it may be required to increase the .
Also published .