Building an Agentic AI Office Assistant: A Practical Guide with Node.js
Want to create an AI that does more than just chat? Let’s explore how to build an AI-powered office assistant that can handle real tasks like scheduling meetings, checking HR availability, and sending emails. This AI will not just respond to queries but will also take action using APIs and other integrations.
What is Agentic AI?
Agentic AI refers to systems that can take actions on their own to achieve specific goals. Unlike basic chatbots that only provide pre-set responses, agentic AI can:
- Make decisions based on real-time data.
- Call APIs and execute functions.
- Handle multi-step processes.
- Maintain context throughout a conversation
The Project: An AI Office Assistant
Let’s build something practical: an AI office assistant that can:
- Check office hours and HR availability
- Provide information about company services
- Schedule appointments and send confirmation emails
- Handle date/time conversions and validations
The office hours and availability of HR in this demo is mock data. In a real-world application, you would typically fetch this information by making calls to calendar APIs or querying a database.
How the AI Agent Works
User Input
↓
AI Analysis (GPT-4)
↓
Function Selection
↓
Execute Function
↓
Add Result to Context
↓
AI Decides:
│
├─→ Need more info? (Call another function)
│
└─→ Ready to respond? (Return final answer)
Demo Video
The Conversation Flow
Imagine you’re talking to our AI office assistant. Here’s what happens behind the scenes:
Handling Complex Tasks: Appointment Scheduling
Let’s walk through how the AI schedules an appointment:
Step 1: Initial Request
User: “I’d like to schedule an appointment for tomorrow.
Step 2: Information Gathering
The AI automatically:
1. Determines tomorrow’s date
2. Checks if it’s a working day
3. Verifies HR availability
4. Decides if scheduling is possible
Step 3: User Information Collection
If HR is available, the AI:
1. Asks for your name
2. Requests email
3. Gets phone number
4. Confirms preferred time
Step 4: Verification & Booking
The system then:
1. Validates all information
2. Books the appointment
3. Sends email confirmation
4. Provides confirmation to user
Key Components of the AI Assistant
1. Function Calling
The core of Agentic AI is its ability to decide when and which functions to call. Here’s how it works:
// AI receives a question: "Is HR available today?"
// 1. First, it calls getTodayDate()
const today = await getTodayDate(); // Returns "2024-03-20"
// 2. Then converts date to weekday
const weekday = await getWeekFromDate(today); // Returns "Wednesday"
// 3. Finally checks HR availability
const availability = await getHRAvailability(weekday);
// Returns "10:00 AM - 05:00 PM"
2. Context Management
The AI maintains context throughout the conversation:
// User: "I want to schedule a meeting"
// AI first checks availability before booking
async function handleAppointmentRequest() {
// 1. Check availability
const hrAvailable = await getHRAvailability(weekday);
if (hrAvailable) {
// 2. Collect appointment details
const appointment = await requestAppointment(
name, email, phone, date, time
);
// 3. Send confirmation
return "Great! I've scheduled your appointment...";
} else {
return "I apologize, but HR isn't available at that time...";
}
}
3. Tool Integration
A tool in the OpenAI API is a function or capability that the model can call dynamically to perform specific tasks, retrieve external data, or interact with other systems. Tools extend the model’s abilities beyond just text-based responses by enabling actions like fetching real-time data, executing functions, or integrating with APIs
[
{
type: "function",
function: {
name: "getHRAvailability",
description: "Get the HR availability",
parameters: {
type: "object",
properties: {
week: {
type: "string",
description: "Day of the week (Monday, Tuesday, etc.)"
}
},
required: ["week"]
}
}
},
]
How to run the AgenticAI Office Assistant NodeJS Project
Prerequisites
- Node.js (version 20.0.0 or higher)
- npm (Node Package Manager)
- OpenAI API key
- SMTP email server credentials
Configuration
git clone https://github.com/synclovisdevs/agentic-ai-demo
cd agentic-ai-demo
npm install
3. Create a `.env` file in the root directory with the following variables:
OPENAI_API_KEY=your_openai_key_here
EMAIL_HOST=your_smtp_host
EMAIL_USER=your_email
EMAIL_PASS=your_email_password
EMAIL_TO=recipient_email
EMAIL_FROM=sender_email
Start the application
npm start
Key concepts through the code
The assistant follows a structured flow to process user requests and perform necessary actions:
1. User Input Processing
– The assistant starts with a system message that defines its role:
const messages = [
{
role: "system",
content: "You are a helpful assistant of Synclovis Company Office..."
}
];
– When a user provides input, it is added to the messages array.
2. Generating AI Response with OpenAI
The core of the assistant is the call to OpenAI’s GPT-4 API, which analyzes the conversation and decides if it needs to invoke a function (tool). This happens through the following code:
const response = await openaiClient.chat.completions.create({
model: "gpt-4",
messages: messages,
tools: toolsConfig,
});
Here, toolsConfig contains the set of predefined tools (functions) the AI can use.
3. Handling Function Calls (Tools)
– If OpenAI determines that a tool should be called (e.g., checking HR availability), it returns a response with tool_calls.
– Then we extract the function name and arguments from the agent response:
const functionName = message.tool_calls[0].function.name;
const functionToCall = availableTools[functionName];
const functionArgs = JSON.parse(message.tool_calls[0].function.arguments);
– The corresponding function is executed dynamically:
const functionResponse = await functionToCall(...Object.values(functionArgs));
– The result is then added to the conversation context, allowing the AI to make further decisions.
4. Iterative Reasoning (Looping) and Why maxIterations is Used
The assistant can handle multi-step interactions by looping through function calls. If another tool is needed, it calls itself recursively:
return agent(userInput, iterationCount + 1, maxIterations);
Why maxIterations?
This prevents infinite loops where the assistant keeps calling functions without reaching a final response. It ensures that the assistant stops after a reasonable number of steps, avoiding unnecessary API calls and improving performance. If the limit is reached, the assistant returns a fallback response:
return "The maximum number of iterations has been met without a suitable answer. Please try again with a more specific input.";
5. Returning the Final Response
Once all required actions are completed, the assistant responds to the user with a natural language answer:
if (finish_reason === "stop") {
messages.push(message);
return message.content;
}
Source Code
Want to explore the complete implementation? Check out the full source code on GitHub.
Conclusion
Agentic AI is the next step in AI evolution, moving beyond simple chatbots to AI systems that can perform meaningful actions. By integrating large language models with well-defined tools and APIs, we can build AI assistants that truly enhance productivity.