DEV Community

Ramandeep Singh
Ramandeep Singh

Posted on

Build a Powerful AI Chatbot UI in Minutes with Next.js

Vercel AI SDK is a library that allows you to build AI-powered applications in Next.js projects without any knowing specific LLM provider APIs. The most common use case being messaging use-case.

Demo video:

Create the next.js project and install the dependencies. Only 2 files - page.tsx and route.ts are required to build a chatbot. Find the complete code here.

Install the dependencies.

npm install ai @ai-sdk/openai zod @ai-sdk/react
Enter fullscreen mode Exit fullscreen mode

To make it specialized, I only needed to add a system parameter to your streamText configuration with a financial advisor-specific prompt. This system prompt will:

  1. Role: Financial advisor AI assistant
  2. Guidelines: Professional, accurate, user-focused
  3. Scope: Personal finance, investing, budgeting, planning
  4. Uncertainty: Ask clarifying questions if needed

It also provides ability to include tools for financial calculations. For example, in this case I included a tool for calculating compound interest.

tools: {
    calculateCompoundInterest: tool({
        description: 'Calculate compound interest for investments',
    inputSchema: z.object({
      principal: z.number().describe('Initial investment amount'),
      rate: z.number().describe('Annual interest rate (as decimal)'),
      time: z.number().describe('Time period in years'),
    }),
    execute: async ({ principal, rate, time }) => {
      const amount = principal * Math.pow(1 + rate, time);
      return { amount, interest: amount - principal };
    },
  }),
}
Enter fullscreen mode Exit fullscreen mode

This chatbot now behaves as a specialized financial advisor, providing more focused and relevant responses for financial queries!

Demo

Top comments (1)

Collapse
 
nguyn_nhc_a2df78ca8 profile image
Nguyễn Đình Đức

"Discover the power of AhaChat AI – designed to grow your business.”

“AhaChat AI: The smart solution for sales, support, and marketing.”

“Empower your business with AhaChat AI today.”