src/Entity/AiRequest.php line 17

  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Repository\AiRequestRepository;
  5. use Doctrine\DBAL\Types\Types;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Serializer\Annotation\Groups;
  8. #[ORM\Entity(repositoryClassAiRequestRepository::class)]
  9. #[ApiResource(
  10.     normalizationContext: ['groups' => ['read','thread_ai_request']],
  11.     denormalizationContext: ['groups' => ['write','thread_ai_request']],
  12.     
  13.     )]
  14. class AiRequest
  15. {
  16.     #[Groups(['read','thread_ai_request'])]
  17.     #[ORM\Id]
  18.     #[ORM\GeneratedValue]
  19.     #[ORM\Column]
  20.     private ?int $id null;
  21.     #[Groups(['read','thread_ai_request''write'])]
  22.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  23.     private ?string $question null;
  24.     #[Groups(['read''write'])]
  25.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  26.     private ?\DateTimeInterface $createdAt null;
  27.     #[Groups(['read''write'])]
  28.     #[ORM\ManyToOne(inversedBy'aiRequests')]
  29.     private ?User $user null;
  30.     #[Groups(['read','thread_ai_request''write'])]
  31.     #[ORM\Column(length255nullabletrue)]
  32.     private ?string $namespace null;
  33.     #[Groups(['read','thread_ai_request''write'])]
  34.     #[ORM\Column(length255nullabletrue)]
  35.     private ?string $entity null;
  36.     #[ORM\Column(nullabletrue)]
  37.     private ?int $tokensUsed null;
  38.     #[ORM\Column(nullabletrue)]
  39.     private ?int $promptTokens null;
  40.     #[ORM\Column(nullabletrue)]
  41.     private ?int $completionTokens null;
  42.     #[ORM\Column(length255nullabletrue)]
  43.     private ?string $requestId null;
  44.     #[ORM\Column(nullabletrue)]
  45.     private ?int $createdNumber null;
  46.     #[ORM\Column(length255nullabletrue)]
  47.     private ?string $modelUsed null;
  48.     #[ORM\Column(length255nullabletrue)]
  49.     private ?string $handledEntity null;
  50.     #[ORM\Column(nullabletrue)]
  51.     private ?int $handledEntityId null;
  52.     #[Groups(['read''write'])]
  53.     #[ORM\ManyToOne(inversedBy'aiRequests',cascade:["persist"])]
  54.     private ?ThreadAiRequest $threadAiRequest null;
  55.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  56.     private ?string $jsonRequest null;
  57.     
  58.     #[ORM\Column(typeTypes::TEXTnullabletrue)]
  59.     private ?string $jsonResponse null;
  60.     #[Groups(['read''write'])]
  61.     #[ORM\Column(length255nullabletrue)]
  62.     private ?string $status null;
  63.     #[Groups(['read','thread_ai_request''write'])]
  64.     #[ORM\Column(length255nullabletrue)]
  65.     private ?string $action null;
  66.     public function __toString()
  67.     {
  68.         $status $this->status $this->status 'none';
  69.         return (string)("#".$this->id."-".$this->question."(status:".$status.")");
  70.     }
  71.     public function getId(): ?int
  72.     {
  73.         return $this->id;
  74.     }
  75.     public function getQuestion(): ?string
  76.     {
  77.         return $this->question;
  78.     }
  79.     public function setQuestion(?string $question): static
  80.     {
  81.         $this->question $question;
  82.         return $this;
  83.     }
  84.     public function getCreatedAt(): ?\DateTimeInterface
  85.     {
  86.         return $this->createdAt;
  87.     }
  88.     public function setCreatedAt(?\DateTimeInterface $createdAt): static
  89.     {
  90.         $this->createdAt $createdAt;
  91.         return $this;
  92.     }
  93.     public function getUser(): ?User
  94.     {
  95.         return $this->user;
  96.     }
  97.     public function setUser(?User $user): static
  98.     {
  99.         $this->user $user;
  100.         return $this;
  101.     }
  102.     public function getNamespace(): ?string
  103.     {
  104.         return $this->namespace;
  105.     }
  106.     public function setNamespace(?string $namespace): static
  107.     {
  108.         $this->namespace $namespace;
  109.         return $this;
  110.     }
  111.     public function getEntity(): ?string
  112.     {
  113.         return $this->entity;
  114.     }
  115.     public function setEntity(?string $entity): static
  116.     {
  117.         $this->entity $entity;
  118.         return $this;
  119.     }
  120.     public function getTokensUsed(): ?int
  121.     {
  122.         return $this->tokensUsed;
  123.     }
  124.     public function setTokensUsed(?int $tokensUsed): static
  125.     {
  126.         $this->tokensUsed $tokensUsed;
  127.         return $this;
  128.     }
  129.     public function getPromptTokens(): ?int
  130.     {
  131.         return $this->promptTokens;
  132.     }
  133.     public function setPromptTokens(?int $promptTokens): static
  134.     {
  135.         $this->promptTokens $promptTokens;
  136.         return $this;
  137.     }
  138.     public function getCompletionTokens(): ?int
  139.     {
  140.         return $this->completionTokens;
  141.     }
  142.     public function setCompletionTokens(?int $completionTokens): static
  143.     {
  144.         $this->completionTokens $completionTokens;
  145.         return $this;
  146.     }
  147.     public function getRequestId(): ?string
  148.     {
  149.         return $this->requestId;
  150.     }
  151.     public function setRequestId(?string $requestId): static
  152.     {
  153.         $this->requestId $requestId;
  154.         return $this;
  155.     }
  156.     public function getCreatedNumber(): ?int
  157.     {
  158.         return $this->createdNumber;
  159.     }
  160.     public function setCreatedNumber(?int $createdNumber): static
  161.     {
  162.         $this->createdNumber $createdNumber;
  163.         return $this;
  164.     }
  165.     public function getModelUsed(): ?string
  166.     {
  167.         return $this->modelUsed;
  168.     }
  169.     public function setModelUsed(?string $modelUsed): static
  170.     {
  171.         $this->modelUsed $modelUsed;
  172.         return $this;
  173.     }
  174.     public function getHandledEntity(): ?string
  175.     {
  176.         return $this->handledEntity;
  177.     }
  178.     public function setHandledEntity(?string $handledEntity): static
  179.     {
  180.         $this->handledEntity $handledEntity;
  181.         return $this;
  182.     }
  183.     public function getHandledEntityId(): ?int
  184.     {
  185.         return $this->handledEntityId;
  186.     }
  187.     public function setHandledEntityId(?int $handledEntityId): static
  188.     {
  189.         $this->handledEntityId $handledEntityId;
  190.         return $this;
  191.     }
  192.     public function getThreadAiRequest(): ?ThreadAiRequest
  193.     {
  194.         return $this->threadAiRequest;
  195.     }
  196.     public function setThreadAiRequest(?ThreadAiRequest $threadAiRequest): static
  197.     {
  198.         $this->threadAiRequest $threadAiRequest;
  199.         return $this;
  200.     }
  201.     #[Groups(['read''write'])]
  202.     public function getAiResponse(){
  203.         $data null;
  204.         if (!empty($this->getJsonResponse())){
  205.             
  206.                 
  207.                     $data $this->getJsonResponse();
  208.               
  209.         }
  210.         return json_encode($data);
  211.     }
  212.     #[Groups(['read''write'])]
  213.     public function getFunctionCall(){
  214.         $data "Nothing to generate";
  215.         if (!empty($this->getJsonResponse())){
  216.             
  217.             //dd($this->getJsonResponse());
  218.             if(!array_key_exists("choices",$this->getJsonResponse()) and !array_key_exists("candidates",$this->getJsonResponse())){
  219.                 return  ["text"=>$data];
  220.             } 
  221.             //Gemini Google AI
  222.             if(array_key_exists("candidates",$this->getJsonResponse())){
  223.                 foreach ($this->getJsonResponse()["candidates"] as $result) {
  224.                     //
  225.                     
  226.                     
  227.     
  228.                     if(array_key_exists("parts",$result["content"])){
  229.                         //dd($result);
  230.                             if($result["content"]["parts"][0]["text"]){
  231.                                 $data $result["content"]["parts"][0]["text"];
  232.                                 return  ["text"=>$data];
  233.                             }
  234.     
  235.                             if($result["content"]["parts"][0]["functionCall"]){
  236.                                 $data $result["content"]["parts"][0]["functionCall"];
  237.                                 return  ["text"=>$data];
  238.                             }
  239.                             
  240.                         
  241.                     }
  242.                    
  243.                     
  244.                         $data ="Nothing generated";
  245.                         return  ["text"=>$data];
  246.                         //break;
  247.                     
  248.     
  249.                     
  250.     
  251.     
  252.                 }
  253.             }
  254.             
  255.             if(array_key_exists("choices",$this->getJsonResponse())){
  256.                 foreach ($this->getJsonResponse()["choices"] as $result) {
  257.                     //
  258.                     
  259.     
  260.                     if(array_key_exists("functionCall",$result["message"])){
  261.                         //dd($result);
  262.                             if($result["message"]["functionCall"]){
  263.                                 $data $result["message"]["functionCall"]["arguments"];
  264.                                 return  json_decode($this->convertToUTF8($data),true);
  265.                             }
  266.                             
  267.                         
  268.                     }
  269.                    
  270.                     if(array_key_exists("content",$result["message"])){
  271.                         //dd($result);
  272.                         if($result["message"]["content"]){
  273.                             
  274.                             $data $result["message"]["content"];
  275.                             return  json_decode($this->convertToUTF8($data),true);
  276.                             
  277.                         }
  278.                         
  279.                     }
  280.                     // if(array_key_exists("toolCalls",$result["message"])){
  281.                     //     //dd($result);
  282.                         
  283.                     //         $data = $result["message"]["toolCalls"][0]["function"]["arguments"];
  284.                     //         return  $data;
  285.                     //         //break;
  286.                         
  287.                     // }
  288.                     
  289.                     $data ="Nothing generated";
  290.                     return  ["text"=>$data];
  291.                         //break;
  292.                     
  293.     
  294.                     
  295.     
  296.                     // $result->index; // 0
  297.                     // $result->message->role; // 'assistant'
  298.                     // $result->message->content; // null
  299.                     // $result->message->toolCalls[0]->id; // 'call_123'
  300.                     // $result->message->toolCalls[0]->type; // 'function'
  301.                     // $result->message->toolCalls[0]->function->name; // 'get_current_weather'
  302.                     // $result->finishReason; // 'tool_calls'
  303.                 }
  304.             }
  305.              
  306.               
  307.         }
  308.         return ["text"=>$data];
  309.     }
  310.     public function getFunctionCallToPrecise(){
  311.         $data "Nothing to generate";
  312.         if (!empty($this->getJsonResponse())){
  313.             
  314.             //dd($this->getJsonResponse());
  315.             if(!array_key_exists("choices",$this->getJsonResponse()) and !array_key_exists("candidates",$this->getJsonResponse())){
  316.                 return  ["text"=>$data];
  317.             } 
  318.             //Gemini Google AI
  319.             if(array_key_exists("candidates",$this->getJsonResponse())){
  320.                 foreach ($this->getJsonResponse()["candidates"] as $result) {
  321.                     //
  322.                     
  323.                     
  324.     
  325.                     if(array_key_exists("parts",$result["content"])){
  326.                         //dd($result);
  327.                             if($result["content"]["parts"][0]["text"]){
  328.                                 $data $result["content"]["parts"][0]["text"];
  329.                                 return  ["text"=>$data];
  330.                             }
  331.     
  332.                             if($result["content"]["parts"][0]["functionCall"]){
  333.                                 $data $result["content"]["parts"][0]["functionCall"];
  334.                                 return  ["functionCall"=>$data];
  335.                             }
  336.                             
  337.                         
  338.                     }
  339.                    
  340.                     
  341.                         $data ="Nothing generated";
  342.                         return  ["text"=>$data];
  343.                         //break;
  344.                     
  345.     
  346.                     
  347.     
  348.     
  349.                 }
  350.             }
  351.             
  352.             if(array_key_exists("choices",$this->getJsonResponse())){
  353.                 foreach ($this->getJsonResponse()["choices"] as $result) {
  354.                     //
  355.                     
  356.     
  357.                     if(array_key_exists("functionCall",$result["message"])){
  358.                         //dd($result);
  359.                             if($result["message"]["functionCall"]){
  360.                                 $data $result["message"]["functionCall"]["arguments"];
  361.                                 return  $data;
  362.                             }
  363.                             
  364.                         
  365.                     }
  366.                    
  367.                     if(array_key_exists("content",$result["message"])){
  368.                         //dd($result);
  369.                         if($result["message"]["content"]){
  370.                             
  371.                             $data $result["message"]["content"];
  372.                             return  $data;
  373.                             
  374.                         }
  375.                         
  376.                     }
  377.                     
  378.                     $data ="Nothing generated";
  379.                     return  ["text"=>$data];
  380.                 }
  381.             }
  382.              
  383.               
  384.         }
  385.         return ["text"=>$data];
  386.     }
  387.     #[Groups(['read''write'])]
  388.     public function getFunctionName(){
  389.         $data null;
  390.         // if (!empty($this->getJsonResponse())){
  391.             
  392.         //     if(!array_key_exists("choices",$this->getJsonResponse())){
  393.         //         return null;
  394.         //     }   
  395.         //      foreach ($this->getJsonResponse()["choices"] as $result) {
  396.                 
  397.         //         $data = $result["message"]["toolCalls"][0]["function"]["name"]; // "{\n  \"location\": \"Boston, MA\"\n}"
  398.         //         // $result->index; // 0
  399.         //         // $result->message->role; // 'assistant'
  400.         //         // $result->message->content; // null
  401.         //         // $result->message->toolCalls[0]->id; // 'call_123'
  402.         //         // $result->message->toolCalls[0]->type; // 'function'
  403.         //         // $result->message->toolCalls[0]->function->name; // 'get_current_weather'
  404.         //         // $result->finishReason; // 'tool_calls'
  405.         //     }
  406.               
  407.         // }
  408.         return $data;
  409.     }
  410.     #[Groups(['read''write'])]
  411.     public function getToolCallId(){
  412.         $data null;
  413.         // if (!empty($this->getJsonResponse())){
  414.             
  415.         //     if(!array_key_exists("choices",$this->getJsonResponse())){
  416.         //         return null;
  417.         //     }   
  418.         //      foreach ($this->getJsonResponse()["choices"] as $result) {
  419.         //         if(isset($result["message"]["content"])){
  420.         //             $data = $result["message"]["content"];
  421.         //         }else{
  422.         //             $data = $result["message"]["toolCalls"][0]["id"];
  423.         //         }
  424.         //          // "{\n  \"location\": \"Boston, MA\"\n}"
  425.         //         // $result->index; // 0
  426.         //         // $result->message->role; // 'assistant'
  427.         //         // $result->message->content; // null
  428.         //         // $result->message->toolCalls[0]->id; // 'call_123'
  429.         //         // $result->message->toolCalls[0]->type; // 'function'
  430.         //         // $result->message->toolCalls[0]->function->name; // 'get_current_weather'
  431.         //         // $result->finishReason; // 'tool_calls'
  432.         //     }
  433.               
  434.         // }
  435.         return $data;
  436.     }
  437.     #[Groups(['read''write'])]
  438.     public function getFunctionCallEscaped(){
  439.         //$data = $this->convertToUTF8((string)$this->getFunctionCall());
  440.         $data $this->getFunctionCall();
  441.         return $data;
  442.     }
  443.    
  444.     public function getJsonRequest(): ?string
  445.     {
  446.         return $this->jsonRequest;
  447.     }
  448.     public function setJsonRequest(?string $jsonRequest): static
  449.     {
  450.         $this->jsonRequest $jsonRequest;
  451.         return $this;
  452.     }
  453.     public function getJsonResponse(): ?array
  454.     {
  455.         return json_decode($this->jsonResponse,true);
  456.     }
  457.     public function setJsonResponse(?string $jsonResponse): static
  458.     {
  459.         $this->jsonResponse $jsonResponse;
  460.         return $this;
  461.     }
  462.     public function getStatus(): ?string
  463.     {
  464.         return $this->status;
  465.     }
  466.     public function setStatus(?string $status): static
  467.     {
  468.         $this->status $status;
  469.         return $this;
  470.     }
  471.     public function getExecuteFunctionCall()
  472.     {
  473.         return "executeFunctionCall";
  474.     }
  475.     public function getAction(): ?string
  476.     {
  477.         return $this->action;
  478.     }
  479.     public function setAction(?string $action): static
  480.     {
  481.         $this->action $action;
  482.         return $this;
  483.     }
  484.     function convertToUTF8($input) {
  485.         if (is_array($input)) {
  486.             foreach ($input as $key => $value) {
  487.                 $input[$key] = $this->convertToUTF8($value);
  488.             }
  489.         } elseif (is_string($input)) {
  490.             return mb_convert_encoding($input'UTF-8''UTF-8');
  491.         }
  492.         return $input;
  493.     }
  494. }