AIbitat Framework

repository·main·Indexed 41 days ago

https://github.com/wladpaiva/aibitat

An extensible, stateless, and customizable TypeScript framework for multi-agent conversations. AIbitat allows developers to define agents with specific roles, organize them into channels for group interactions, and register executable functions as tools. It supports multiple AI providers including OpenAI and Anthropic, and includes plugins for CLI interaction, file history, and experimental web browsing.

Tokens
7.5K
Snippets
25
Records
35
Agent score
100%

What's inside AIbitat

  1. Install and set up AIbitat

    main

    AIbitat is a TypeScript framework for multi-agent conversations. It is recommended to use Bun for installation and execution.

    1. Initialize a project:
    mkdir my-project
    cd my-project
    bun init
    1. Install the package:
    bun install aibitat
    1. Configure Environment Variables: Create an .env file in your project root and add your OpenAI API key:
    OPENAI_API_KEY=your_api_key_here
    # to install bun go to https://bun.sh and follow the instructions
    mkdir my-project
    cd my-project
    bun init
    
    # then install aibitat
    bun install aibitat
  2. How the CLI plugin handles interruptions and feedback

    main

    The cli plugin implements an interactive feedback loop during conversation interruptions. When the framework triggers an onInterrupt event, the plugin uses the cli.askForFeedback method to pause execution and wait for user input via the terminal.

    Workflow:

    1. Interruption: An agent or process triggers an interrupt.
    2. Prompt: The terminal displays: Provide feedback to [target] as [source]. Press enter to skip and use auto-reply, or type 'exit' to end the conversation: .
    3. User Action:
      • Input text: The input is passed back to the framework via aibitat.continue(feedback).
      • Empty input (Enter): The framework proceeds using its default auto-reply logic.
      • 'exit': The process terminates with process.exit(0).

    This mechanism allows humans to steer multi-agent conversations in real-time.

  3. Use the web-browsing-plugin

    main

    The web-browsing-plugin allows agents to search the internet via Google (using Serper.dev) or scrape and summarize specific website content.

    To use this plugin, register it with your AIbitat instance using the experimental_webBrowsing function. Once registered, the agent can call a tool named web-browsing which accepts either a query (for searching) or a url (for scraping).

    Required Environment Variables:

    • SERPER_API_KEY: Required for Google searches via serper.dev.
    • BROWSERLESS_TOKEN: Required for scraping website content via browserless.io.

    Tool Parameters:

    • query (string): A search query to perform on Google.
    • url (string, URI format): A specific URL to scrape.

    Note: If the scraped content exceeds 8,000 characters, the plugin automatically summarizes it using OpenAI's gpt-3.5-turbo-16k-0613 model.

    import { experimental_webBrowsing } from 'aibitat/plugins/web-browsing';
    
    const aibitat = new AIbitat(config);
    aibitat.use(experimental_webBrowsing());
  4. Quickstart: Create a multi-agent conversation

    main

    To start a conversation between multiple agents, instantiate AIbitat, define agents with specific roles, group them into a channel, and call .start().

    In the example below, a client (human-like) asks a question, a mathematician solves it, and a reviewer confirms the result.

    import {AIbitat} from 'aibitat'
    import {cli} from 'aibitat/plugins'
    
    const aibitat = new AIbitat()
      .use(cli())
      .agent('client', {
        interrupt: 'ALWAYS',
        role: 'You are a human assistant. Reply "TERMINATE" when there is a correct answer or there`s no answer to the question.',
      })
      .agent('mathematician', {
        role: `You are a Mathematician and only solve math problems from @client`,
      })
      .agent('reviewer', {
        role: `You are a Peer-Reviewer and you do not solve math problems. 
        Check the result from @mathematician and then confirm. Just confirm, no talk.`,
      })
      .channel('management', ['mathematician', 'reviewer', 'client'])
    
    await aibitat.start({
      from: 'client',
      to: 'management',
      content: 'How much is 2 + 2?',
    })
    
    console.log(aibitat.chats)
    import {AIbitat} from 'aibitat'
    import {cli} from 'aibitat/plugins'
    
    const aibitat = new AIbitat()
      .use(cli())
      .agent('client', {
        interrupt: 'ALWAYS',
        role: 'You are a human assistant. Reply "TERMINATE" when there is a correct answer or there`s no answer to the question.',
      })
      .agent('mathematician', {
        role: `You are a Mathematician and only solve math problems from @client`,
      })
      .agent('reviewer', {
        role: `You are a Peer-Reviewer and you do not solve math problems. 
        Check the result from @mathematician and then confirm. Just confirm, no talk.`,
      })
      .channel('management', ['mathematician', 'reviewer', 'client'])
    
    await aibitat.start({
      from: 'client',
      to: 'management',
      content: 'How much is 2 + 2?',
    })
    
    console.log(aibitat.chats)
  5. Define an agent with `.agent(name, config)`

    main

    The .agent(name, config) method creates a new agent.

    Config Options:

    • role: A string describing the agent's persona/task. Used to guide the agent's behavior.
    • interrupt: Determines if the agent can pause the conversation.
      • NEVER: The agent will not interrupt.
      • ALWAYS: The agent will always interrupt (though agents can also interrupt by replying INTERRUPT or TERMINATE).
    • functions: An array of strings containing the names of functions this agent is allowed to call.
    • maxRounds: The maximum number of replies this agent can contribute to prevent loops.

    Agents inherit the interrupt setting from the global AIbitat config unless specified otherwise.

  6. Create a communication channel with `.channel(name, agents, config)`

    main

    The .channel(name, agents, config) method creates a group of agents that can interact like a Slack channel.

    Parameters:

    • name: The unique identifier for the channel.
    • agents: An array of agent names belonging to this channel.
    • config: (Optional) Configuration object. By default, maxRounds is set to 100.

    Channels allow agents to talk with many others simultaneously rather than just via direct messages.

  7. Register and use functions with `.function(config)`

    main

    Functions allow agents to execute code and return results back into the conversation.

    To use a function:

    1. Define the function using .function(config).
    2. Add the function's name to the functions array in the target agent's configuration.

    Function Configuration Object:

    • name: Unique identifier for the function.
    • description: A description used to help the LLM understand when to call the function.
    • parameters: A JSON Schema object defining the input arguments.
    • async handler(): The implementation of the function that returns the result to the conversation.
    const aibitat = new AIbitat()
      .agent('coder', {functions: ['doSomething']})
      .function({
        name: 'doSomething',
        description: 'Let me do something for you.',
        parameters: {
          type: 'object',
          properties: {},
        },
        async handler() {
          return 'Result of execution'
        },
      })
    const aibitat = new AIbitat()
      .agent('...', {functions: ['doSomething']})
      .function({
        name: 'doSomething',
        description: 'Let me do something for you.',
        parameters: {
          type: 'object',
          properties: {},
        },
        async handler() {
          return '...'
        },
      })
  8. Extend AIbitat with plugins using `.use(plugin)`

    main

    Plugins allow you to add new features or integrations to AIbitat. You can use built-in plugins or implement your own using the AIbitatPlugin interface.

    Available Plugins:

    • cli: Adds a CLI interaction mode to the chat.
    • fileHistory: Saves chat history to a JSON file (defaults to the history folder).
    • experimental_webBrowsing: Enables agents to search and navigate the internet (experimental).

    Implementing a Custom Plugin:

    import {AIbitatPlugin} from 'aibitat'
    
    export function myPlugin(): AIbitatPlugin {
      return {
        name: 'my-plugin',
        setup(aibitat) {
          console.log(`setting up my plugin`)
          aibitat.onMessage(({from, to, content}) => {
            console.log(`${from}: ${content}`)
          })
        },
      }
    }
    
    // Usage
    const aibitat = new AIbitat().use(myPlugin())
    import {AIbitatPlugin} from 'aibitat'
    
    export function myPlugin(): AIbitatPlugin {
      return {
        name: 'my-plugin',
        setup(aibitat) {
          console.log(`setting up my plugin`)
    
          aibitat.onMessage(({from, to, content}) => {
            console.log(`${from}: ${content}`)
          })
        },
      }
    }
    
    // To use:
    // const aibitat = new AIbitat().use(myPlugin())
  9. Initialize AIbitat with configuration

    main

    Use new AIbitat(config) to create a new instance. By default, AIbitat uses OpenAI with GPT-3.5-TURBO for conversations and GPT-4 for predicting the next speaker.

    Default Configuration Object:

    {
      providers: 'openai',
      model: 'gpt-3.5-turbo',
      interrupt: 'NEVER',
      maxRounds: 100,
      chats: []
    }

    You can override these settings globally in the constructor, or specifically within individual .agent() or .channel() calls.

    const aibitat = new AIbitat({
      providers: 'openai',
      model: 'gpt-4',
      interrupt: 'ALWAYS',
      maxRounds: 50
    });
  10. Configure the OpenAIProvider

    main

    The OpenAIProvider allows you to use OpenAI's chat completion models within the AIbitat framework. You can configure the provider using an OpenAIProviderConfig object.

    Configuration Options

    KeyTypeDefaultDescription
    optionsClientOptions{ apiKey: process.env.OPENAI_API_KEY, maxRetries: 3 }Standard OpenAI client configuration options (e.g., apiKey, organization, timeout).
    modelOpenAIModel'gpt-3.5-turbo'The specific OpenAI model to use for completions.

    Cost Tracking

    The provider includes built-in cost estimation based on token usage for several models, including gpt-4, gpt-4-32k, gpt-3.5-turbo, and gpt-3.5-turbo-16k.

    const provider = new OpenAIProvider({
      options: {
        apiKey: 'your-api-key',
      },
      model: 'gpt-4',
    });
  11. Configure the file-history plugin

    main

    The fileHistory plugin accepts an optional configuration object to specify where the history should be stored.

    OptionTypeDefaultDescription
    filenamestring`history/chat-history-${new Date().toISOString()}.json`The file path (including filename) where the chat history JSON will be saved.
    // Example with default filename
    aibitat.use(fileHistory());
    
    // Example with custom filename
    aibitat.use(fileHistory({ filename: 'logs/chat.json' }));
  12. Configure web-browsing-plugin environment variables

    main

    The web-browsing plugin relies on external services for search and scraping. Ensure the following environment variables are set in your runtime:

    VariablePurposeService
    SERPER_API_KEYEnables Google search functionalityserper.dev
    BROWSERLESS_TOKENEnables website scraping and content extractionbrowserless.io

    If SERPER_API_KEY is not provided via environment variables, you can pass it directly to the internal search function if accessing the plugin's logic directly.