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
- Create a new directory for your chatbot project.
- Navigate to the directory and run the following command to initialize a Node.js project:
npm init -y
- Install dependencies:
npm install dotenv readline @ai-sdk/openai @picahq/ai ai
- Install TypeScript (if needed):
npm install typescript ts-node --save-dev
-
Create a
.env
file to store your API secret key:
PICA_SECRET_KEY=your-pica-secret-key-here
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();
Step 3: Understanding the Code
Let's break down the key components of the code:
-
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.
- We use the
-
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.
- The
-
Chat Loop:
- The
chat()
function handles the chatbot loop. - Users can type messages, start new conversations with
/new
, or exit with/exit
.
- The
-
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.
-
Streaming AI Responses:
- The
streamText()
function calls the AI API, passing in the conversation history. - Responses are streamed in real-time, chunk by chunk.
- The
-
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
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.
Top comments (0)