request.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup AIProxy AI Proxy
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. $aRequest = array_values(array_filter($GLOBALS['aRequest'] ?? [], static function ($sPart) {
  12. return $sPart !== '';
  13. }));
  14. echoDbgLog($aRequest);
  15. $bEnabled = !empty($GLOBALS['aModule']['enabled']);
  16. $bPost = ($_SERVER['REQUEST_METHOD'] ?? '') === 'POST';
  17. $bCompletion = $bPost && $aRequest === ['v1', 'chat', 'completions'] && $bEnabled;
  18. $bKnowledge = $bPost && $aRequest === ['v1', 'knowledge', 'search'] && $bEnabled;
  19. if (!$bCompletion && !$bKnowledge) {
  20. http_response_code(404);
  21. header('Content-Type: application/json; charset=utf-8');
  22. echo json_encode([
  23. 'error' => [
  24. 'message' => 'Not found',
  25. 'type' => 'not_found',
  26. ],
  27. ]);
  28. return;
  29. }
  30. $oModule = BxDolModule::getInstance($GLOBALS['aModule']['name']);
  31. if (!$oModule)
  32. return;
  33. if ($bKnowledge)
  34. $oModule->knowledge();
  35. else
  36. $oModule->completions();
  37. /** @} */