PHP code example of excelle-insights / ai-assistant

1. Go to this page and download the library: Download excelle-insights/ai-assistant library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

excelle-insights / ai-assistant example snippets


if (class_exists(\ExcelleInsights\AiAssistant\Facade\AiAssistantManager::class)) {
    (new \ExcelleInsights\AiAssistant\Services\AiAssistantService())->onInboundMessages($results);
}

// api/src/Services/MaintainaContextProvider.php
use ExcelleInsights\AiAssistant\Contracts\SystemContextProviderInterface;

class MaintainaContextProvider implements SystemContextProviderInterface {
  public function getCompanyContext(int $companyId): array {
    $db = \ExcelleCore\Core\Database::getInstance()->getConnection();
    $row = $db->query("SELECT name FROM companies WHERE id=$companyId")->fetch();
    return ['name'=>$row['name'], 'services'=> $db->query("SELECT name FROM service_types WHERE company_id=$companyId")->fetchAll(PDO::FETCH_COLUMN)];
  }
  public function getCustomerContext(int $conversationId): array { /* last 3 service_records like AiController.php:604 */ return []; }
  public function getCompanyKnowledge(int $companyId, string $query, int $k=5): array { /* FTS over ai_assistant_knowledge */ return []; }
  public function canAutoReply(int $conversationId): bool { return true; } // business hours, opt-in
}

use ExcelleInsights\AiAssistant\Support\LlmFactory;

$ai = new \ExcelleInsights\AiAssistant\Facade\AiAssistantManager(
  pdo: \ExcelleCore\Core\Database::getInstance()->getConnection(),
  contextProvider: new MaintainaContextProvider(),
  openAI: LlmFactory::make() // or new \ExcelleCore\AI\OpenAIClient() to reuse host's
);

// Host side, e.g. api/src/Services/AppBookingHandler.php
use ExcelleInsights\AiAssistant\Contracts\BookingHandlerInterface;
use ExcelleInsights\AiAssistant\Support\BookingIntent;

class AppBookingHandler implements BookingHandlerInterface {
  public function handleBookingIntent(BookingIntent $intent): void {
    // $intent->companyId, ->contactPhone, ->contactName, ->service,
    // ->preferredDate (Y-m-d or null), ->confidence, ->messageBody, ->aiReply
    // De-dupe + insert into YOUR bookings table + notify staff here.
  }
}

$ai = new \ExcelleInsights\AiAssistant\Services\AiAssistantService(
  openAI: $llm,
  bookingHandler: new AppBookingHandler(),   // or:
);
$ai->setBookingHandler(new AppBookingHandler());
$ai->onInboundMessages($results);

use ExcelleInsights\AiAssistant\Services\KnowledgeService;
use ExcelleInsights\AiAssistant\Services\TrainingService;

$knowledge = new KnowledgeService($pdo, LlmFactory::make());
$training = new TrainingService($pdo, $knowledge);
$training->ingest($companyId, "Return Policy", "30 days...", "manual");

return [
  'table_prefix' => $_ENV['AI_ASSISTANT_TABLE_PREFIX'] ?? $_ENV['AI_WHATSAPP_TABLE_PREFIX'] ?? 'ai_assistant',
  'llm' => [ // canonical — any OpenAI-compatible provider (see Support\LlmFactory)
    'provider' => $_ENV['LLM_PROVIDER'] ?? 'openai',
    'api_key' => $_ENV['LLM_API_KEY'] ?? $_ENV['OPENAI_API_KEY'] ?? '',
    'base_url' => $_ENV['LLM_BASE_URL'] ?? $_ENV['OPENAI_BASE_URL'] ?? null,
    'model' => $_ENV['LLM_MODEL'] ?? $_ENV['OPENAI_MODEL'] ?? 'gpt-4o',
    'embedding_model' => $_ENV['LLM_EMBEDDING_MODEL'] ?? 'text-embedding-3-small',
    'timeout' => (int)($_ENV['LLM_REQUEST_TIMEOUT'] ?? $_ENV['OPENAI_REQUEST_TIMEOUT'] ?? 30),
    'whisper_model' => $_ENV['OPENAI_WHISPER_MODEL'] ?? 'whisper-1',
  ],
  'auto_reply' => [
    'enabled' => ($_ENV['AI_ASSISTANT_AUTO_REPLY'] ?? $_ENV['AI_WHATSAPP_AUTO_REPLY'] ?? 'true') !== 'false',
    'outside_session_use_template' => true, // 24h window → queue template
    'fallback_template_id' => (int)($_ENV['AI_WHATSAPP_FALLBACK_TEMPLATE'] ?? 0),
  ],
];