src/Entity/AiRequest.php line 17
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\AiRequestRepository;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: AiRequestRepository::class)]
#[ApiResource(
normalizationContext: ['groups' => ['read','thread_ai_request']],
denormalizationContext: ['groups' => ['write','thread_ai_request']],
)]
class AiRequest
{
#[Groups(['read','thread_ai_request'])]
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[Groups(['read','thread_ai_request', 'write'])]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $question = null;
#[Groups(['read', 'write'])]
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
private ?\DateTimeInterface $createdAt = null;
#[Groups(['read', 'write'])]
#[ORM\ManyToOne(inversedBy: 'aiRequests')]
private ?User $user = null;
#[Groups(['read','thread_ai_request', 'write'])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $namespace = null;
#[Groups(['read','thread_ai_request', 'write'])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $entity = null;
#[ORM\Column(nullable: true)]
private ?int $tokensUsed = null;
#[ORM\Column(nullable: true)]
private ?int $promptTokens = null;
#[ORM\Column(nullable: true)]
private ?int $completionTokens = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $requestId = null;
#[ORM\Column(nullable: true)]
private ?int $createdNumber = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $modelUsed = null;
#[ORM\Column(length: 255, nullable: true)]
private ?string $handledEntity = null;
#[ORM\Column(nullable: true)]
private ?int $handledEntityId = null;
#[Groups(['read', 'write'])]
#[ORM\ManyToOne(inversedBy: 'aiRequests',cascade:["persist"])]
private ?ThreadAiRequest $threadAiRequest = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $jsonRequest = null;
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $jsonResponse = null;
#[Groups(['read', 'write'])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $status = null;
#[Groups(['read','thread_ai_request', 'write'])]
#[ORM\Column(length: 255, nullable: true)]
private ?string $action = null;
public function __toString()
{
$status = $this->status ? $this->status : 'none';
return (string)("#".$this->id."-".$this->question."(status:".$status.")");
}
public function getId(): ?int
{
return $this->id;
}
public function getQuestion(): ?string
{
return $this->question;
}
public function setQuestion(?string $question): static
{
$this->question = $question;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): static
{
$this->user = $user;
return $this;
}
public function getNamespace(): ?string
{
return $this->namespace;
}
public function setNamespace(?string $namespace): static
{
$this->namespace = $namespace;
return $this;
}
public function getEntity(): ?string
{
return $this->entity;
}
public function setEntity(?string $entity): static
{
$this->entity = $entity;
return $this;
}
public function getTokensUsed(): ?int
{
return $this->tokensUsed;
}
public function setTokensUsed(?int $tokensUsed): static
{
$this->tokensUsed = $tokensUsed;
return $this;
}
public function getPromptTokens(): ?int
{
return $this->promptTokens;
}
public function setPromptTokens(?int $promptTokens): static
{
$this->promptTokens = $promptTokens;
return $this;
}
public function getCompletionTokens(): ?int
{
return $this->completionTokens;
}
public function setCompletionTokens(?int $completionTokens): static
{
$this->completionTokens = $completionTokens;
return $this;
}
public function getRequestId(): ?string
{
return $this->requestId;
}
public function setRequestId(?string $requestId): static
{
$this->requestId = $requestId;
return $this;
}
public function getCreatedNumber(): ?int
{
return $this->createdNumber;
}
public function setCreatedNumber(?int $createdNumber): static
{
$this->createdNumber = $createdNumber;
return $this;
}
public function getModelUsed(): ?string
{
return $this->modelUsed;
}
public function setModelUsed(?string $modelUsed): static
{
$this->modelUsed = $modelUsed;
return $this;
}
public function getHandledEntity(): ?string
{
return $this->handledEntity;
}
public function setHandledEntity(?string $handledEntity): static
{
$this->handledEntity = $handledEntity;
return $this;
}
public function getHandledEntityId(): ?int
{
return $this->handledEntityId;
}
public function setHandledEntityId(?int $handledEntityId): static
{
$this->handledEntityId = $handledEntityId;
return $this;
}
public function getThreadAiRequest(): ?ThreadAiRequest
{
return $this->threadAiRequest;
}
public function setThreadAiRequest(?ThreadAiRequest $threadAiRequest): static
{
$this->threadAiRequest = $threadAiRequest;
return $this;
}
#[Groups(['read', 'write'])]
public function getAiResponse(){
$data = null;
if (!empty($this->getJsonResponse())){
$data = $this->getJsonResponse();
}
return json_encode($data);
}
#[Groups(['read', 'write'])]
public function getFunctionCall(){
$data = "Nothing to generate";
if (!empty($this->getJsonResponse())){
//dd($this->getJsonResponse());
if(!array_key_exists("choices",$this->getJsonResponse()) and !array_key_exists("candidates",$this->getJsonResponse())){
return ["text"=>$data];
}
//Gemini Google AI
if(array_key_exists("candidates",$this->getJsonResponse())){
foreach ($this->getJsonResponse()["candidates"] as $result) {
//
if(array_key_exists("parts",$result["content"])){
//dd($result);
if($result["content"]["parts"][0]["text"]){
$data = $result["content"]["parts"][0]["text"];
return ["text"=>$data];
}
if($result["content"]["parts"][0]["functionCall"]){
$data = $result["content"]["parts"][0]["functionCall"];
return ["text"=>$data];
}
}
$data ="Nothing generated";
return ["text"=>$data];
//break;
}
}
if(array_key_exists("choices",$this->getJsonResponse())){
foreach ($this->getJsonResponse()["choices"] as $result) {
//
if(array_key_exists("functionCall",$result["message"])){
//dd($result);
if($result["message"]["functionCall"]){
$data = $result["message"]["functionCall"]["arguments"];
return json_decode($this->convertToUTF8($data),true);
}
}
if(array_key_exists("content",$result["message"])){
//dd($result);
if($result["message"]["content"]){
$data = $result["message"]["content"];
return json_decode($this->convertToUTF8($data),true);
}
}
// if(array_key_exists("toolCalls",$result["message"])){
// //dd($result);
// $data = $result["message"]["toolCalls"][0]["function"]["arguments"];
// return $data;
// //break;
// }
$data ="Nothing generated";
return ["text"=>$data];
//break;
// $result->index; // 0
// $result->message->role; // 'assistant'
// $result->message->content; // null
// $result->message->toolCalls[0]->id; // 'call_123'
// $result->message->toolCalls[0]->type; // 'function'
// $result->message->toolCalls[0]->function->name; // 'get_current_weather'
// $result->finishReason; // 'tool_calls'
}
}
}
return ["text"=>$data];
}
public function getFunctionCallToPrecise(){
$data = "Nothing to generate";
if (!empty($this->getJsonResponse())){
//dd($this->getJsonResponse());
if(!array_key_exists("choices",$this->getJsonResponse()) and !array_key_exists("candidates",$this->getJsonResponse())){
return ["text"=>$data];
}
//Gemini Google AI
if(array_key_exists("candidates",$this->getJsonResponse())){
foreach ($this->getJsonResponse()["candidates"] as $result) {
//
if(array_key_exists("parts",$result["content"])){
//dd($result);
if($result["content"]["parts"][0]["text"]){
$data = $result["content"]["parts"][0]["text"];
return ["text"=>$data];
}
if($result["content"]["parts"][0]["functionCall"]){
$data = $result["content"]["parts"][0]["functionCall"];
return ["functionCall"=>$data];
}
}
$data ="Nothing generated";
return ["text"=>$data];
//break;
}
}
if(array_key_exists("choices",$this->getJsonResponse())){
foreach ($this->getJsonResponse()["choices"] as $result) {
//
if(array_key_exists("functionCall",$result["message"])){
//dd($result);
if($result["message"]["functionCall"]){
$data = $result["message"]["functionCall"]["arguments"];
return $data;
}
}
if(array_key_exists("content",$result["message"])){
//dd($result);
if($result["message"]["content"]){
$data = $result["message"]["content"];
return $data;
}
}
$data ="Nothing generated";
return ["text"=>$data];
}
}
}
return ["text"=>$data];
}
#[Groups(['read', 'write'])]
public function getFunctionName(){
$data = null;
// if (!empty($this->getJsonResponse())){
// if(!array_key_exists("choices",$this->getJsonResponse())){
// return null;
// }
// foreach ($this->getJsonResponse()["choices"] as $result) {
// $data = $result["message"]["toolCalls"][0]["function"]["name"]; // "{\n \"location\": \"Boston, MA\"\n}"
// // $result->index; // 0
// // $result->message->role; // 'assistant'
// // $result->message->content; // null
// // $result->message->toolCalls[0]->id; // 'call_123'
// // $result->message->toolCalls[0]->type; // 'function'
// // $result->message->toolCalls[0]->function->name; // 'get_current_weather'
// // $result->finishReason; // 'tool_calls'
// }
// }
return $data;
}
#[Groups(['read', 'write'])]
public function getToolCallId(){
$data = null;
// if (!empty($this->getJsonResponse())){
// if(!array_key_exists("choices",$this->getJsonResponse())){
// return null;
// }
// foreach ($this->getJsonResponse()["choices"] as $result) {
// if(isset($result["message"]["content"])){
// $data = $result["message"]["content"];
// }else{
// $data = $result["message"]["toolCalls"][0]["id"];
// }
// // "{\n \"location\": \"Boston, MA\"\n}"
// // $result->index; // 0
// // $result->message->role; // 'assistant'
// // $result->message->content; // null
// // $result->message->toolCalls[0]->id; // 'call_123'
// // $result->message->toolCalls[0]->type; // 'function'
// // $result->message->toolCalls[0]->function->name; // 'get_current_weather'
// // $result->finishReason; // 'tool_calls'
// }
// }
return $data;
}
#[Groups(['read', 'write'])]
public function getFunctionCallEscaped(){
//$data = $this->convertToUTF8((string)$this->getFunctionCall());
$data = $this->getFunctionCall();
return $data;
}
public function getJsonRequest(): ?string
{
return $this->jsonRequest;
}
public function setJsonRequest(?string $jsonRequest): static
{
$this->jsonRequest = $jsonRequest;
return $this;
}
public function getJsonResponse(): ?array
{
return json_decode($this->jsonResponse,true);
}
public function setJsonResponse(?string $jsonResponse): static
{
$this->jsonResponse = $jsonResponse;
return $this;
}
public function getStatus(): ?string
{
return $this->status;
}
public function setStatus(?string $status): static
{
$this->status = $status;
return $this;
}
public function getExecuteFunctionCall()
{
return "executeFunctionCall";
}
public function getAction(): ?string
{
return $this->action;
}
public function setAction(?string $action): static
{
$this->action = $action;
return $this;
}
function convertToUTF8($input) {
if (is_array($input)) {
foreach ($input as $key => $value) {
$input[$key] = $this->convertToUTF8($value);
}
} elseif (is_string($input)) {
return mb_convert_encoding($input, 'UTF-8', 'UTF-8');
}
return $input;
}
}