Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add js sdk folder to fish-tank #75

Merged
merged 26 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0c25cb0
Add js sdk folder to fish-tank
frankfeng98 Oct 28, 2024
7b4f43f
Update formatting
frankfeng98 Oct 28, 2024
9de8235
Add placeholder files
frankfeng98 Oct 28, 2024
66c82e3
Update naming of the file
frankfeng98 Oct 28, 2024
c9ea375
Add one more example
frankfeng98 Oct 29, 2024
8ee3b01
kate/tf 3913 application collect ecommerce pricing data (#81)
KateZhang98 Oct 29, 2024
380c262
Add js example for list query usage (#83)
thakkerurvish Oct 30, 2024
a03e11a
add save / load authenticated session js sdk example. ran script and…
lozzle Oct 30, 2024
4477e87
Add close-popup files (#76)
SuveenE-TF Oct 30, 2024
384a67a
Close cookie popup js sdk (#77)
aarontantf Oct 30, 2024
2b5f1b4
Add log-into-sites examples (#79)
SuveenE-TF Oct 30, 2024
2bb7af2
Compare Product Prices Example (#78)
jinyangTF Oct 31, 2024
f02afc4
Add headless browser script (#85)
Zechereh Oct 31, 2024
09399d2
Add Getting Started YT script (#90)
colriot Nov 1, 2024
010f559
Add wait for entire page to load example script
Zechereh Nov 1, 2024
2cdd115
Js stealth mode example (#80)
TinyGambe Nov 4, 2024
5f7527c
Add xpath application example (#92)
Zechereh Nov 4, 2024
1dc3197
Added javascript sentiment analysis script (#94)
Zechereh Nov 4, 2024
408ac66
Add wait for entire page to load example script (#95)
Zechereh Nov 4, 2024
ad248ac
submit form js example (#89)
joshkung247 Nov 5, 2024
7eb3413
Add get_by_prompt example
frankfeng98 Nov 5, 2024
ec4bdb6
Merge branch 'add_js_sdk_examples_folder' of github.com:tinyfish-io/f…
frankfeng98 Nov 5, 2024
085e4a7
Fix formatting
frankfeng98 Nov 5, 2024
1a7d682
add interact with existing browser example
frankfeng98 Nov 5, 2024
ce9ad3f
Update interact with existing browser example
frankfeng98 Nov 5, 2024
f024082
address comments & fix examples
frankfeng98 Nov 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added javascript sentiment analysis script (#94)
  • Loading branch information
Zechereh authored Nov 4, 2024
commit 1dc31979dee649ae44dfed1c6d3a2978b3ff5da0
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",
"playwright-dompath": "^0.0.7"
}
Expand Down
Loading