GIVE US A CALL (949) 446-1716

Building Your Own Multi-Agent AI Sales Platform with Real-Time Human Support

Have you ever wanted to create a sophisticated AI sales system that combines multiple specialized AI agents while allowing human team members to monitor and step in when needed? In this DIY guide, I’ll walk you through how we built our own system using OpenAI’s Assistants API, WebSockets, PHP, and React.

What You’ll Need

  • A web server with PHP support (we used WAMP)
  • MySQL database
  • OpenAI API key
  • Basic knowledge of PHP, JavaScript, and React
  • Familiarity with WebSockets

Step 1: Set Up Your Project Structure

Start by creating your project directory structure:

CopyInsert/your-project-name/
├── admin/
│   ├── index.php
│   └── login.php
├── vendor/
├── config.php
├── index.html
├── style.css
├── websocket_server.php
└── composer.json

Step 2: Configure Your Environment

Install required dependencies using Composer:

{ "require": { "cboden/ratchet": "^0.4.4", "openai/client": "^1.0.0", "phpmailer/phpmailer": "^6.8" } }

Create a config.php file with your database and API credentials:

Step 3: Set Up Your Database

Create your MySQL database with the necessary tables:

CREATE DATABASE your_database;<br>USE your_database;<br><br>CREATE TABLE conversation_summaries (<br>    id INT AUTO_INCREMENT PRIMARY KEY,<br>    thread_id VARCHAR(255) NOT NULL,<br>    user_ip VARCHAR(45) NOT NULL,<br>    summary TEXT,<br>    current_agent VARCHAR(255),<br>    status ENUM('active', 'completed') DEFAULT 'active',<br>    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,<br>    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP<br>);<br><br>CREATE TABLE messages (<br>    id INT AUTO_INCREMENT PRIMARY KEY,<br>    thread_id VARCHAR(255) NOT NULL,<br>    message_id VARCHAR(255) NOT NULL,<br>    role ENUM('user', 'assistant', 'system', 'admin') NOT NULL,<br>    content TEXT NOT NULL,<br>    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,<br>    INDEX (thread_id)<br>);

Step 4: Create Your WebSocket Server

The heart of our system is the WebSocket server that handles real-time communication. Here’s how to build it:

Create websocket_server.php:

