chatwithai.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. helper::import('../../control.php');
  3. class myIm extends im
  4. {
  5. public function chatWithAi($modelId, $assistantId, $userID = 0, $version = '', $device = 'desktop')
  6. {
  7. $user = $this->im->user->getByID($userID);
  8. $user->rights = $this->loadModel('user')->authorize($user->account);
  9. global $app;
  10. $app->user = $user;
  11. $chatGid = "$userID&ai-{$modelId}";
  12. $aiChatPermission = commonModel::hasPriv('ai', 'chat');
  13. if(!$aiChatPermission) return false;
  14. $chat = $this->im->chat->getByGid($chatGid, false, false);
  15. if(!$chat) return false;
  16. $aiModel = $this->loadModel('ai')->getLanguageModel($modelId);
  17. if(!$aiModel || $aiModel->enabled == 0) return false;
  18. $context = $this->im->getAiChatLatestContext($modelId, $userID);
  19. $context = array_map(function($message) use ($userID) {
  20. return (object)array(
  21. 'role' => $message->user == $userID ? 'user' : 'assistant',
  22. 'content' => $message->content,
  23. );
  24. }, $context);
  25. if(!empty($assistantId))
  26. {
  27. $assistant = $this->ai->getAssistantById($assistantId);
  28. if($assistant)
  29. {
  30. array_unshift($context, (object)array(
  31. 'role' => 'system',
  32. 'content' => $assistant->systemMessage,
  33. ));
  34. }
  35. }
  36. $messages = $this->ai->converse($modelId, $context);
  37. if(!$messages)
  38. {
  39. $statusOutput = new stdclass();
  40. $statusOutput->result = 'success';
  41. $statusOutput->method = 'chatwithai';
  42. $statusOutput->users = array($userID);
  43. $statusOutput->data = (object)array('status' => 'error', 'modelId' => $modelId);
  44. return $this->im->sendOutputGroup(array($statusOutput));
  45. }
  46. $replyMessage = new stdclass();
  47. $replyMessage->gid = imModel::createGID();
  48. $replyMessage->cgid = $chatGid;
  49. $replyMessage->user = "ai-{$modelId}";
  50. $replyMessage->content = end($messages);
  51. $replyMessage->type = 'normal';
  52. $replyMessage->contentType = 'text';
  53. $sender = new stdclass();
  54. $sender->id = 0;
  55. $sender->displayName = empty($aiModel->name) ? $this->lang->ai->models->typeList[$aiModel->type] : $aiModel->name;
  56. if(!empty($assistant))
  57. {
  58. $sender->displayName = $assistant->name;
  59. $sender->aiAssistantIcon = $assistant->icon;
  60. }
  61. $replyMessage->data = new stdclass();
  62. $replyMessage->data->sender = $sender;
  63. $replyMessage->data = json_encode($replyMessage->data);
  64. $chatMessages = $this->im->messageCreate(array($replyMessage), $userID);
  65. $chatOutput = new stdclass();
  66. $chatOutput->result = 'success';
  67. $chatOutput->method = 'messagesend';
  68. $chatOutput->users = array($userID);
  69. $chatOutput->data = $chatMessages;
  70. $statusOutput = new stdclass();
  71. $statusOutput->result = 'success';
  72. $statusOutput->method = 'chatwithai';
  73. $statusOutput->users = array($userID);
  74. $statusOutput->data = (object)array('status' => 'success', 'modelId' => $modelId);
  75. return $this->im->sendOutputGroup(array($statusOutput, $chatOutput));
  76. }
  77. }