Skip to content

Commit

Permalink
Added javascript sentiment analysis script
Browse files Browse the repository at this point in the history
  • Loading branch information
Zechereh committed Nov 1, 2024
1 parent 09399d2 commit 344559c
Show file tree
Hide file tree
Showing 5 changed files with 287 additions and 1 deletion.
2 changes: 1 addition & 1 deletion application_examples/perform_sentiment_analysis/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Example script: perform sentiment anaylsis with AgentQL
# 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.

Expand Down
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)
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();
162 changes: 162 additions & 0 deletions javascript-sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions javascript-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"dependencies": {
"agentql": "^0.0.1",
"openai": "^4.70.1",
"playwright": "^1.48.2"
}
}

0 comments on commit 344559c

Please sign in to comment.