Giter VIP home page Giter VIP logo

my-cognitive-bot's Introduction

My Cognitive Bot

My Cognitive Bot is an AI-powered chatbot with cognitive capabilities implemented using TensorFlow.js. This bot can learn and respond to user inputs intelligently.

Table of Contents

Installation

To get started with My Cognitive Bot, you need to have Node.js installed on your system. Follow the steps below to set up the project:

  1. Clone the repository:

    git clone https://github.com/vrzaq/my-cognitive-bot.git
    cd my-cognitive-bot
  2. Install dependencies:

    npm install

Usage

To start the bot, run the following command:

npm start

The bot will start running and will be ready to interact with.

Project Structure

The project consists of the following files:

  • index.js: Entry point of the application.
  • CognitiveBot.js: Contains the implementation of the CognitiveBot class.

index.js

import CognitiveBot from './CognitiveBot.js';

// Vocabulary size, embedding size, and hidden size
const vocabSize = 5000;
const embeddingSize = 128;
const hiddenSize = 128;

const bot = new CognitiveBot(vocabSize, embeddingSize, hiddenSize);

// Example training data
const inputTexts = ['hello', 'how are you'];
const targetTexts = ['hi', 'I am fine'];

async function trainBot() {
  await bot.train(inputTexts, targetTexts);
  console.log('Training completed.');
}

trainBot();

async function respondToInput(input) {
  const response = await bot.respond(input);
  console.log('Bot response:', response);
}

// Example interaction
respondToInput('hello');

CognitiveBot.js

import * as tf from '@tensorflow/tfjs';

class CognitiveBot {
  constructor(vocabSize, embeddingSize, hiddenSize) {
    this.vocabSize = vocabSize;
    this.embeddingSize = embeddingSize;
    this.hiddenSize = hiddenSize;

    this.embedding = tf.layers.embedding({ inputDim: vocabSize, outputDim: embeddingSize });
    this.encoder = tf.layers.lstm({ units: hiddenSize, returnState: true });
    this.decoder = tf.layers.lstm({ units: hiddenSize, returnSequences: true, returnState: true });
    this.output = tf.layers.dense({ units: vocabSize, activation: 'softmax' });

    this.optimizer = tf.train.adam();
    this.lossFunction = tf.losses.sparseCategoricalCrossentropy;

    this.conversationHistory = [];
  }

  async encode(inputText) {
    const embeddedInput = this.embedding.apply(inputText);
    const [, stateH, stateC] = await this.encoder.apply(embeddedInput);
    return [stateH, stateC];
  }

  async decode(stateH, stateC, targetText) {
    const embeddedTarget = this.embedding.apply(targetText);
    const [decoderOutputs, , ] = await this.decoder.apply(embeddedTarget, { initialState: [stateH, stateC] });
    const output = await this.output.apply(decoderOutputs);
    return output;
  }

  async train(inputTexts, targetTexts) {
    for (let i = 0; i < inputTexts.length; i++) {
      const inputText = inputTexts[i];
      const targetText = targetTexts[i];

      const gradientFunction = () => {
        const [stateH, stateC] = this.encode(inputText);
        const output = this.decode(stateH, stateC, targetText);
        const loss = this.lossFunction(targetText, output);
        return [loss, this.getTrainableWeights()];
      };

      const grads = await tf.tidy(() => this.optimizer.minimize(gradientFunction));
      this.optimizer.applyGradients(grads);
    }
  }

  async respond(inputText) {
    const [stateH, stateC] = await this.encode(inputText);
    const startToken = tf.tensor2d([this.vocabSize - 1], [1, 1]); // Start token
    let outputText = [startToken];

    while (true) {
      const [output, newStateH, newStateC] = await this.decoder.apply(tf.expand_dims(outputText[outputText.length - 1], 0), { initialState: [stateH, stateC] });
      const predictedToken = tf.argMax(output, -1);
      outputText.push(predictedToken);

      if (predictedToken.dataSync()[0] === this.vocabSize - 2) { // End token
        break;
      }

      [stateH, stateC] = [newStateH, newStateC];
    }

    this.conversationHistory.push([inputText, outputText]);
    return outputText.slice(1, -1).map((token) => this.vocabToText(token.dataSync()[0]));
  }

  vocabToText(token) {
    // Implement this function to convert tokens to text
  }
}

export default CognitiveBot;

Training the Model

The train method of the CognitiveBot class is used to train the bot with the provided input and target texts. You can modify the inputTexts and targetTexts arrays with your training data and call the train function to start training.

const inputTexts = ['hello', 'how are you'];
const targetTexts = ['hi', 'I am fine'];

async function trainBot() {
  await bot.train(inputTexts, targetTexts);
  console.log('Training completed.');
}

trainBot();

Author

This project is developed by Arifi Razzaq.

License

This project is licensed under the MIT License.


Let me know if you need any adjustments or additional sections!

my-cognitive-bot's People

Contributors

vrzaq avatar

Stargazers

Muhamad Fadhlan avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.