-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added javascript sentiment analysis script
- Loading branch information
Showing
5 changed files
with
287 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Example script: perform sentiment analysis with AgentQL | ||
|
||
This example demonstrates how to perform sentiment analysis on YouTube comments with AgentQL and OpenAI's GPT-3.5 model. | ||
|
||
## Run the script | ||
|
||
- [Install AgentQL SDK](https://docs.agentql.com/javascript-sdk/installation) | ||
- [Install OpenAI SDK](https://www.npmjs.com/package/openai) with the following command: | ||
|
||
```bash | ||
npm install openai | ||
``` | ||
|
||
- Save this Javascript file locally as **perform_sentiment_analysis.js** | ||
- Set your OpenAI API key as an environment variable with the following command: | ||
|
||
```bash | ||
export OPENAI_API_KEY="My API Key" | ||
``` | ||
|
||
- Run the following command from the project's folder: | ||
|
||
```bash | ||
node perform_sentiment_analysis.js | ||
``` | ||
|
||
## Play with the query | ||
|
||
Install the [AgentQL Debugger Chrome extension](https://docs.agentql.com/installation/chrome-extension-installation) to play with the AgentQL query. [Learn more about the AgentQL query language](https://docs.agentql.com/agentql-query/query-intro) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* This example demonstrates how to perform sentiment analysis on YouTube comments with AgentQL and OpenAI's GPT-3.5 model. */ | ||
|
||
const { wrap } = require('agentql'); | ||
const { chromium } = require('playwright'); | ||
|
||
// Import the OpenAI API client. | ||
const { OpenAI } = require('openai'); | ||
|
||
// Define the URL of the page to scrape. | ||
const URL = 'https://www.youtube.com/watch?v=JfM1mr2bCuk'; | ||
|
||
// Define a query to interact with the page. | ||
const QUERY = ` | ||
{ | ||
video_title | ||
video_channel | ||
comments[] { | ||
comment_text | ||
author | ||
} | ||
} | ||
`; | ||
|
||
async function getComments() { | ||
// Launch a headless browser using Playwright. | ||
const browser = await chromium.launch({ headless: false }); | ||
|
||
// Create a new page in the browser and wrap it to get access to the AgentQL's querying API | ||
const page = await wrap(await browser.newPage()); | ||
await page.goto(URL); | ||
|
||
for (let i = 0; i < 5; i++) { | ||
// Scroll down the page to load more comments. | ||
await page.waitForPageReadyState(); | ||
|
||
// Scroll down the page to load more comments | ||
await page.keyboard.press('PageDown'); | ||
} | ||
|
||
// Use queryData() method to fetch the video information from the page. | ||
const response = await page.queryData(QUERY); | ||
|
||
// Close the browser | ||
await browser.close(); | ||
|
||
return response; | ||
} | ||
|
||
async function performSentimentAnalysis(comments) { | ||
// User message construction | ||
let USER_MESSAGE = | ||
'These are the comments on the video. I am trying to understand the sentiment of the comments.'; | ||
|
||
// Append each comment's text to USER_MESSAGE | ||
comments.comments.forEach((comment) => { | ||
USER_MESSAGE += comment.comment_text; | ||
}); | ||
|
||
// Define the system message | ||
const SYSTEM_MESSAGE = `You are an expert in understanding social media analytics and specialize in analyzing the sentiment of comments. | ||
Please find the comments on the video as follows: | ||
`; | ||
|
||
// Append request for a summary and takeaways | ||
USER_MESSAGE += | ||
' Could you please provide a summary of the comments on the video. Additionally, just give only 3 takeaways which would be important for me as the creator of the video.'; | ||
|
||
// Initialize OpenAI client | ||
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); | ||
|
||
try { | ||
const response = await client.chat.completions.create({ | ||
model: 'gpt-3.5-turbo', | ||
messages: [ | ||
{ role: 'system', content: SYSTEM_MESSAGE }, | ||
{ role: 'user', content: USER_MESSAGE }, | ||
], | ||
}); | ||
|
||
// Return the content of the first completion choice | ||
return response.choices[0].message.content; | ||
} catch (error) { | ||
console.error('Error during API call:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
async function main() { | ||
const comments = await getComments(); | ||
const summary = await performSentimentAnalysis(comments); | ||
console.log(summary); | ||
} | ||
|
||
main(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
}, | ||
"dependencies": { | ||
"agentql": "^0.0.1", | ||
"openai": "^4.70.1", | ||
"playwright": "^1.48.2" | ||
} | ||
} |