Patterns

Patterns with Examples

Drawing from Anthropic’s guide Building effective agents, let's explore structured patterns for building intelligent workflows. Using the ChatBotKit SDK, developers can create scalable and efficient AI-driven applications. Here are some key patterns with implementation examples.

1. Sequential Processing

Sequential processing involves executing tasks in a predetermined order, where each step depends on the output of the previous step.

import { ChatBotKit } from '@chatbotkit/sdk' const cbk = new ChatBotKit({ secret: process.env.CHATBOTKIT_API_KEY, }) async function sequentialProcessing() { // Create specialized bots for each step const analyzerBot = await cbk.bot.create({ name: 'Data Analyzer', model: 'gpt-4', backstory: 'You are specialized in analyzing raw data and extracting key information.', }) const summarizerBot = await cbk.bot.create({ name: 'Summarizer', model: 'gpt-4', backstory: 'You are specialized in creating concise summaries from analyzed data.', }) // Create a conversation that will use these bots sequentially const conversation = await cbk.conversation.create({ model: 'gpt-4', botId: analyzerBot.id, }) // Process data through the first bot const analysisResult = await cbk.conversation.message.create(conversation.id, { content: 'Raw data to analyze...', role: 'user', }) // Update conversation to use the second bot await cbk.conversation.update(conversation.id, { botId: summarizerBot.id, }) // Process the analysis through the summarizer const finalSummary = await cbk.conversation.message.create(conversation.id, { content: analysisResult.content, role: 'user', }) return finalSummary }

javascript

Let's break down what's happening in the sequential processing code example:

  • Bot Creation: Two specialized bots are created - one for data analysis and another for summarization, each with specific roles defined in their backstories.
  • Conversation Initialization: A conversation is created initially using the analyzer bot.
  • First Processing Step: Raw data is sent to the analyzer bot for initial processing.
  • Bot Switching: The conversation is updated to use the summarizer bot for the second step.
  • Final Processing: The analysis results are passed to the summarizer bot to create a final summary.

This pattern demonstrates how to chain multiple specialized agents together, where each agent performs its specific task in sequence, building upon the output of the previous agent.

2. Parallel Processing

Parallel processing allows multiple agents to work simultaneously on different aspects of a task.

import { ChatBotKit } from '@chatbotkit/sdk' const cbk = new ChatBotKit({ secret: process.env.CHATBOTKIT_API_KEY, }) async function parallelProcessing(input) { // Create multiple specialized bots const sentimentBot = await cbk.bot.create({ name: 'Sentiment Analyzer', model: 'gpt-4', backstory: 'You analyze the sentiment of text.', }) const entityBot = await cbk.bot.create({ name: 'Entity Extractor', model: 'gpt-4', backstory: 'You extract named entities from text.', }) // Create parallel conversations const [sentimentConv, entityConv] = await Promise.all([ cbk.conversation.create({ botId: sentimentBot.id }), cbk.conversation.create({ botId: entityBot.id }), ]) // Process in parallel const results = await Promise.all([ cbk.conversation.message.create(sentimentConv.id, { content: input, role: 'user', }), cbk.conversation.message.create(entityConv.id, { content: input, role: 'user', }), ]) return { sentiment: results[0].content, entities: results[1].content, } }

javascript

Let's examine the parallel processing code example in detail:

  • Bot Initialization: Two specialized bots are created - a sentiment analyzer and an entity extractor, each with specific roles.
  • Parallel Conversation Creation: Using Promise.all, two conversations are created simultaneously, one for each bot.
  • Concurrent Processing: The input text is processed simultaneously by both bots using Promise.all for parallel execution.
  • Result Aggregation: The results from both bots are collected and returned in a structured object containing sentiment analysis and entity extraction data.

This pattern demonstrates how to process data concurrently using multiple specialized agents, improving efficiency by running tasks in parallel rather than sequentially.

3. Evaluation/Feedback Loops

This pattern implements continuous improvement through feedback and evaluation mechanisms.

import { ChatBotKit } from '@chatbotkit/sdk' const cbk = new ChatBotKit({ secret: process.env.CHATBOTKIT_API_KEY, }) async function feedbackLoop() { // Create a bot with evaluation capabilities const mainBot = await cbk.bot.create({ name: 'Main Agent', model: 'gpt-4', backstory: 'You are the primary agent that generates responses.', }) const evaluatorBot = await cbk.bot.create({ name: 'Evaluator', model: 'gpt-4', backstory: 'You evaluate responses for quality and accuracy.', }) // Create a conversation const conversation = await cbk.conversation.create({ botId: mainBot.id, }) // Generate initial response const response = await cbk.conversation.message.create(conversation.id, { content: 'User query...', role: 'user', }) // Evaluate the response const evaluationConv = await cbk.conversation.create({ botId: evaluatorBot.id, }) const evaluation = await cbk.conversation.message.create(evaluationConv.id, { content: `Evaluate this response: ${response.content}`, role: 'user', }) // If evaluation score is low, update the bot's response if (evaluation.score < 0.7) { await cbk.bot.downvote(mainBot.id, { value: 1 }) return await regenerateResponse(conversation.id) } await cbk.bot.upvote(mainBot.id, { value: 1 }) return response }

javascript

Let's analyze the feedback loop code example in detail:

  • Bot Creation: Two bots are created - a main agent for generating responses and an evaluator bot for assessing response quality.
  • Initial Response Generation: The main bot processes the user query and generates a response.
  • Evaluation Process: The evaluator bot assesses the quality of the main bot's response.
  • Feedback Mechanism: Based on the evaluation score:
    • If score is low (<0.7), the main bot receives a downvote and regenerates the response
    • If score is satisfactory, the main bot receives an upvote and the original response is returned

This pattern implements a quality control system where responses are evaluated before being returned to the user, ensuring continuous improvement through feedback.

4. Orchestration

Orchestration manages the coordination of multiple agents and their interactions.

import { ChatBotKit } from '@chatbotkit/sdk' const cbk = new ChatBotKit({ secret: process.env.CHATBOTKIT_API_KEY, }) async function orchestrator() { // Create a skillset for orchestration const orchestrationSkillset = await cbk.skillset.create({ name: 'Orchestration Skillset', description: 'Manages and coordinates multiple agents', }) // Create specialized abilities await cbk.skillsetAbility.create(orchestrationSkillset.id, { name: 'Task Distribution', instructions: 'Analyze task and distribute to appropriate agents', }) // Create orchestrator bot const orchestratorBot = await cbk.bot.create({ name: 'Orchestrator', model: 'gpt-4', skillsetId: orchestrationSkillset.id, backstory: 'You coordinate and manage other specialized agents.', }) // Create agent pool const agentPool = await Promise.all([ cbk.bot.create({ name: 'Research Agent', model: 'gpt-4' }), cbk.bot.create({ name: 'Writing Agent', model: 'gpt-4' }), cbk.bot.create({ name: 'Review Agent', model: 'gpt-4' }), ]) // Create main conversation const mainConversation = await cbk.conversation.create({ botId: orchestratorBot.id, }) return { orchestrator: orchestratorBot, agents: agentPool, conversation: mainConversation, } }

javascript

Let's analyze the orchestration code example in detail:

  • Skillset Creation: A specialized skillset is created for orchestration purposes, defining the core capabilities needed for managing multiple agents.
  • Ability Definition: A specific ability for task distribution is added to the skillset, enabling the orchestrator to analyze and delegate tasks.
  • Orchestrator Bot Setup: A main orchestrator bot is created with the defined skillset and given the role of coordinating other agents.
  • Agent Pool Creation: Multiple specialized agents (Research, Writing, and Review) are created in parallel using Promise.all.
  • Conversation Initialization: A main conversation is established under the orchestrator's control.

This pattern demonstrates how to create a hierarchical system where a central orchestrator manages and coordinates multiple specialized agents, enabling complex workflows and task distribution.

5. Routing

Routing directs incoming requests to the most appropriate agent based on specific criteria.

import { ChatBotKit } from '@chatbotkit/sdk' const cbk = new ChatBotKit({ secret: process.env.CHATBOTKIT_API_KEY, }) async function setupRouter() { // Create a dataset for routing rules const routingDataset = await cbk.dataset.create({ name: 'Routing Rules', description: 'Contains patterns and rules for request routing', }) // Add routing rules to dataset await cbk.datasetRecord.create(routingDataset.id, { text: 'technical_support: keywords[error, bug, issue, help]', }) // Create router bot const routerBot = await cbk.bot.create({ name: 'Router', model: 'gpt-4', datasetId: routingDataset.id, backstory: 'You analyze requests and route them to appropriate specialized agents.', }) async function routeRequest(request) { const conversation = await cbk.conversation.create({ botId: routerBot.id, }) // Search dataset for matching routing rules const routingMatch = await cbk.dataset.search( routingDataset.id, request ) // Route to appropriate agent based on match const targetAgent = determineTargetAgent(routingMatch) await cbk.conversation.update(conversation.id, { botId: targetAgent.id, }) return conversation } return { routerBot, routeRequest } }

javascript

Let's analyze the routing code example in detail:

  • Dataset Creation: A dataset is created to store routing rules and patterns that determine how requests should be directed.
  • Rule Definition: Routing rules are added to the dataset, including keyword patterns that help identify request types (e.g., technical support keywords).
  • Router Bot Setup: A specialized router bot is created with access to the routing rules dataset and configured to analyze and direct requests.
  • Request Routing Function: The routeRequest function:
    • Creates a new conversation with the router bot
    • Searches the dataset for matching routing rules based on the request
    • Determines the appropriate target agent
    • Updates the conversation to use the selected target agent

This pattern demonstrates how to implement intelligent request routing, ensuring that each request is handled by the most appropriate specialized agent based on predefined rules and patterns.

Best Practices

  1. Error Handling: Implement robust error handling for each pattern to manage failures gracefully.
  2. Monitoring: Use the usage API to monitor resource consumption and performance.
  3. Feedback Integration: Implement upvote/downvote mechanisms to continuously improve agent performance.
  4. Resource Management: Clean up unused conversations and bots to optimize resource usage.
  5. Security: Use secrets management for sensitive configurations and API keys.

Considerations

  • Scalability: Consider implementing rate limiting and queue mechanisms for high-load scenarios.
  • Cost Efficiency: Monitor and optimize model usage to balance cost and performance.
  • Maintenance: Regularly update bot backstories and skillsets based on performance data.
  • Integration: Consider using triggers for automated pattern execution and monitoring.

These patterns can be combined and customized based on specific use cases and requirements. The ChatBotKit SDK provides the flexibility to implement complex agent architectures while maintaining simplicity in implementation.