DEV Community

Cover image for MINDSDB: Integrate AI/ML models into your applications
Niharika Goulikar
Niharika Goulikar

Posted on

MINDSDB: Integrate AI/ML models into your applications

🌟What is MindsDB?
MindsDB is a platform that simplifies model management and integrates existing AI models into applications with ease.
It covers various aspects of model management including creation, training, fine-tuning, and version control.We can also automate tasks in our AI workflows with MindsDB Jobs, or define and execute specific events with Triggers.

🟒Let's build an application in which we will learn how to integrate mindsDB into our application:-

🌟What is this application all about?
This application leverages MindsDB to deliver intelligent chat assistance and automatically categorize each post into specific categories. Users can view all posts, comment on them, like their favorites, and create new posts. The app uses token-based authentication to ensure secure access and interactions. Additionally, users have the ability to report posts, helping to maintain content quality and compliance with community standards.

πŸ›  TECH STACK:

  • Frontend: NextJs, Tailwind CSS
  • Backend: Next Js
  • Database: Postgres DB

πŸ“€ SETUP

  • Create a .evn file in the root level of your application and add these variables into it:
MINDSDB_KEY=<your api key>
MINDS_URL=<minds url>
BASE_URL=<application url>
Enter fullscreen mode Exit fullscreen mode

πŸš€To get Api key from MindsDB follow these steps:
1.Go to https://mdb.ai/minds and create a new account if you don't have an existing account on MindsDB.
2.Go to the ApiKeys section and generate a new ApiKey and now you are good to go.

Mindsdb ApiKey generation page

  • BASE_URL is your application url which host the frontend (in our case since we are using nextJS, both frontend and backend will be hosted on the same url) and this is just to prevent cors error.You can bypass it by incorporating some workarounds.

  • MINDS_URL We will get this URL from the MindsDB Docker instance that we will start shortly.

☘ Since we are dealing with integration of mindsDB into our application. There are many ways to integrate mindsDB into our application.Few of them are:

  • SQL APIs
  • REST APIs
  • Docker In this example we are going to use MindsDB Docker image to create models. To create mindsDB docker instance run this command:
docker run --name mindsdb_container -p 47334:47334 mindsdb/mindsdb
Enter fullscreen mode Exit fullscreen mode

πŸš€Go the editor router of that docker container url and you will see this(P.S: the image attached has models predefined but you need to create models on your own)

MINDSDB docker image

πŸš€ How to create models?
Creating a model in MindsDB is as easy as writing a prompt(because all you have to do is just write a prompt πŸ˜†).

  • But before creating model we have to create an engine which will run our models.
CREATE ML_ENGINE engine_name
FROM handler_name
USING
  api_key_name = 'your-api-key-value';
Enter fullscreen mode Exit fullscreen mode

Here we are going to use apiKey that we just generated.
πŸ€Creating models

CREATE MODEL model_name
PREDICT column_name
USING
    engine = 'engine_name',
    prompt_template = 'Your prompt here: {{variable}}';
Enter fullscreen mode Exit fullscreen mode

Here,

  • column_name this is the variable which will contain the response for your input.
  • {{variable}} this is your input.
  • 'engine_name' replace this the engine that you create.

πŸ€Writing prompt:-
We can write prompts to accomplish almost any task, though there are some limitations.
With one such prompt being present in the image below:

Prompt example

Now that we have defined our models,we need use these models in our application.We will create Api endpoints, so that our frontend can talk to these models
πŸ€Example of one such endpoint:

import client from "@/db";
import { NextRequest, NextResponse } from "next/server";

interface Body {
  query: string;
}

export async function POST(req: NextRequest) {
  try {
    // Parse request body
    const request: Body = await req.json();

    const response = await fetch(
 `${process.env.MINDS_URL}/api/projects/mindsdb/models/blog_helper/predict`,
      {
        method: "POST",
        headers: {
          'Content-Type': 'application/json',
          'Authorization': `Bearer ${process.env.MINDSDB_KEY}`  // Replace with your actual API key
        },
        body: JSON.stringify({
          data: [{ query: request.query }]
        })
      }
    );

    const newPost = await response.json();

    return NextResponse.json({ response: newPost, status: 200 });
  } catch (error: any) {
    console.error("Error processing request:", error);
    return NextResponse.json({ message: "Internal server error", status: 500 });
  }
}




Enter fullscreen mode Exit fullscreen mode

Now our frontend can talk to these models we have created.

This is the project that I'm referencing to:
BLOGGO

Here a video demonstration of the project:
Loom video link

I hope you enjoyed this short tutorial on MindsDB.Stay tuned for more such interesting and concise tutorials!

Hope to see you all in the next one,

Niharika.

Top comments (9)

Collapse
 
chiragagg5k profile image
Chirag Aggarwal

nice article!

Collapse
 
niharikaa profile image
Niharika Goulikar

Glad you liked it!

Collapse
 
suraj_kumar_79ebbb6e3724f profile image
SuRaj KuMar

Mind Blowing Article......!!!!!!!!!__

Collapse
 
niharikaa profile image
Niharika Goulikar

Thanks buddy!

Collapse
 
harshika_982e868132d9ddba profile image
Harshika

Amazing article!

Collapse
 
akshaya_goulikar_0d04bc39 profile image
Akshaya Goulikar

Wonderful!

Collapse
 
suraj_kumar_79ebbb6e3724f profile image
SuRaj KuMar

Areee Gjb........!!!!!!πŸ”₯πŸ”₯πŸ”₯πŸ”₯

Collapse
 
kevingu profile image
Kevin • Edited
Collapse
 
goulikar_rajesh_9eb772b11 profile image
Rajesh

Helpful!