Every day, new AI products and tools emerge, making it feel like AI is taking the world by storm — and for good reason. AI assistants can be incredibly useful across various domains, including ecommerce, customer support, media and content creation, marketing, education, and more. The significance and utility of AI are undeniable.
Having expertise in AI in today’s rapidly evolving landscape can give you a huge advantage. The skill to build and ship AI agents is becoming increasingly sought after as the demand for AI-powered solutions continues to grow. The good news is that you don’t need to be an AI/ML expert to build AI agents and products. With the right toolset, building AI agents can be both accessible and enjoyable.
This tutorial will guide you through building AI agents from scratch. We’ll build, deploy, implement, and test a webpage FAQ generator AI agent within a frontend project. This agent will generate a selected number of FAQs based on specified topics and keywords. We’ll also explore how to enhance the accuracy of an AI agent by instructing it to use a predefined set of documents instead of relying solely on web data.
As with any development project, success hinges on choosing the right platform and tools. Here’s our powerful tech stack for building AI agents:
- OpenAI: The most used AI model provider. To follow along with this tutorial you’ll need to have an account with it
- BaseAI: A free and open source web AI framework for building serverless AI agents with Node.js and TypeScript
- Langbase: A powerful serverless platform for building and deploying AI products
The BaseAI and Langbase duo is a powerful and flexible professional toolset for building and deploying AI agents and products with great DX. Developers can build, mix and match, test, and deploy AI agents and use them to create powerful AI products fast, easy, and at low cost. All major LLMs are supported and can be used with one unified API.
Get excited, because by the end of this post, you'll be well on your way to creating your very own AI agents like a pro. Let's get started!
What is an AI agent?
An AI agent is a software program that uses artificial intelligence to perform tasks or make decisions on its own, often interacting with users or systems. It can be a chatbot, virtual assistant, or any tool that learns from data and automates processes, making things easier and faster.
What are the benefits of building your own AI agent?
- Being able to leverage all the power of LLMs without the limits of third-party tools
- Flexible customization tailored to your specific project needs/use cases
- Seamless integration with your workflow and toolset
- Full control over the processed data
- Better privacy and security
- Scalability that meets your changing/growing needs
- Cost efficiency in the long run
- Full alignment with your brand identity
An introduction to the BaseAI framework
To use BaseAI efficiently, you need to understand the main functionalities it offers:
- AI pipes: These are serverless AI agents. They provide a unified API for all LLMs and work with any language and framework
- AI memory: This is a serverless RAG (Retrieval Augmented Generation) agent that provides long-term memory functionality with the ability to obtain, process, retain, and retrieve information
- AI tool: This is a function inside your codebase used to perform tasks that the AI model can't handle alone
In this tutorial, we’ll explore the first two: AI pipes and memory.
Getting started
BaseAI works in close relation with Langbase, which provides a versatile AI Studio for building, testing, and deploying AI agents. The first step is to create a free account with Langbase. Once you have an account, you need to set up two things:
- Langbase User API key (you can find this in the Langbase settings)
- Your OpenAI API key in the LLM API keyset
Now you are ready to start using BaseAI!
Let’s create a new Node project:
mkdir building-ai-agents && cd building-ai-agents
npm init -y
npm install dotenv
Now, let’s initialize the new BaseAI project inside:
npx baseai@latest init
Normally, the base project structure looks like this:
ROOT (of your app)
├── baseai
| ├── baseai.config.ts
| ├── memory
| ├── pipes
| └── tools
├── .env (your env file)
└── package.json
Right now, your project can differentiate a bit. You may notice that in your project, the memory, pipes, and tools directories are missing. Don’t worry — these are auto-generated when you create at least one memory, pipe, or tool respectively.
Also before you start building AI agents, you need to add the Langbase API key and OpenAI API key in the project’s .env file. Rename the env.baseai.example file to .env and put the API keys in the appropriate places:
# !! SERVER SIDE ONLY !!
# Keep all your API keys secret — use only on the server side.
# TODO: ADD: Both in your production and local env files.
# Langbase API key for your User or Org account.
# How to get this API key https://langbase.com/docs/api-reference/api-keys
LANGBASE_API_KEY="YOUR-LANGBASE-KEY"
# TODO: ADD: LOCAL ONLY. Add only to local env files.
# Following keys are needed for local pipe runs. For providers you are using.
# For Langbase, please add the key to your LLM keysets.
# Read more: Langbase LLM Keysets https://langbase.com/docs/features/keysets
OPENAI_API_KEY="YOUR-OPENAI-KEY"
ANTHROPIC_API_KEY=
COHERE_API_KEY=
FIREWORKS_API_KEY=
GOOGLE_API_KEY=
GROQ_API_KEY=
MISTRAL_API_KEY=
PERPLEXITY_API_KEY=
TOGETHER_API_KEY=
XAI_API_KEY=
N.B., the baseai.config.ts file provides several configuration settings, one of which is to change the name of your .env file to suit your needs. You can do this by setting the envFilePath property.
Building a webpage FAQ generator AI agent locally using BaseAI
In this section, we’ll create your first AI agent — a webpage FAQ generator that generates a specified number of question-answer pairs about specific topics and keywords, with the selected tone.
Creating and configuring an AI pipe
To create a new pipe, run the following:
npx baseai@latest pipe
The CLI will ask you for the name and description of the pipe, and whether it will be public or private. Set the name to “faqs-generator” and the description to “A webpage FAQs generator”. Finally, make the pipe private. Once the pipe is created, you can find it in baseai/pipes/faqs-generator.ts. Open it and replace the content with this:
import { PipeI } from '@baseai/core';
const pipeFaqsGenerator = (): PipeI => ({
// Replace with your API key https://langbase.com/docs/api-reference/api-keys
apiKey: process.env.LANGBASE_API_KEY!,
name: 'faqs-generator',
description: 'A webpage FAQs generator',
status: 'private',
model: 'openai:gpt-4o-mini',
stream: true,
json: false,
store: true,
moderate: true,
top_p: 1,
max_tokens: 1000,
temperature: 0.7,
presence_penalty: 1,
frequency_penalty: 1,
stop: [],
tool_choice: 'auto',
parallel_tool_calls: true,
messages: [
{
role: 'system',
content: `You're a helpful AI assistant. Generate {{count}} frequently asked questions (FAQs) about {{topic}} using the keywords {{keywords}}.
Each FAQ should consist of a question followed by a concise answer. Ensure the answers are clear, accurate, and helpful for someone who is unfamiliar with the topic. Keep the tone {{tone}}.
`
}
],
variables: [
{ name: 'count', value: '' },
{ name: 'topic', value: '' },
{ name: 'keywords', value: '' },
{ name: 'tone', value: '' }],
memory: [],
tools: []
});
export default pipeFaqsGenerator;
As you can see, the system prompt has now changed to suit our specific needs for FAQ generation:
"You're a helpful AI assistant. Generate {{count}} frequently asked questions (FAQs) about {{topic}} using the keywords {{keywords}}.
Each FAQ should consist of a question followed by a concise answer. Ensure the answers are clear, accurate, and helpful for someone who is unfamiliar with the topic. Keep the tone {{tone}}."
BaseAI allows you to use variables in your prompts. You can turn any text into a variable by putting it between {{}}. So in our case, we need to create four variables:
-
count: Sets the number of the FAQs we want to be generated -
topic: Sets the main topic of the FAQs -
keywords: Adds additional keywords to make the topic more specific -
tone: Defines the tone of the generated content
These variables are provided when you run the pipe. We’ll explore this in a moment.
Integrating the pipe
Once we have created the pipe, we need to put it into action. Create a index.ts file in the root and add this content:
import 'dotenv/config';
import {Pipe, getRunner} from '@baseai/core';
import pipeFaqsGenerator from './baseai/pipes/faqs-generator';
const pipe = new Pipe(pipeFaqsGenerator());
async function main() {
const {stream} = await pipe.run({
messages: [],
variables: [
{ name: 'count', value: '3' },
{ name: