Showing posts with label ai agent using javascript. Show all posts
Showing posts with label ai agent using javascript. Show all posts

Tuesday, May 12, 2026

How to create your own AI agent using Javascript


 

You can create your own AI Agent in JavaScript (Node.js) using:

  • OpenAI API
  • Memory
  • Tools/functions
  • Chat interface
Create AI Agent

<script>
require("dotenv").config();

const express = require("express");
const cors = require("cors");
const OpenAI = require("openai");

const app = express();

app.use(cors());
app.use(express.json());

const client = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});

// Simple Tool
function calculator(a, b) {
  return a + b;
}

app.post("/chat", async (req, res) => {

  const userMessage = req.body.message;

  try {

    // Tool Logic
    if(userMessage.includes("add")) {

      const result = calculator(10, 20);

      return res.json({
        reply: "Answer is " + result
      });
    }

    // AI Response
    const response = await client.chat.completions.create({
      model: "gpt-4.1-mini",
      messages: [
        {
          role: "system",
          content: "You are a helpful AI agent."
        },
        {
          role: "user",
          content: userMessage
        }
      ]
    });

    res.json({
      reply: response.choices[0].message.content
    });

  } catch (error) {

    console.log(error);

    res.json({
      reply: "Error occurred"
    });

  }

});

app.listen(3000, () => {
  console.log("Server running on port 3000");
});

</script>

Please comment if want video on that.


๐Ÿค– AI เค”เคฐ PC: เคฌเคฆเคฒเคคी เคฆुเคจिเคฏा เค•ी เคจเคˆ เคคाเค•เคค

 เค†เคœ Artificial Intelligence (AI) เค•ेเคตเคฒ เคเค• เคคเค•เคจीเค• เคจเคนीं, เคฌเคฒ्เค•ि เคถिเค•्เคทा, เคต्เคฏเคตเคธाเคฏ, เคธ्เคตाเคธ्เคฅ्เคฏ, เคธुเคฐเค•्เคทा เค”เคฐ เคถोเคง เค•े เค•्เคทेเคค्เคฐ เคฎें เค•्เคฐांเคคि เคฒा เคฐเคนी เคนै। AI เค”...