How To Integrate Digitalocean Genai Platform Into An Existing Web Application With Digitalocean Cloud Functions

Trending 1 month ago
ARTICLE AD BOX

Introduction

In this tutorial, you will study really to merge DigitalOcean GenAI level into existing web applications. To do so, you will build connected apical of an existing demo exertion built from a Jamstack exertion betterment guide. This existing demo task is simply a fixed HTML that renders contented by invoking DigitalOcean Serverless Functions written successful Node.js to retrieve contented from a MongoDB database.

At nan extremity of this tutorial, you’ll personification a fixed web exertion pinch a chatbot characteristic to query accusation astir various java contented rendered by nan app. The instruction from this tutorial will line leveraging nan DigitalOcean Cloud Functions to merge nan DigitalOcean GenAI level into immoderate web-based application.

Prerequisites

To complete this project, you need:

  • A DigitalOcean unreality account.
  • The Coffee Demo Application. To get nan codebase for nan demo application, you tin fork this repository into a GitHub narration and past clone it to our conception instrumentality aliases you tin recreation this guideline to study really to create specified demo task and deploy it to DigitalOcean App Platform
  • Access to DigitalOcean GenAI platform.
  • The latest type of doctl installed connected your strategy to usage nan DigitalOcean CLI commands.
  • The latest type of nan npm installed connected your system.

Step 1 — Setting Up nan Project

By forking and cloning from nan demo application’s codebase from GitHub, you will announcement you personification 2 branches; The main branch, that has nan complete codification pinch nan GenAI integration and nan Setup branch, pinch nan starting codification that you will build connected apical successful this tutorial to merge nan chatbot feature. The commands beneath will thief america to clone nan task to our conception environment, navigate to nan guidelines of nan project, move to nan Setup branch and instal nan exertion dependencies:

git clone https://github.com/digitalocean/Jamstack-DigitalOcean-Coffee-App.git cd Jamstack-DigitalOcean-Coffee-App git checkout Setup

Open nan task successful a matter editor aliases an IDE, and nan task building should look for illustration this:

alt text

If you forked and cloned nan demo exertion from nan GitHub repository, make judge to create a DigitalOcean-managed Mongodb database and adhd nan sample accusation needed for this exertion to render nan content. This portion of nan line should thief america do that. Once you personification created nan database, you tin configure our application’s business adaptable for nan database entree by creating a .env grounds successful nan app’s directory and copying our database narration drawstring to group that up successful nan .env file. This portion of nan line tin thief you configure nan business variables.

After configuring nan business variables, you are successful a bully position to spot what nan exertion looks like. Let’s commencement by opening nan index.html grounds successful nan web directory successful a browser. And it should look for illustration this:

alt text

Note: Looking astatine nan task building above, if you followed nan line successful creating nan demo application, you would announcement you personification tweaked 1 of nan usability names, getCoffee, to getCoffees. The logic is nan getCoffees function, successful particular, gets each nan disposable coffees successful nan database. Going forward, you will adhd different usability named getCoffee to thief america retrieve accusation astir an individual coffee. So, make nan basal changes successful nan demo exertion you created from nan line to bespeak nan caller changes you want to personification successful this application.

Step 2 — Creating DigitalOcean GenAI Chatbot

Log successful to nan DigitalOcean Cloud Panel successful nan adjacent sidebar, past click nan GenAI Platform tab. The GenAI dashboard page will look for illustration this:

Create Agent

Under nan Agent tab of this page, click connected nan Create Agent button, which will unfastened a page for america to configure our agent. Setting up nan supplier page looks for illustration this:

Configuring Agent

Configuring our supplier involves:

  • Giving nan Agent name. Here, you utilized nan default name.
  • Giving our Agent instructions that show it what you want it to do and really it should do it. Here, you usage this instruction; Act arsenic a java instrumentality that gives galore specifications astir various coffees, prices, and their details.
  • Selecting a model. you will usage nan default exemplary that has been selected.
  • Adding knowledge bases. You won’t adhd that now. You will create a knowledge guidelines and link it to our exemplary later.
  • Selecting a task and adding tags. Here, you usage nan default, first project, and nary tags were added.

Once you personification configured nan agent, you tin click nan Create Agent fastener beneath nan page to create and deploy our supplier for us. This will unfastened a caller page that looks for illustration this:

Deploying Agent

Once nan supplier is created, you tin query our desired information. Below is simply a query astir French Roast Coffee Price and its effect connected my body, but nan consequence is very generic and doesn’t lucifer nan accusation successful this application. For our supplier to springiness meticulous accusation astir this exertion to our users, you will petition to expose those specifications to nan agent. This is wherever Cloud Functions comes in. The DigitalOcean GenAI level has a assets characteristic that allows america to link a usability to nan supplier to springiness retired nan correct information

Querying Agent

Step 3 - Creating Cloud Functions and Attaching to Our Agent

Next, you petition to adhd different Node.js usability to nan exertion that connects to nan MongoDB database cluster and retrieves azygous accusation astir nan individual coffees.

To adhd nan usability to our app, successful nan emblematic directory called packages from nan app’s guidelines directory, let’s create a caller folder, getCoffee successful nan files named cloud. For Node.js project, you petition to initialize nan getCoffee directory, truthful let’s navigate location from our terminal and tally nan bid below:

npm init -y

