DEV Community

Moe Katib
Moe Katib

Posted on

1

Building a CLI Chatbot with AI

In this tutorial, we will walk through how to create a command-line interface (CLI) chatbot using Node.js and AI-powered APIs. By the end, you'll have a functional chatbot that can carry on conversations, manage chat histories, and even interact with external tools.

Prerequisites

Make sure you have the following installed:

  • Node.js (version 14 or higher)
  • npm or yarn
  • A Pica API key
  • Basic knowledge of TypeScript

Step 1: Setting Up the Project

  1. Create a new directory for your chatbot project.
  2. Navigate to the directory and run the following command to initialize a Node.js project:
   npm init -y
Enter fullscreen mode Exit fullscreen mode
  1. Install dependencies:
   npm install dotenv readline @ai-sdk/openai @picahq/ai ai
Enter fullscreen mode Exit fullscreen mode
  1. Install TypeScript (if needed):
   npm install typescript ts-node --save-dev
Enter fullscreen mode Exit fullscreen mode
  1. Create a .env file to store your API secret key:
   PICA_SECRET_KEY=your-pica-secret-key-here
Enter fullscreen mode Exit fullscreen mode

Step 2: Create the Chat Script

Create a new file called chat.ts in your project directory and copy the following code:

import { createInterface } from "readline";
import { streamText, CoreMessage, tool } from "ai";
import { openai } from "@ai-sdk/openai";
import { config } from "dotenv";
import { Pica } from "@picahq/ai";

config();

const rl = createInterface({
  input: process.stdin,
  output: process.stdout,
});

let chatHistory: CoreMessage[] = [];

async function chat(): Promise<void> {
  const pica = new Pica(process.env.PICA_SECRET_KEY as string);
  const systemPrompt = await pica.generateSystemPrompt();

  console.log(
    "Welcome to the CLI Chat! Type '/new' to start a new chat or '/exit' to quit."
  );

  while (true) {
    const userInput: string = await new Promise((resolve) =>
      rl.question("You: ", resolve)
    );

    if (userInput.toLowerCase() === "/exit") {
      console.log("Goodbye!");
      rl.close();
      process.exit(0);
    }

    if (userInput.toLowerCase() === "/new") {
      console.log("Starting a new chat...");
      chatHistory = [];
      continue;
    }

    chatHistory.push({ role: "user", content: userInput });

    try {
      const result = await streamText({
        model: openai("gpt-4o"),
        messages: chatHistory,
        system: systemPrompt,
        tools: { ...pica.oneTool },
        maxSteps: 10,
      });

      process.stdout.write("AI: ");
      let aiResponse = "";

      for await (const chunk of result.textStream) {
        process.stdout.write(chunk);
        aiResponse += chunk;
      }
      console.log("\n");

      chatHistory.push({ role: "assistant", content: aiResponse });
    } catch (error) {
      console.error("Error:", (error as Error).message);
    }
  }
}

chat();
Enter fullscreen mode Exit fullscreen mode

Step 3: Understanding the Code

Let's break down the key components of the code:

  1. Readline Interface:

    • We use the readline module to interact with the user via the terminal.
    • The createInterface() method sets up input and output streams for the CLI.
  2. Environment Configuration:

    • The dotenv package reads the .env file to load environment variables.
    • PICA_SECRET_KEY is used to authenticate requests to the Pica API.
  3. Chat Loop:

    • The chat() function handles the chatbot loop.
    • Users can type messages, start new conversations with /new, or exit with /exit.
  4. Tool Integration:

    • The chatbot has access to tools through the Pica API. This allows it to perform additional tasks, such as reading and sending emails, managing calendars, and accessing other connected tools.
  5. Streaming AI Responses:

    • The streamText() function calls the AI API, passing in the conversation history.
    • Responses are streamed in real-time, chunk by chunk.
  6. Error Handling:

    • Errors are caught and logged to the console.

Step 4: Running the Chatbot

To run the chatbot, use the following command:

npx ts-node chat.ts
Enter fullscreen mode Exit fullscreen mode

You should see a welcome message prompting you to start a conversation.

Step 5: Customizing the Chatbot

You can enhance the chatbot by modifying the following:

  • System Prompt: Customize the generateSystemPrompt() method to adjust the AI's tone and style.
  • Max Steps: Increase or decrease the maxSteps parameter to control the complexity of AI responses.
  • Commands: Add more commands like /help or /settings for improved user experience.
  • Tool Access: Integrate new tools through the Pica API to expand the bot's capabilities.
  • Error Handling: Implement more detailed error handling and logging.

Step 6: Deploying the Chatbot

If you want to share your chatbot with others, consider packaging it as an npm module.

Conclusion

Congratulations! You've built a fully functional CLI chatbot powered by AI. With this foundation, you can expand and customize the bot to fit various use cases, such as customer support, productivity assistants, or educational tools.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more