BxDolAiAgent.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. use NeuronAI\RAG\RAG;
  10. use NeuronAI\Observability\InspectorObserver;
  11. use NeuronAI\Observability\LogObserver;
  12. class BxDolAiAgent extends RAG
  13. {
  14. public function __construct(protected array $aAgent, protected array $aParams = [])
  15. {
  16. parent::__construct();
  17. $sKey = getParam('sys_agents_inspector_key');
  18. if ($sKey) {
  19. $logger = InspectorObserver::instance(
  20. key: $sKey,
  21. autoFlush: true,
  22. );
  23. $this->observe($logger);
  24. }
  25. else {
  26. $logger = new BxDolLoggerDb('sys_agents_' . $this->aAgent['id']);
  27. $this->observe(new LogObserver($logger));
  28. }
  29. }
  30. protected function provider(): NeuronAI\Providers\AIProviderInterface
  31. {
  32. return BxDolAIModelFactory::getModelInstance($this->aAgent['model_id']);
  33. }
  34. protected function instructions(): string
  35. {
  36. $aPromptSystem = !empty($this->aAgent['prompt_system']) ? [$this->aAgent['prompt_system']] : [];
  37. $aPromptSteps = !empty($this->aAgent['prompt_steps']) ? [$this->aAgent['prompt_steps']] : [];
  38. $aPromptTools = !empty($this->aAgent['prompt_tools']) ? [$this->aAgent['prompt_tools']] : [];
  39. $aPromptOutput = !empty($this->aAgent['prompt_output']) ? [$this->aAgent['prompt_output']] : [];
  40. $aPromptTools[] = "Always use agent profile id = {$this->aAgent['profile_id']} as author ('profile_id' tool parameter), unless explicitly specified in the user instructions, no other variants strictly.";
  41. if ('alert' == $this->aAgent['trigger']) {
  42. $aPromptTools[] = "Return array only, modified version of 'extra' array. Modifyable keys: " . $this->getAlertTriggerModifyableKeys() . ".";
  43. $sDesc = trim(BxDolAiQuery::getAlertDesc($this->aAgent['alert']));
  44. if ('.' != mb_substr($sDesc, -1))
  45. $sDesc .= '.';
  46. $aPromptSystem[] = $sDesc;
  47. }
  48. $oPrompt = new NeuronAI\Agent\SystemPrompt(
  49. background: $aPromptSystem,
  50. steps: $aPromptSteps,
  51. output: $aPromptOutput,
  52. toolsUsage: $aPromptTools
  53. );
  54. return (string) $oPrompt;
  55. }
  56. /**
  57. * @return \NeuronAI\Tools\ToolInterface[]
  58. */
  59. protected function tools(): array
  60. {
  61. if ($this->aAgent['tools']) {
  62. $aTools = explode(',', $this->aAgent['tools']);
  63. $aToolInstances = [];
  64. foreach ($aTools as $iToolId) {
  65. $oTool = BxDolAIToolFactory::getToolInstance($iToolId);
  66. $aToolInstances[] = $oTool;
  67. }
  68. return $aToolInstances;
  69. }
  70. else {
  71. return [];
  72. }
  73. }
  74. protected function vectorStore(): NeuronAI\RAG\VectorStore\VectorStoreInterface
  75. {
  76. if ($this->aAgent['vector_store_id'])
  77. return BxDolAIVectorStoreFactory::getVectorStoreInstance($this->aAgent['vector_store_id']);
  78. else
  79. return new NeuronAI\RAG\VectorStore\MemoryVectorStore();
  80. }
  81. protected function chatHistory(): NeuronAI\Chat\History\ChatHistoryInterface
  82. {
  83. if ($this->aAgent['chat_history_context']) {
  84. return new NeuronAI\Chat\History\SQLChatHistory(
  85. thread_id: $this->getСhatHistoryThreadId(),
  86. pdo: BxDolDb::getInstance()->getLink(),
  87. table: 'sys_agents_chat_history',
  88. contextWindow: $this->aAgent['chat_history_context']
  89. );
  90. }
  91. else {
  92. return new NeuronAI\Chat\History\InMemoryChatHistory(
  93. contextWindow: 50000
  94. );
  95. }
  96. }
  97. protected function getСhatHistoryThreadId(): string
  98. {
  99. $s = $this->aAgent['trigger'] . ':' . $this->aAgent['id'];
  100. if (isset($this->aParams['chat_history_subindex']))
  101. $s .= ':' . $this->aParams['chat_history_subindex'];
  102. return $s;
  103. }
  104. protected function getAlertTriggerModifyableKeys(): string
  105. {
  106. if ('alert' != $this->aAgent['trigger'])
  107. return 'none';
  108. $aAlert = BxDolAiQuery::getAlert($this->aAgent['alert']);
  109. if (!$aAlert)
  110. return 'none';
  111. $aKeys = json_decode($aAlert['extra_refs']);
  112. if (!$aKeys)
  113. return 'none';
  114. return implode(',', $aKeys);
  115. }
  116. }