In today's digital landscape, interactive and engaging user experiences are crucial for the success of web applications. As the demand for intelligent and interactive chatbots continues to grow, developers are constantly exploring innovative ways to enhance user experiences within their applications. One powerful tool that has gained significant popularity is ChatGPT, an advanced language model developed by OpenAI. With its ability to generate human-like responses and understand natural language, integrating ChatGPT into your Angular application can elevate your user engagement to new heights.
In this article, we will walk you through the process of adding a ChatGPT bot to your Angular application. We'll explore the necessary steps, from setting up an OpenAI API account to creating a seamless user experience within your application. Whether you're building a customer support chatbot, an interactive virtual assistant, or any other conversational interface, this guide will equip you with the knowledge and tools to get started.
Prerequisites:
Before diving into the integration process, ensure that you have the following prerequisites in place:
ChatGPT API access: To interact with the
ChatGPT model, you'll need an API key or credentials. If you don't have one,
you can obtain it from OpenAI's platform or consult the relevant documentation.
- Visit the OpenAI website:
- Go to the OpenAI website by typing "openai.com" in your web browser's address bar and pressing Enter.
- Explore the OpenAI API section:
- Navigate to the OpenAI API section of the website. You can usually find it in the top navigation menu or by searching for "OpenAI API" in the website's search bar.
- Understand the API access requirements:
- Get familiar with the details and requirements for accessing the OpenAI API. Take note of any pricing, usage limits, or terms of service that may apply.
- Sign in to your OpenAI account or create a new one:
- If you already have an OpenAI account, sign in using your credentials.
- If you don't have an account, create a new one by following the registration process provided on the OpenAI website.
- Navigate to the API documentation:
- Once you're logged in, navigate to the API documentation section. Look for documentation related to the ChatGPT API or any specific information related to integrating ChatGPT into your application.
- Review the API documentation:
- Take the time to carefully read and understand the API documentation. It should provide information on endpoints, request and response formats, authentication, and any additional details necessary for making API calls.
- Familiarize yourself with the pricing and billing:
- Understand the pricing structure for using the ChatGPT API. OpenAI may charge for API usage based on factors such as the number of requests made or the amount of data processed. Ensure that you are aware of the associated costs.
- Apply for API access or join the waitlist:
- Depending on the availability of the ChatGPT API, you may need to apply for access or join a waitlist. Follow the instructions provided in the API documentation to request access. OpenAI may require additional information or have specific criteria for granting access.
- Await confirmation and receive API access:
- Once you have applied for access, await confirmation from OpenAI. They will typically send an email notification with instructions on how to access the ChatGPT API.
- Retrieve your API key:
- Upon receiving access to the ChatGPT API, you should be provided with an API key. This key is a crucial piece of information required to authenticate and make requests to the API.
Choose the model for communication with
Open AI API
The OpenAI API is powered by
a diverse set of models with different capabilities and price points. You can
also make limited customizations to our original base models for your specific
use case with fine-tuning.
MODELS |
DESCRIPTION |
Limited beta |
A set of models that improve on GPT-3.5
and can understand as well as generate natural language or code |
A set of models that improve on GPT-3
and can understand as well as generate natural language or code |
ENDPOINT |
MODEL NAME |
/v1/chat/completions |
gpt-4, gpt-4-0314, gpt-4-32k,
gpt-4-32k-0314, gpt-3.5-turbo, gpt-3.5-turbo-0301 |
/v1/completions |
text-davinci-003, text-davinci-002,
text-curie-001, text-babbage-001, text-ada-001 |
Creating ChatGPT Bot in Angular:
- Set up the Angular Development Environment:
Make sure you have Node.js installed on your machine.
Install the Angular CLI globally by running the following command in your terminal or command prompt: npm install -g @angular/cli
Create a new Angular project:
- Open your terminal or command prompt and navigate to the directory where you want to create your project.
- Run the following command to generate a new Angular project: ng new chatgpt-bot --skip-tests --style=scss
- For the question “Would you like to add Angular routing?” please choose N because we do not need any routing for this app.
Install Angular Material and the Angular CDK:
Navigate to your project's root directory: cd chatgpt-bot
Install Angular Material and the Angular CDK by running the following command:
ng add @angular/material
Select a custom theme when prompted. Choose the "Indigo/Pink" theme, or select any other options you prefer.
This command will install Angular Material and its dependencies and add the necessary styles and configurations to your project.
Customize your application with Angular Material components:
Open the src/app/app.component.html file and modify it according to your requirements. You can use Angular Material components in your templates. For example:
Open the src/app/ app.module.ts file and add MatButtonModule to import.
Run the Angular application:
In your terminal or command prompt, make sure you are in your project's root directory.
Run the following command to start the development server and launch your application: ng serve
Open your web browser and navigate to http://localhost:4200 to see your Angular application in action.
The result should be identical to the screen below.
Open AI API key
Create in the main folder a new folder called environments. Inside this folder, please create a file called environment.ts. This environment.ts file has the next structure:
Go to the Open AI and open the API keys page. Click on
the "+ Create new secret key" button. Let's get a string like
"sk-xxxxx..." and copy it to our environment.ts file as the value of
the openAIKey variable.
Added customer-support-button and customer-support
components:
Step 1: Create the Components
Open your terminal and navigate to your Angular
project's root directory. Then, run the following command to generate the
components:
ng generate component
customer-support-button
ng generate component customer-support
It will create two new directories: customer-support-button
and customer-support. Inside each directory, you'll find the necessary
files for the components.
Step 2: Add Required Material UI modules to app.module.ts
Open your app.module.ts and update it similar to the
screen below:
Step 3: Create a service for Chat GPT communication
Open the terminal once again and run the following command to generate the service:
ng generate service chat-service
Verify that our service was added to app.module.ts. If not, please update your app.module.ts
Open chat-service.service.ts for editing and pass the next logic:
Here you will find detailed information about the parameters of creating completion such as temperature, max_tokens etc.
NB! You will probably hear about nuget package called openai. The reason why we do not use it here is that this library is meant for server-side usage only, as using it in client-side browser code will expose your secret API key. So, if you want to use this library, you must use node.js or some backend server such as express or some others. This logic is out of scope for us because we are not going to share our secret key somewhere if it is crucial to you – move your communication logic to the backend.
Step 4: Add Component Logic and Template
Open the customer-support-button.component.ts
file located in the customer-support-button directory. This file contains the
component's TypeScript logic.
import { Component } from '@angular/core';
Open the customer-support-button.component.html
file located in the customer-support-button directory. This file contains the
component's markup logic.
For the customer-support component, we need to add a few models first. In your Angular project's root directory please create folder models and add the next file gpt-response.model.ts.
This PersonType will help us to identify our response (the question from us or response from ChatGPT).
Repeat the same process for the customer-support
component as we have already done for customer-support-button components. Add logic
and template in the corresponding .ts and .html files in the customer-support
directory.
Open the customer-support.component.html file
located in the customer-support directory and update it in the following way:
Open the customer-support.component.ts file
located in the customer-support directory and update it as follows:
When we run our component right now, it will look ugly,
however, it should work. So, it’s high time to add nice styling to our
application.
Step 5: Add Component Styling
Setup global styling
Inside the folder assets create new folder styles.
Create four SCSS files in our newly created folder: _mixins.scss, _base.scss,
_dialog.scss and _variables.scss. Besides the files above, we
need to create one index.scss file with the next setup:
Open angular.json file and find "styles": [] for build section and update as follows:
Open _variables.scss and provide the next variables:
Open _base.scss and provide the next changes:
Open _mixins.scss and provide the next changes:
Open _dialog.scss and provide the next changes:
Setup local styling
We are almost at the finish line. Only styling for our
created components is left, and we are going to start with the smallest one.
Open customer-support-button.component.scss and update
it in the following way:
Open customer-support.component.scss and update this file as well:
Let's run our application and verify that we have our awesome blue and yellow styles applied.
Open the terminal once again and run the following command:
ng serve
And we have to see the nice styled button at the bottom right side of our application.
Click on this eSupport button and check what we will have next
Let’s ask our Chat GPT bot how to learn angular
Areas for improvement:
In the current implementation, we do not have any error
handling. So, for your production code, you have to handle status codes from
open ai gracefully.
I would like to say a personal thank you to Mykola Pavelchuk for assisting
with the design of this chat gpt bot.
The source code for our chatbot can be found here.
I hope this small example will help you to understand how to use Open AI API with chatbots.
No comments:
Post a Comment