Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
kachiever
Participant
Welcome to the Third Episode of the Series: Hello World OpenAI: Crafting Accurate ChatGPT-Like Custom Search for SAPUI5 Application. Till now we installed IDE, set up the environment, created a Secret Key for our OpenAI Account & tested the same using Postman. So, in this episode, we will use our OpenAI Secret key and create a NodeJS API Service that will help us to create our knowledge base and insert the training data. And, finally use the same Search Domain whenever we do any Search operations.


You can check all the existing & upcoming blog posts of this series via the introduction blog post available below link-

Click here

Prerequisites



  • I am assuming that you have a Trial Account Setup for OpenAI and have created your Secret Key in the previous episode.

  • You have NodeJS & VS Code installed as shared in an earlier episode.


Create NodeJS Project


Step 1: Open VS Code, click on Terminal, and choose New Terminal.


Step 2: You will get the Terminal with the Current Path / Location. You can choose a different Path/Location. I will continue with this path.


Step 3: Copy and paste the following command into the terminal and press enter. This will create a folder for our project.
mkdir openaiapi

You will get an output like below:


Step 4: Copy and paste the below command to navigate to the created folder.
cd openaiapi


Step 5: Copy and paste the below command to initiate.
npm init

Enter the following details, type yes at last & press enter.
package name: (openaiapi) openaiapi
version: (1.0.0) 1.0.0
description: OpenAI based NodeJs API
entry point: (index.js) server.js
test command: node server
git repository:
keywords:
author: Kapil Verma [Put your name offcourse]


Step 6: Click on the Explorer icon, then Open Folder Button, click on Created folder, and click on Select Folder.


Step 7: The folder will be opened, Now click on the package to verify the details. We are ready to start actual coding now.



Time to Code


Step 1: Click on the Create new file button & type server.js to create our js file.


Step 2: Copy and paste these required Libs in the Server.js file.
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
var similarity = require( 'compute-cosine-similarity' );
const basicAuth = require('express-basic-auth');
var cors = require('cors');
const axios = require('axios');
const PORT = 3000;
const token = 'sk-XXXXXXXXXXXXXX|Your Secret KEy|XXXXXXXXXXXXXXXXXXXX';

Should look like this :


Step 4: Now create a New File data.json like we created Server.js. We will be using it to store our Trained Model locally.


Step 5: Now let's quickly add this, our JSON Body Parser, CORS Handler & Welcome Page to our server.js
Note: We are using Basic Auth here with Username: admin & Password: oursecret

You can copy & paste the below code :
// Data File
const dataFilePath = 'data.json';

// Middleware to parse incoming JSON data & CORS Handler
app.use(bodyParser.json());
app.use(cors())

//Adding Auth
app.use(basicAuth({
users: { 'admin': 'oursecret' }
}))

// Welcome Page
app.get('/', (req, res) => {
res.send('<p>Welcome to OPEN API Gateway</p><p>Service is Up & Running </p><p></p>' + Date());
})

Step 6: Now let's add the code to start the server.
// Start the server
app.listen(PORT, () => {
console.log(`OpenAI API is running on port ${PORT}`)
})

The Final code should look like this
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const app = express();
var similarity = require( 'compute-cosine-similarity' );
const basicAuth = require('express-basic-auth');
var cors = require('cors');
const axios = require('axios');
const PORT = 3000;
const token = 'sk-XXXXXXXXXXXXXX|Your Secret KEy|XXXXXXXXXXXXXXXXXXXX';

// Data File
const dataFilePath = 'data.json';

// Middleware to parse incoming JSON data & Disable CORS
app.use(bodyParser.json());
app.use(cors())

//Adding Auth
app.use(basicAuth({
users: { 'admin': 'supersecret' }
}))

// Welcome Page
app.get('/', (req, res) => {
res.send('<p>Welcome to OPEN API Gateway</p><p>Service is Up & Running </p><p></p>' + Date());
})

// Start the server
app.listen(PORT, () => {
console.log(`OpenAI API is running on port ${PORT}`)
})

Congratulations, you have successfully created a NodeJs-based API Service for BTP-CF that has basic Authentication.

Start the Server/API


Let's, test our service locally with Postman.

Step 1: Let's first install all the Libraries/Packages. Copy and paste the below command in the terminal & press enter.
npm i express body-parser compute-cosine-similarity express-basic-auth cors axios

It should look like this :


Step 2: Add a comma and copy-paste the start command in the package.json file.
"start": "node server.js"


Step 3: Now type npm start & press enter, you will screen like below confirming that API is up & running.
npm start


 

 

Congratulations your API is up and running now.

Test The API


Now time to test our Service. We will do it using Postman.

Step 1: Open Postman (Installed in the previous episode) and open our Workspace. Click on the + Button, enter the below URL, and click on Send.

 


You should get 401 Unauthorized Error. Cool, basic Authorization is working fine.

Step 2: Now click on Auth Tab, choose basic auth & enter username & password.


Step 3: Now click on Send and try again.


You will get 200 Status, Click on the Preview Tab, and you will see the Welcome Message with the Server's Time Stamp. Congratulations you have successfully tested the create API via Postman.

Let’s Summarize


We created our NodeJs project from scratch and created an authentication supporting NodeJs-based API Service that can be deployed on BTP Cloud Foundry. Further, we tested the same locally using the Postman Tool. That’s all for this episode, will meet you guys in Episode 4.

Next Episode : Epidosde 4
1 Comment
Labels in this area