<?php require 'vendor/autoload.php'; require_once 'config.php'; use Ratchet\Server\IoServer; use Ratchet\Http\HttpServer; use Ratchet\WebSocket\WsServer; use React\Socket\Server as SocketServer; use React\EventLoop\Factory; use Ratchet\ConnectionInterface; use Ratchet\MessageComponentInterface; class ConversationServer implements MessageComponentInterface { protected $clients; protected $clientThreadMap = []; // Map clients to thread IDs protected $userClients = []; // Track user clients private $conn; private $openAIClient; public function __construct() { $this->clients = new \SplObjectStorage; $this->userClients = new \SplObjectStorage; // Initialize database connection $this->conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); // Initialize OpenAI client $this->openAIClient = \OpenAI::client(OPENAI_API_KEY); } // Implement the required methods: onOpen, onMessage, onClose, onError // [Add implementation details here] } // Create and start the server $loop = Factory::create(); $socket = new SocketServer('0.0.0.0:8080', $loop); $server = new IoServer( new HttpServer( new WsServer( new ConversationServer() ) ), $socket, $loop ); echo "WebSocket server running at 0.0.0.0:8080\n"; $loop->run();

Step 5: Build Your User Interface

Create a user-friendly chat interface using React:

  1. In your index.html, set up the basic structure:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>AI Sales Assistant</title> <script src="https://cdn.tailwindcss.com"></script> <link rel="stylesheet" href="style.css"> </head> <body> <div id="app"></div> <script src="https://unpkg.com/react@17/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script> <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script> <script type="text/babel"> // Your React application code goes here </script> </body> </html>
  2. Create your React chat component with WebSocket connection:
  3. function App() { const [messages, setMessages] = React.useState([]); const [threadId, setThreadId] = React.useState(null); const [inputValue, setInputValue] = React.useState(”); const [isProcessing, setIsProcessing] = React.useState(false); const socket = React.useRef(null); // Initialize WebSocket connection React.useEffect(() => { const wsProtocol = window.location.protocol === ‘https:’ ? ‘wss:’ : ‘ws:’; const wsHost = ‘localhost:8080’; const wsUrl = `${wsProtocol}//${wsHost}`; const newSocket = new WebSocket(wsUrl); // Handle WebSocket events (onopen, onmessage, onclose, onerror) // [Add implementation details here] socket.current = newSocket; return () => { if (newSocket) { newSocket.close(); } }; }, []); // Implement message handling and UI components // [Add implementation details here] return ( // Your chat UI JSX goes here ); } ReactDOM.render(, document.getElementById(‘app’));

Step 6: Create the Admin Interface

Build an admin panel to monitor and manage conversations:

  1. Create admin/index.php:phpCopyInsert<?php session_start(); require_once '../config.php'; // Implement authentication if (!isset($_SESSION['admin_authenticated'])) { // Show login form or authenticate // [Add implementation details] } // Fetch active conversations from database $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); $query = "SELECT * FROM conversation_summaries ORDER BY created_at DESC"; $result = $conn->query($query); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Admin Panel</title> <script src="https://cdn.tailwindcss.com"></script> </head> <body> <!-- Implement your admin UI here --> <!-- Left panel for conversation list --> <!-- Right panel for viewing and participating in conversations --> <script> // JavaScript for WebSocket connection and admin functions // [Add implementation details] </script> </body> </html>

Step 7: Configure Your OpenAI Assistants

You’ll need to create four different assistants in the OpenAI platform:

  1. AI Sales Manager: Configure with instructions to identify user needs and direct to the appropriate specialized agent
  2. New Sales Agent: Specialize in handling new customer inquiries
  3. Existing Sales Agent: Optimize for returning customer interactions
  4. Estimates Agent: Focus on gathering requirements and generating estimates

For each assistant, use the OpenAI web interface to:

  • Create a new assistant with appropriate name and description
  • Set up the model (we recommend GPT-4)
  • Add detailed instructions for the assistant’s role
  • Enable structured output for agent handoffs

Step 8: Implementing Agent Handoffs

The key to our multi-agent system is the handoff mechanism. In your WebSocket server, implement handling for structured outputs:

private function processAssistantResponse($threadId, $runId) {<br>    // Retrieve the run and check for structured output<br>    $run = $this->openAIClient->threads()->runs()->retrieve(<br>        threadId: $threadId,<br>        runId: $runId<br>    );<br>    <br>    // Check for structured outputs indicating agent handoffs<br>    if (property_exists($run, 'structured_outputs') && !empty($run->structured_outputs)) {<br>        foreach ($run->structured_outputs as $output) {<br>            if (property_exists($output, 'next_agent')) {<br>                $nextAgent = $output->next_agent;<br>                <br>                // Update the agent in the database<br>                $this->updateConversationAgent($threadId, $nextAgent);<br>                <br>                // Create a new run with the next agent<br>                $this->openAIClient->threads()->runs()->create(<br>                    threadId: $threadId,<br>                    parameters: [<br>                        'assistant_id' => $nextAgent<br>                    ]<br>                );<br>                <br>                return true; // Handoff initiated<br>            }<br>        }<br>    }<br>    <br>    return false; // No handoff<br>}

Step 9: Start Your WebSocket Server

Create a simple script to start your WebSocket server:

# start_websocket_server.bat<br>php websocket_server.php

Step 10: Testing Your System

  1. Start your WebSocket server
  2. Open your main site in one browser window
  3. Open the admin panel in another browser window
  4. Test the entire flow:
    • Start a conversation as a user
    • Observe the conversation in the admin panel
    • Test agent handoffs by triggering different scenarios
    • Test human takeover from the admin panel

Troubleshooting Common Issues

  • WebSocket Connection Failures: Ensure your server allows WebSocket connections and that ports are properly configured
  • OpenAI API Errors: Verify your API key and check OpenAI’s status page for service issues
  • Database Connection Problems: Confirm your database credentials and that your tables are properly set up
  • Agent Handoff Issues: Review your structured output format and ensure your assistants are properly configured

Leave a Reply