AI agents are revolutionizing the way we interact with the internet. These autonomous software programs leverage the power of large language models (LLMs) to go beyond simple text generation. They can engage with digital environments, make decisions, and perform tasks based on their understanding of language.
Prerequisites
A Langbase account: Sign up for a free account athttps://langbase.com .Understanding of Next.js: Familiarity with the Next.js framework is essential for this project.Basic knowledge of BaseAI: A grasp of the fundamental concepts of BaseAI will be helpful.
Setting up the Environment
Installing Next.js: Start by creating a new Next.js project using the following command:
npx create-next-app@latest nextjs-baseai-app
Installing BaseAI: Install BaseAI within your project directory:
npx baseai@latest init
Creating a Summary AI Agent Pipe: Use the baseai CLI to create a new pipe named "summary". Pipes are the building blocks of AI agents, defining their specific functionality.
npx baseai@latest pipe
Setting Environment Variables: Create a .env file in your project directory to store your API keys.
cp .env.baseai.example .env
Building the AI Agent
Adding an API Route Handler: Create a new API route handler at app/api/langbase/pipes/run/route.ts to handle requests to your AI agent.
import { Pipe } from '@baseai/core';
import { NextRequest } from 'next/server';
import pipeSummary from '../../../../../baseai/pipes/summary';
export async function POST(req: NextRequest) {
const runOptions = await req.json();
// 1. Initiate the Pipe.
const pipe = new Pipe(pipeSummary());
// 2. Run the pipe
const result = await pipe.run(runOptions);
// 3. Return the response stringified.
return new Response(JSON.stringify(result));
}
Creating a React Component: Add the following components to your Next.js app to provide a user interface for interacting with your AI agent:
app/pipe-run/page.tsx: This page will display the AI agent's interface.components/pipe-run.tsx: This component will handle the execution of the AI agent's pipe.components/ui/button.tsx: This component will provide a button for triggering the AI agent.components/ui/input.tsx: This component will allow users to enter their prompts.
npm install @radix-ui/react-slot class-variance-authority clsx tailwind-merge
import PipeRunExample from '@/components/pipe-run';
export default function Page() {
return (
<div className="w-full max-w-md">
<h1 className="text-2xl font-light text-gray-800 mb-1 text-center">
⌘ Langbase AI Agent Pipe: Run
</h1>
<p className="text-muted-foreground text-base font-light mb-20 text-center">
Run a pipe to generate a text completion
</p>
<PipeRunExample />
</div>
);
}
Integrating with Langbase
Adding Environment Variables: Configure your Langbase and LLM provider keys by adding them to your .env file. These keys are essential for authentication and accessing the necessary language models.
# !! 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=
# TODO: ADD: LOCAL ONLY. Add only to local env files.
# Following keys are needed for local pipe runs. Add the providers you are using.
# For Langbase, please add the keys in your LLM keysets on Langbase Studio.
# Read more: Langbase LLM Keysets https://langbase.com/docs/features/keysets
OPENAI_API_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=
Running the Application: Start the BaseAI development server and your Next.js application in separate terminal windows.
# Terminal 1
npx baseai@latest dev # Start BaseAI dev server
# Terminal 2
npm run dev # Start Next.js app
Deploying to Langbase
Deploying to Langbase: Deploy your project to Langbase to make it accessible as a serverless, highly scalable API. First, authenticate with your Langbase account using the following command:
npx baseai@latest auth
npx baseai@latest deploy
0 comments:
Post a Comment