Quick Start
Installation
AINU is available as an npm package. You can install it using the following command:
npm install @ainulabs/ainuCreate An Agent
Agents are the core abstraction in AINU. They orchestrate AI tasks by interacting with providers, tools, and other components. Follow these steps to create your first agent.
Step 1: Import Required Modules
Start by importing the necessary classes from AINU:
import { Agent, Anthropic } from "@ainulabs/ainu";Here, Agent is the main class for creating agents, and Anthropic is a provider that connects the agent to an AI model.
Step 2: Set Up a Provider
Providers are responsible for connecting your agent to an AI service. For this example, we'll use the Anthropic provider:
const provider = new Anthropic({
apiKey: "your-api-key", // Replace with your actual API key
});The apiKey is required to authenticate with the Anthropic API. You can replace this with other providers like OpenAI or XAI, depending on your use case.
Step 3: Create the Agent
Now, create an agent and pass the provider to it:
const agent = new Agent({
provider,
});This sets up a basic agent that can interact with the Anthropic provider.
Adding a Tool
Step 1: Import the Tool Class
Import the Tool class from AINU:
import { Tool } from "@ainulabs/ainu";
import { z } from "zod"; // For schema validationStep 2: Create a Tool
Define a tool with a name, description, parameters, and a handler function. For example, a tool to fetch weather data:
const weatherTool = new Tool("getWeather", {
description: "Get the weather at a given location",
parameters: z.object({
location: z.string(),
}),
handler: ({ location }) => {
// Mock implementation
return `The weather in ${location} is sunny.`;
},
});Here:
parameters defines the expected input using zod for validation.
handler is the function executed when the tool is called.
Step 3: Attach the Tool to the Agent
Add the tool to the agent using the putTool method:
agent.putTool(weatherTool);The agent can now use this tool to handle tasks related to weather.
Generating Text
Once your agent is set up with a provider and tools, you can use it to generate text or perform other tasks.
Step 1: Use the generateText Method
generateText MethodCall the generateText method on the agent to generate text based on a prompt:
const response = await agent.generateText({
prompt: "What is the weather in New York?",
maxSteps: 3, // Optional: Limits the number of steps the agent takes
});
console.log(response.data?.text);Here:
promptis the input text for the agent.maxStepslimits the number of steps the agent takes to complete the task.
Step 2: Combine Tools and Text Generation
If the agent has tools (like the weather tool), it can use them to enhance its responses. For example:
const response = await agent.generateText({
prompt: "What is the weather in New York?",
tools: [weatherTool], // Pass the tool explicitly
});
console.log(response.data?.text);This allows the agent to use the weatherTool to fetch weather data dynamically.
Running the Code
To run your code, ensure all dependencies are installed and your file is saved. Use the following command:
node your-file-name.jsIf you're using TypeScript, compile the code first:
npm run build
node dist/your-file-name.jsWith these steps, you've successfully installed AINU, created an agent, added a tool, and generated text. You're now ready to explore more advanced features, such as MCP integration and multi-agent systems, in the Core Concepts section.
Last updated