BxDolAIProviderUnaProxy.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolAIProviderUnaProxy extends NeuronAI\Providers\OpenAI\OpenAI
  10. {
  11. protected string $baseUri = 'https://api.unacms.com/m/ai_proxy/v1';
  12. /**
  13. * null reads the AI Proxy setting. A string, including empty, is used as given.
  14. */
  15. public function __construct(
  16. string $sKey,
  17. string $sSecret,
  18. string $sModel,
  19. ?NeuronAI\HttpClient\HttpClientInterface $httpClient = null,
  20. protected ?string $sPrompt = null,
  21. ) {
  22. if ($sKey === '' || $sSecret === '')
  23. throw new Exception(_t('_sys_agents_una_proxy_unconfigured'));
  24. parent::__construct(
  25. key: '',
  26. model: $sModel,
  27. parameters: [],
  28. strict_response: false,
  29. httpClient: $httpClient ?? new BxDolAIUnaProxyHttpClient($sKey, $sSecret),
  30. );
  31. }
  32. public function systemPrompt(?string $prompt): NeuronAI\Providers\AIProviderInterface
  33. {
  34. $sOperator = trim($this->operatorPrompt());
  35. if ($sOperator !== '') {
  36. $sClient = trim((string)$prompt);
  37. $prompt = $sClient === '' ? $sOperator : $sOperator . "\n\n" . $sClient;
  38. }
  39. return parent::systemPrompt($prompt);
  40. }
  41. protected function operatorPrompt(): string
  42. {
  43. if ($this->sPrompt !== null)
  44. return $this->sPrompt;
  45. return (string)getParam('bx_ai_proxy_prompt');
  46. }
  47. }
  48. /** @} */