This guide provides practical examples of how to use Truffle AI agents for different scenarios.

Virtual Assistant

Create a personal assistant that helps with various tasks:

import { TruffleAI } from 'truffle-ai';

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

const assistant = await truffle.deployAgent({
  name: 'Personal Assistant',
  instruction: `You are a helpful personal assistant who:
    - Provides clear and concise responses
    - Maintains a friendly tone
    - Helps users with their tasks
    - Remembers context from previous interactions`,
  model: 'gpt-4o-mini',
  tool: 'Google Calendar' // Authentication is required for this tool via dashboard
});

// Run one-off tasks
const taskResponse = await assistant.run("What's on my schedule today?");
console.log(taskResponse);

// Start an interactive session
const chat = assistant.chat();
const responses = await Promise.all([
  chat.send('Can you help me plan my week?'),
  chat.send('I need to schedule a meeting for tomorrow'),
  chat.send('Also remind me to buy groceries')
]);

// Check conversation history
const history = chat.getHistory();
console.log('Conversation History:', history);

Research Assistant

Create a Q&A system that provides accurate information:

const qaBot = await truffle.deployAgent({
  name: 'Research Assistant',
  instruction: `You are a research assistant who:
    - Provides accurate, factual information
    - Cites sources when possible
    - Admits when information is uncertain
    - Explains complex topics clearly
    - Uses examples to illustrate concepts`,
  model: 'gpt-4o-mini',
  tool: 'Tavily Research'
});

// Ask specific questions
const answer1 = await qaBot.run('What is quantum computing?');
console.log('Quantum Computing:', answer1);

const answer2 = await qaBot.run('Explain machine learning in simple terms');
console.log('Machine Learning:', answer2);

// Interactive learning session
const chat = qaBot.chat();
await chat.send('I want to learn about artificial intelligence');
await chat.send('What should I study first?');
await chat.send('Can you create a learning plan for me?');

Content Assistant

Create an agent that helps repurpose YouTube content:

const contentAgent = await truffle.deployAgent({
  name: 'Content Creator',
  instruction: `You are a content creator who:
    - Writes engaging and clear content
    - Adapts tone to match the target audience
    - Provides creative suggestions
    - Helps with editing and improvement
    - Maintains consistent style`,
  model: 'gpt-4o-mini',
  tool: 'YouTube Transcript'
});

// Convert YouTube video to blog post
const blogPost = await contentAgent.run(
  'Convert this YouTube video to a blog post: https://youtube.com/watch?v=example'
);
console.log('Blog Post:', blogPost);

// Interactive content repurposing
const chat = contentAgent.chat();
await chat.send('I want to turn this YouTube video into social media content: https://youtube.com/watch?v=example');
await chat.send('Can you create 5 Twitter threads from the key points?');
await chat.send('Now let's create some LinkedIn carousel slides from the main concepts');

Best Practices for Examples

When implementing these examples, keep in mind:

  1. Error Handling: Always implement proper error handling in production code:
try {
  const agent = await truffle.deployAgent({
    name: 'My Assistant',
    instruction: 'You are a helpful AI assistant.',
    model: 'gpt-4o-mini'
  });
  
  const result = await agent.run('Hello!');
} catch (error) {
  if (error instanceof ValidationError) {
    console.error('Invalid configuration:', error.message);
  } else if (error instanceof AuthenticationError) {
    console.error('Authentication failed:', error.message);
  } else {
    console.error('An error occurred:', error.message);
  }
}
  1. Context Management: Use chat sessions effectively:

    • Keep related conversations in the same chat session
    • Clear chat history when starting new topics
    • Save important conversation history when needed
  2. Agent Instructions: Write clear and specific instructions:

    • Define the agent’s role and personality
    • Specify expected behavior and limitations
    • Include examples of desired responses when relevant
  3. Resource Management: Clean up resources when done:

    • Delete agents that are no longer needed
    • Clear chat histories to free up memory
    • Handle connections and errors properly