Skip to content

Building AI agents, atomically

feat: Add multi-agent quickstart tutorial notebook

KennyVaneetvelde8909f4889774
diff --git a/examples/notebooks/multi_agent_quickstart.ipynb b/examples/notebooks/multi_agent_quickstart.ipynb
new file mode 100644
index 0000000..49845a3
--- /dev/null
+++ b/examples/notebooks/multi_agent_quickstart.ipynb
@@ -0,0 +1,233 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Multi-Agent Quickstart Tutorial\n",
+ "\n",
+ "This quickstart tutorial demonstrates how to create a multi-agent system using the Atomic Agents library. The system will include a search agent and a calculator agent, which can be orchestrated to handle different types of user queries.\n",
+ "\n",
+ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/KennyVaneetvelde/atomic_agents/blob/main/examples/notebooks/multi_agent_quickstart.ipynb)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Prerequisites\n",
+ "\n",
+ "Before proceeding with this notebook, it is highly recommended to read up on the basics of the following libraries:\n",
+ "\n",
+ "- **Pydantic**: A data validation and settings management library using Python type annotations. You can find more information and documentation at [Pydantic GitHub](https://github.com/pydantic/pydantic).\n",
+ "- **Instructor**: A Python library that simplifies working with structured outputs from large language models (LLMs). It provides a user-friendly API to manage validation, retries, and streaming responses. More details can be found at [Instructor GitHub](https://github.com/jxnl/instructor).\n",
+ "\n",
+ "Understanding these libraries will help you make the most of this library.\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Install Necessary Packages\n",
+ "\n",
+ "First, we need to install the required packages. Run the following command to install `atomic-agents`, `openai`, and `instructor` libraries."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Install necessary packages\n",
+ "%pip install atomic-agents openai instructor"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Import Libraries\n",
+ "\n",
+ "We will import the necessary libraries for creating the multi-agent system."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import os\n",
+ "from typing import Union\n",
+ "import instructor\n",
+ "import openai\n",
+ "from pydantic import create_model\n",
+ "from rich.console import Console\n",
+ "from atomic_agents.agents.base_chat_agent import BaseAgentIO, BaseChatAgent, BaseChatAgentConfig\n",
+ "from atomic_agents.agents.tool_interface_agent import ToolInterfaceAgent, ToolInterfaceAgentConfig\n",
+ "from atomic_agents.lib.tools.search.searx_tool import SearxNGSearchTool, SearxNGSearchToolConfig\n",
+ "from atomic_agents.lib.tools.calculator_tool import CalculatorTool, CalculatorToolConfig"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Initialize Components\n",
+ "\n",
+ "Initialize the necessary components including the client, tools, and agents."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Initialize the console for output\n",
+ "console = Console()\n",
+ "\n",
+ "# Initialize the client\n",
+ "client = instructor.from_openai(openai.OpenAI())\n",
+ "\n",
+ "# Initialize the SearxNG search tool\n",
+ "searx_tool = SearxNGSearchTool(SearxNGSearchToolConfig(base_url=os.getenv('SEARXNG_BASE_URL'), max_results=10))\n",
+ "\n",
+ "# Initialize the calculator tool\n",
+ "calc_tool = CalculatorTool(CalculatorToolConfig())"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Configure and Create Agents\n",
+ "\n",
+ "Configure and create the search agent and calculator agent."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Configure the search agent\n",
+ "search_agent_config = ToolInterfaceAgentConfig(client=client, model='gpt-3.5-turbo', tool_instance=searx_tool, return_raw_output=False)\n",
+ "\n",
+ "# Configure the calculator agent\n",
+ "calculator_agent_config = ToolInterfaceAgentConfig(client=client, model='gpt-3.5-turbo', tool_instance=calc_tool, return_raw_output=False)\n",
+ "\n",
+ "# Create the search agent and calculator agent\n",
+ "searx_agent = ToolInterfaceAgent(config=search_agent_config)\n",
+ "calc_agent = ToolInterfaceAgent(config=calculator_agent_config)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Define Union Response Schema\n",
+ "\n",
+ "Create a union response schema that can handle responses from both the search agent and the calculator agent."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Define a union response schema\n",
+ "UnionResponse = create_model('UnionResponse', __base__=BaseAgentIO, response=(Union[searx_agent.input_schema, calc_agent.input_schema], ...))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Create Orchestration Agent\n",
+ "\n",
+ "Create an orchestration agent that can manage the interactions between the user and the individual agents."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "# Create the orchestration agent\n",
+ "orchestration_agent = BaseChatAgent(config=BaseChatAgentConfig(client=client, model='gpt-3.5-turbo', output_schema=UnionResponse))"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Main Chat Loop\n",
+ "\n",
+ "Create a main chat loop to interact with the orchestration agent. The agent will determine whether to use the search tool or the calculator tool based on the user's input."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "while True:\n",
+ " user_input = input('You: ')\n",
+ " if user_input.lower() in ['exit', 'quit']:\n",
+ " print('Exiting chat...')\n",
+ " break\n",
+ "\n",
+ " response = orchestration_agent.run(orchestration_agent.input_schema(chat_input=user_input))\n",
+ " console.print(f'Agent: {response.response}')\n",
+ "\n",
+ " if isinstance(response.response, searx_agent.input_schema):\n",
+ " console.print(f'Using searx agent')\n",
+ " response = searx_agent.run(response.response)\n",
+ " elif isinstance(response.response, calc_agent.input_schema):\n",
+ " console.print(f'Using calc agent')\n",
+ " response = calc_agent.run(response.response)\n",
+ "\n",
+ " console.print(f'Agent: {response.response}')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Conclusion\n",
+ "\n",
+ "In this notebook, we demonstrated how to create a multi-agent system using the Atomic Agents library. The system includes a search agent and a calculator agent, which can be orchestrated to handle different types of user queries. You can further customize the agents and enhance their capabilities based on your requirements.\n",
+ "\n",
+ "You can also create your own tools and wrap them into the tool agent. This allows you to extend the functionality of the multi-agent system to suit your specific needs."
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.12.3"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 4
+ }
\ No newline at end of file
diff --git a/examples/notebooks/orchestrator.ipynb b/examples/notebooks/orchestrator.ipynb
index 6a9ed60..b608280 100644
--- a/examples/notebooks/orchestrator.ipynb
+++ b/examples/notebooks/orchestrator.ipynb
@@ -332,7 +332,7 @@
" print('Exiting chat...')\n",
" break\n",
"\n",
- " response = agent.run(user_input)\n",
+ " response = agent.run(agent.input_schema(chat_input=user_input))\n",
" console.print(f'Agent: {response}')\n"
]
}