Backend Development 8 min read

Sentiment Analysis of User Comments Using Node.js and OpenAI (AIGC)

This tutorial explains how to combine Node.js with OpenAI's AIGC to build a backend service that reads user comments, sends them to a GPT‑3.5‑turbo model for sentiment classification, and outputs whether the sentiment is positive or negative, complete with setup steps and full source code.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Sentiment Analysis of User Comments Using Node.js and OpenAI (AIGC)

Introduction

In the era of digital transformation, Node.js has become a popular server‑side JavaScript runtime thanks to its non‑blocking I/O model and event‑driven architecture, making it ideal for high‑performance, scalable network applications. At the same time, the rise of AI‑generated content (AIGC) enables intelligent solutions such as sentiment analysis of user comments.

Purpose

The goal is to use Node.js together with AIGC to analyze a user‑provided comment and output whether its sentiment is negative or positive.

Node.js and AIGC: Technical Fusion

Node.js Role

Node.js serves as a backend development star, offering excellent concurrency handling and a JavaScript‑centric environment for quickly building RESTful APIs, handling network requests, and integrating various machine‑learning and AI services, thus providing an efficient, scalable foundation for a sentiment‑analysis system.

AIGC Innovation

AIGC models, especially those trained for sentiment analysis, learn from large annotated datasets to automatically recognize emotional tones such as joy, sadness, anger, or surprise. These models can be called via APIs from a Node.js environment to achieve real‑time sentiment classification.

Implementation Steps

Preparation

Create a new Node.js project and initialize a default package.json file. npm init -y

Install the openai module. npm i openai

Install the dotenv module for loading environment variables from a .env file. npm i dotenv

Obtain an OpenAI API key (e.g., from the provided free‑API‑key portal) and store it securely.

Create a .env file in the project root and add the line: OPENAI_API_KEY=your_api_key

Import Modules and Load Environment Variables

const OpenAI = require('openai');
require('dotenv').config();

Instantiate the OpenAI Client

const client = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY,
    baseURL: 'https://api.chatanywhere.tech/v1'
});

Read User Comment from Standard Input

process.stdin.on('data', (buffer) => {
    const comment = buffer.toString().trim();
    // ... further processing ...
});

Define Sentiment Analysis Function

async function main() {
    let prompt = `
    判断一下用户的评论情感是正面的还是负面的
    评论:我非常喜欢这个产品,它非常实用。
    情感:正面。
    评论:这个产品非常糟糕,我无法使用它。
    情感:负面。
    `;
    let myPrompt = `
    ${prompt}
    评论:${comment}
    情感:
    `;
    const chatCompletion = await client.chat.completions.create({
        messages: [{ role: 'user', content: myPrompt }],
        model: 'gpt-3.5-turbo',
        n: 1
    });
    console.log(chatCompletion.choices[0]);
}

main();

Full Source Code

const OpenAI = require('openai');
require('dotenv').config();

const client = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY,
    baseURL: 'https://api.chatanywhere.tech/v1'
});

process.stdin.on('data', (buffer) => {
    const comment = buffer.toString().trim();
    async function main() {
        let prompt = `
        判断一下用户的评论情感是正面的还是负面的
        评论:我非常喜欢这个产品,它非常实用。
        情感:正面。
        评论:这个产品非常糟糕,我无法使用它。
        情感:负面。
        `;
        let myPrompt = `
        ${prompt}
        评论:${comment}
        情感:
        `;
        const chatCompletion = await client.chat.completions.create({
            messages: [{ role: 'user', content: myPrompt }],
            model: 'gpt-3.5-turbo',
            n: 1
        });
        console.log(chatCompletion.choices[0]);
    }
    main();
});

Result Demonstration

When the input "这个商品很差劲" is provided, the analysis returns a negative sentiment; when the input "遥遥领先" is provided, the analysis returns a positive sentiment.

Conclusion

By combining Node.js with AIGC technology, we can create an efficient sentiment‑analysis solution and open doors to many innovative applications such as personalized recommendations, intelligent customer service, and public‑opinion monitoring. This integration demonstrates how understanding user emotions can drive continuous product and service optimization in an increasingly intelligent digital world.

Backendjavascriptnode.jssentiment analysisOpenAIAIGC
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.