DEV Community

Cover image for Build an AI Personality Analyzer with Groq and Node.js 🀩
Harshit Rawat
Harshit Rawat

Posted on

Build an AI Personality Analyzer with Groq and Node.js 🀩

Introduction

Hey everyone, Good to see you back! We're going to build an AI-powered personality analyzer using the Groq SDK with Node.js. The app will analyze user responses to fun questions and match them with characters from Stranger Things. Why Stranger Things? Because it's awesome!

We'll be using one of the models given by Groq to build this application. The main reason behind using Groq is its high RPM (Requests Per Minute), making it ideal for building fast and scalable applications.

This project is ideal for beginners who want to explore how AI can be used to create fun and interactive applications. However, even experienced developers will enjoy the process, and your suggestions are always welcome!

intro

Setting up the Project

  1. To setup the project start with first initializing nodejs into the project. Create a directory "aiapp".

  2. Type the command below to initialize a npm project.

npm init y
Enter fullscreen mode Exit fullscreen mode

Dependencies setup

To make this project we need few dependencies to install. Type the below command onto your terminal.

npm install dotenv express groq-sdk

Enter fullscreen mode Exit fullscreen mode

Here's how package.json should look like:

Don't forget to add the script : start: node app.js

pkgjson

Here we need express to setup the server for our nodejs project, followed by dotenv to load the values (Api keys) from our .env file and finally the groq-sdk package provided by groq that will be used to analyze the personalities.

  • Inside your projects directory , create a file named .env. Add the below code inside the file.
GROQ_API_KEY= your api key  #generated by groq
Enter fullscreen mode Exit fullscreen mode

Where can you get the Api key from?

Visit the official website of groq here, sign up for a free account and visit the api keys section.
Here, click generate api key and copy the key generated , create a env file in the project's root and paste the key in your env file.

apikeys

Let's configure the server
Follow the below folder structure for setting up the project.

folderstucture

The files inside the folders can be accessed here. Make sure to star the project :)

Create a file app.js and write the following code inside it (The whole code can be accessed here)

const express = require('express');
const path = require('path');
require('dotenv').config();

const questionsRoutes = require('./routes/questions');
const quizRoutes = require('./routes/quiz');

const app = express();
const port = 3000;

app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/questions', questionsRoutes);
app.use('/quiz', quizRoutes);

app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'public', 'index.html'));
});

app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
});

Enter fullscreen mode Exit fullscreen mode

I have added another file, script.js in order to fetch the questions, This allows for dynamic loading of questions without requiring a full page refresh.

Now, we can Start the server, run the below command.

npm start
Enter fullscreen mode Exit fullscreen mode

serverimg

I have created some questions for users to answer and match with a stranger things personality. Let's check if we can access them, visit any of the api testing platforms, I'm using Thunder Client.

questions

It works!!

hopper

Setting up Groq and prompts

Now that we have created the server let's now setup groq and feed in some data inside the utils/analysis.js that can be used further.

const { Groq } = require('groq-sdk');

const groq = new Groq({ apiKey: process.env.GROQ_API_KEY });

const characterDescriptions = { "This data can be accessed from the below link" };

async function analyzePersonality(answers, questions) {
  const prompt = " This data can be accesses from the below link"

  try {
    const response = await groq.chat.completions.create({
      model: "llama3-8b-8192",
      messages: [{ role: "user", content: prompt }],
    });

    return response?.choices[0]?.message?.content || "Error analyzing personality.";
  } catch (error) {
    console.error("Error analyzing personality:", error);
    return "Error analyzing personality.";
  }
}

module.exports = { analyzePersonality };
Enter fullscreen mode Exit fullscreen mode

Use the link, for accessing the Character details and prompt.

The above code uses the Groq SDK to define a function analyzePersonality that analyzes personality traits based on user input by interacting with the Groq API. Here I'm using the llama3-8b-8192 model, which is part of the LLaMA family and perfectly balanced generating human-like text in applications like personality analyzer. The model's size and capabilities allow it to understand provide relevant responses efficiently, making it a practical choice compared to smaller or more resource-intensive alternatives.

Let's now check the functionality of our application, run the server visit http://localhost:3000 to see the live application.

Testing

Let's now test the app if its working as expected and check what character will I receive.

npm start
Enter fullscreen mode Exit fullscreen mode

and visit localhost:3000 and answer all the questions.

Lucas

Ohhh Nice! I got Lucas, such a practical and trustworthy Character, I'm excited to see what character you get.

Conclusion

With this we have successfully integrated AI inside our app using Groq, this can be further used in creating many such fun projects. Thankyou If you made it to last.
I hope you found the content enjoyable and inspiring for your own projects! If you have any suggestions for improvements or enhancements, please don’t hesitate to share your thoughts in the comments.

Also, consider sharing this with anyone interested in creating exciting projects using AI!

For paid collaborations mail me at: harshit77dev@gmail.com
Feel free to reach out to me on Twitter, LinkedIn, Youtube, Github.
Thankyou Once Again!!

Thankyou

Top comments (1)

Collapse
 
shubh_shekhar_dbeb5c2df19 profile image
shubh shekhar

I will surely try this.