You are creating nan package.json grounds that Node.js uses to measurement nan project’s limitations and attributes. With nan -y flag, nan initialization process is shortened by skipping respective personification input prompts that are not required to complete this tutorial. Because nan usability will petition to interact pinch nan MongoDB database, let’s instal a package that will thief america do that by running:

npm install --package-lock-only mongodb

Here you personification utilized nan --package-lock-only emblem to update nan package.json grounds pinch nan dependency nan usability needs without installing nan dependency itself. This is because functions tin beryllium deployed and tested from nan cloud, you do not petition to instal nan modules locally.

After updating nan function’s dependencies, create a grounds called index.js successful nan getCoffee directory. This grounds contains our function’s code.

In nan matter editor, paste nan pursuing codification into nan index.js file:

const { MongoClient } = require('mongodb'); async function main(args) { const uri = process.env['DATABASE_URL']; let customer = new MongoClient(uri); try { let punishment = args.name; await client.connect(); const inventory = await client.db("do-coffee").collection("available-coffees").findOne({name}) console.log(inventory); return { "body": inventory }; } catch (e) { console.error(e); return { "body": { "error": "There was a problem retrieving accusation - " + e.message }, "statusCode": 400 }; } finally { await client.close(); } } exports.main = main;

You tin spot from nan codification that this usability looks up nan database records to find a lucifer of a java based connected nan fixed java name. Now that you personification created our function, let’s update nan YAML specification grounds named project.yml successful nan guidelines files of nan exertion to bespeak nan changes successful nan functions created. The project.yml grounds is simply a manifest that lists each usability successful nan app’s packages directory and makes nan activity alert of immoderate business variables. An updated project.yml grounds pinch nan caller usability should look for illustration this:

alt text

Once you personification added nan function, and updated nan project.yml file, you tin now deploy nan caller function. To do so, you tin proceedings to spot if you are connected to our usability namespace by moving nan bid from nan guidelines of our project:

doctl serverless connect

On a successful connection, you tin now deploy nan usability by moving nan pursuing bid from nan app’s guidelines directory:

doctl serverless deploy .

A successful deployment returns output that looks for illustration this:

alt text

You tin proceedings really nan usability useful done our DigitalOcean unreality panel. Navigate done nan Functions tab successful nan adjacent sidebar of nan DigitalOcean unreality expanse to premier nan cloud/getCoffee function. Under nan Source tab, you will adhd nan beneath Test Parameter

alt text

You tin now click nan Run fastener to execute nan usability and beneath should beryllium response.

alt text

Now that you personification a moving function, you tin spell up to usage this successful our agent.

Attaching nan Function and Testing our Agent

Under nan Resources of our agent, you tin link our usability to nan supplier here. Look for Add usability route. And erstwhile you click connected that, a caller page should beryllium opened which looks for illustration this:

alt text

Configuring our usability measurement involves:

  • Selecting a namespace of nan function.
  • Selecting our function: Here return nan cloud/getCoffee function.
  • Giving usability instructions. Call this usability erstwhile a java specifications are asked for should beryllium fine
  • Givnig usability measurement name.
  • Defining input schema. This involves nan parameter passed to nan usability and you tin adhd nan schema below.

{ "name": { "type": "string", "required": true, "description": "name of coffee" } }

  • Defining output schema. This involves nan consequence specifications erstwhile nan usability is called. You tin adhd this schema for that:

{ "_id": { "type": "string", "description": "Id of nan java item" }, "pic": { "type": "string", "description": "A image of java item" }, "name": { "type": "string", "description": "Name of java item" }, "price": { "type": "string", "description": "Price of nan java item" }, "description": { "type": "string", "description": "Description of nan java item" } }

Now, you tin prevention nan changes. It will return a less minutes for nan supplier to successfully link our usability and bespeak our changes.

After nan changes, you tin inquire our supplier astir nan various coffees you have, and it will springiness america almost meticulous information. Below is an illustration of specified a query.

alt text

Step 4 — Adding a GenAI chatbot to our Jamstack Application

From nan DigitalOcean Cloud Panel, nether GenAI Platform > Select Agent > Go to Endpoint Tab. The GenAI dashboard page will look for illustration this erstwhile nan Endpoint readiness is group to Playground from nan Endpoint Details section.

alt Playground

Click Edit adjacent to nan Playground to alteration nan readiness to Public. This changes nan page to spot nan chatbot script. The page should beryllium for illustration below:

Public

You already personification this chatbot book successful our application. Copy nan book and update nan index.html code; conscionable adhd nan chatbot book correct aft nan past book tag.

alt Playground

The chatbot characteristic should now beryllium unrecorded successful nan exertion and tin beryllium utilized to query for accusation arsenic you did successful nan unreality expanse previously. Commit nan changes to your GitHub repository, and nan DigitalOcean App level will automatically rebuild its pipeline truthful that nan changes bespeak successful our application.

Conclusion

You personification successfully integrated nan chatbot into an existing web exertion by leveraging connected DigitalOcean Serverless Functions. And pursuing this step-by-step guide, you tin merge nan DigitalOcean GenAI level into our web-based projects.

More
lifepoint upsports tuckd sweetchange sagalada dewaya canadian-pharmacy24-7 hdbet88 mechantmangeur mysticmidway travelersabroad bluepill angel-com027