Loading...
Large Language Models (LLMs) are great at answering questions, but Agents can take actions. By giving an LLM access to tools (like a calculator, web search, or a database), you empower it to solve complex, multi-step problems.
In this tutorial, we will build a simple agent using LangChain and deploy it to a Vercel Edge Function.
Create a new Next.js project:
npx create-next-app@latest ai-agent
cd ai-agent
npm install @langchain/openai langchain @langchain/core
Create a .env.local file and add your API key:
OPENAI_API_KEY=sk-...
Create a new file at app/api/chat/route.ts. We will use Next.js Route Handlers configured for the Edge runtime.
import { ChatOpenAI } from "@langchain/openai";
import { AgentExecutor, createOpenAIFunctionsAgent } from "langchain/agents";
import { Calculator } from "langchain/tools/calculator";
import { ChatPromptTemplate } from "@langchain/core/prompts";
export const runtime = 'edge';
export async function POST(req: Request) {
const { messages } = await req.json();
// 1. Initialize the model
const model = new ChatOpenAI({ temperature: 0, modelName: "gpt-4-turbo" });
// 2. Define tools
const tools = [new Calculator()];
// 3. Create the prompt
const prompt = ChatPromptTemplate.fromMessages([
["system", "You are a helpful mathematical assistant."],
["human", "{input}"],
["placeholder", "{agent_scratchpad}"],
]);
// 4. Initialize agent
const agent = await createOpenAIFunctionsAgent({ llm: model, tools, prompt });
const executor = new AgentExecutor({ agent, tools });
// 5. Execute
const result = await executor.invoke({ input: messages[messages.length - 1].content });
return Response.json({ output: result.output });
}
Deploying is as simple as pushing your code to GitHub and importing the repository into Vercel. Because we specified export const runtime = 'edge', Vercel will automatically deploy this route handler to their global edge network, ensuring blazing-fast response times globally.
Remember to add your OPENAI_API_KEY to the Environment Variables section in your Vercel project settings!