In this Blog, we aim to create a server which will help us to use Gemini without rewriting the code again. You can create your own independent service for Generative AI after completing the Blog.
So, Let's get Started...
Prerequisites:
- Node.js 18+ installed on the device. Node.js — Download (nodejs.org)
Create a Server
We will start by creating a express.js server.
mkdir genai
cd genai
npm init -y
You will see a "package.json" file. This means you have create a npm package.
We are going to use express.js server to serve the Api. You will need to install following packages
npm install express
npm install --save-dev -g nodemon
Now, create index.js with following:
const express=require('express');
const app=express();
const port=8000;
app.listen(port,function(err){
console.log("Server is running on port: ",port);
});
You can test your application by running: `nodemon index.js`
PS C:~/(genai)> nodemon index.js
[nodemon] 3.1.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node index.js`
Server is running on port: 8000
Your Basic Server is Ready!
Getting Gemini Keys
Log in to : Google AI Studio
You can create an API Key here with `Create API in new Project`
Now copy the API key. Create a .env file in the genai project and paste it. This is to avoid the risk of exposing the API Key
API_KEY=A........................a
Creating APIs
Install dependencies using:
npm install @google/generative-ai
npm install dotenv --save
Add this to first line in index.js file.
require('dotenv').config()
.......
Create a New file router/gemini.js
and put this
const express=require('express');
const router=express.Router();
//Post Method :/api/gemini/prompt
router.post('/prompt',(req,res)=>{
});
export default router;
The File Structure:
genai
-->node_modules
-->router
-->gemini.js
-->.env
-->index.js
-->package.json
-->package-lock.json
Now lets write the logic to get the prompt, Generate using Gemini and send the response to the user.
const { GoogleGenerativeAI } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI(process.env.API_KEY);
....
//Post Method :/api/gemini/prompt
router.post('/prompt',async(req,res)=>{
const model = genAI.getGenerativeModel({ model: "gemini-pro"});// You can use other models
const prompt = req.body.prompt;
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
console.log(text);
res.status(200).json({answer:text});
});
.....
Add this to your index.js
for completing the basic API:
//....Initialization of express and app
app.use(express.json()); // For Parsing Json Body in Post Request
const geminiRouter=require('./router/gemini');
app.use('/api/gemini',geminiRouter);
//..... app.listen call
You have successfully completed your own prompt model!
Testing the API
Start your server using nodemon index.js and test the API.
http://127.0.0.1:8000/api/gemini/prompt
Method: POST
Header: content-type:application.json
body:
{
"prompt":"Tell me about Yourself"
}
curl -X POST -H "Content-Type: application/json" -d '{ "prompt":"Tell me about Yourself" }' http://127.0.0.1:8000/api/gemini/prompt
We will learn about creating chats and much more on next Blog. Stay tuned!
GitHub for Reference: Tanmai2002/blog_project_genai_server at blog-1 (github.com)