This guide will help you get started with Truffle AI by creating and using your first AI agent.

Installation

Install the Truffle AI SDK using npm:

npm install truffle-ai

Basic Usage

1. Initialize the Client

Login to Truffle AI, go to settings page and get your API key.

Use this to create a new TruffleAI client:

import { TruffleAI } from 'truffle-ai';

const truffle = new TruffleAI('your-api-key');

2. Create an Agent

Deploy a new AI agent with specific instructions:

const agent = await truffle.deployAgent({
  name: 'Support Assistant',
  instruction: 'You are a customer support specialist who helps users with their questions.',
  model: 'gpt-4o-mini'
});

You can also create agents directly on the platform.

3. Using Your Agent

You can use your agent in two ways:

Run One-off Tasks

For single tasks or questions:

const result = await agent.run('What is artificial intelligence?');
console.log(result);

Chat Sessions

For interactive conversations with context:

// Create a chat session
const chat = agent.chat();

// Send messages and get responses
const response1 = await chat.send('Hi! My name is Alice.');
console.log(response1);

const response2 = await chat.send("What's my name?"); // Agent remembers "Alice"
console.log(response2);

// Get chat history
const history = chat.getHistory();

// Clear chat history if needed
chat.clearHistory();

Agent Management

Loading Existing Agents

Load a previously created agent:

const existingAgent = await truffle.loadAgent('your-agent-id');

You can see all your agents on the agents page. Get agent ids by clicking on the info button on the agent card.

Complete Example

Here’s a complete example showing various agent operations:

import { TruffleAI } from 'truffle-ai';

async function main() {
  // Initialize client
  const truffle = new TruffleAI('your-api-key');
  
  // Create an agent
  const agent = await truffle.deployAgent({
    name: 'Support Assistant',
    instruction: 'You are a customer support specialist who helps users with their questions.',
    model: 'gpt-4o-mini'
  });
  
  // Run a one-off task
  const taskResult = await agent.run('What are your main responsibilities?');
  console.log('Task Result:', taskResult);
  
  // Start a chat session
  const chat = agent.chat();
  
  // Have a conversation
  const responses = await Promise.all([
    chat.send('Hi! I need help with my account.'),
    chat.send('How do I change my password?'),
    chat.send('Thanks for your help!')
  ]);
  
  // Get chat history
  const history = chat.getHistory();
  console.log('Chat History:', history);
}

main().catch(console.error);

Next Steps

Now that you’ve created your first agent, you can: