Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chatbot v2 #9656

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor MessageFactory
Remg committed Feb 5, 2024
commit d9b80fd9016961473a813dd89f8bf58195b1794c
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

namespace App\Chatbot\Assistant;
namespace App\Chatbot\Assistant\OpenAI;

use App\Chatbot\Assistant\AssistantHandlerInterface;
use App\Entity\Chatbot\Message;
use App\OpenAI\Event\ThreadEvent;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

class OpenAIAssistantHandler implements AssistantHandlerInterface
class AssistantHandler implements AssistantHandlerInterface
{
public function __construct(private readonly EventDispatcherInterface $dispatcher)
{
47 changes: 47 additions & 0 deletions src/Chatbot/Assistant/OpenAI/MessageFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Chatbot\Assistant\OpenAI;

use App\Chatbot\ThreadFactory;
use App\Entity\Chatbot\Run;
use App\Entity\Chatbot\Thread;
use App\Entity\OpenAI\Assistant;
use App\OpenAI\MessageFactoryInterface;
use App\OpenAI\Model\AssistantInterface;
use App\OpenAI\Model\MessageInterface;
use App\OpenAI\Model\RunInterface;
use App\OpenAI\Model\ThreadInterface;

class MessageFactory implements MessageFactoryInterface
{
public function __construct(private readonly ThreadFactory $threadFactory)
{
}

public function createAssistantMessage(
ThreadInterface $thread,
string $openAiId,
string $text,
array $annotations,
\DateTimeInterface $date,
?AssistantInterface $assistant,
?RunInterface $run
): MessageInterface {
if (!$thread instanceof Thread) {
throw new \InvalidArgumentException('This factory can only handle "%s" entities.', Thread::class);
}

$message = $this->threadFactory->createAssistantMessage($thread, $text, $annotations, $date);
$message->openAiId = $openAiId;

if ($assistant instanceof Assistant) {
$message->assistant = $assistant;
}

if ($run instanceof Run) {
$message->run = $run;
}

return $message;
}
}
2 changes: 1 addition & 1 deletion src/Chatbot/EventListener/UserMessageListener.php
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ public function __construct(private readonly Assistant $assistant)
{
}

public function __invoke(UserMessageEvent $event)
public function __invoke(UserMessageEvent $event): void
{
$this->assistant->handle($event->message);
}
30 changes: 11 additions & 19 deletions src/Chatbot/ThreadFactory.php
Original file line number Diff line number Diff line change
@@ -23,31 +23,23 @@ public function createUserMessage(
array $entities,
\DateTimeInterface $date
): Message {
return $this->createMessage($thread, MessageRoleEnum::USER, $text, $entities, $date);
return Message::create($thread, MessageRoleEnum::USER, $text, $entities, $date);
}

private function createThread(Chatbot $chatbot): Thread
public function createAssistantMessage(
Thread $thread,
string $text,
array $entities,
\DateTimeInterface $date
): Message {
return Message::create($thread, MessageRoleEnum::ASSISTANT, $text, $entities, $date);
}

protected function createThread(Chatbot $chatbot): Thread
{
$thread = new Thread();
$thread->chatbot = $chatbot;

return $thread;
}

private function createMessage(
Thread $thread,
MessageRoleEnum $role,
string $text,
array $entities,
\DateTimeInterface $date,
): Message {
$message = new Message();
$message->thread = $thread;
$message->role = $role;
$message->text = $text;
$message->entities = $entities;
$message->date = $date;

return $message;
}
}
17 changes: 17 additions & 0 deletions src/Entity/Chatbot/Message.php
Original file line number Diff line number Diff line change
@@ -75,6 +75,23 @@ public function __construct(?UuidInterface $uuid = null)
$this->uuid = $uuid ?? Uuid::uuid4();
}

public static function create(
Thread $thread,
MessageRoleEnum $role,
string $text,
array $entities,
\DateTimeInterface $date
): self {
$message = new self();
$message->thread = $thread;
$message->role = $role;
$message->text = $text;
$message->entities = $entities;
$message->date = $date;

return $message;
}

public function isUserMessage(): bool
{
return MessageRoleEnum::USER === $this->role;
21 changes: 21 additions & 0 deletions src/OpenAI/MessageFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\OpenAI;

use App\OpenAI\Model\AssistantInterface;
use App\OpenAI\Model\MessageInterface;
use App\OpenAI\Model\RunInterface;
use App\OpenAI\Model\ThreadInterface;

interface MessageFactoryInterface
{
public function createAssistantMessage(
ThreadInterface $thread,
string $openAiId,
string $text,
array $annotations,
\DateTimeInterface $date,
?AssistantInterface $assistant,
?RunInterface $run
): MessageInterface;
}
3 changes: 2 additions & 1 deletion src/OpenAI/OpenAI.php
Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ public function __construct(
private readonly ThreadProviderInterface $threadProvider,
private readonly MessageProviderInterface $messageProvider,
private readonly RunProviderInterface $runProvider,
private readonly MessageFactoryInterface $messageFactory,
private readonly AssistantProviderInterface $assistantProvider,
private readonly EventDispatcherInterface $dispatcher
) {
@@ -107,7 +108,7 @@ public function retrieveAssistantMessages(ThreadInterface $thread): void
continue;
}

$message = $this->messageProvider->createAssistantMessage(
$message = $this->messageFactory->createAssistantMessage(
$thread,
$messageResponse->id,
$messageResponse->text,
13 changes: 0 additions & 13 deletions src/OpenAI/Provider/MessageProviderInterface.php
Original file line number Diff line number Diff line change
@@ -2,22 +2,9 @@

namespace App\OpenAI\Provider;

use App\OpenAI\Model\AssistantInterface;
use App\OpenAI\Model\MessageInterface;
use App\OpenAI\Model\RunInterface;
use App\OpenAI\Model\ThreadInterface;

interface MessageProviderInterface
{
public function save(MessageInterface $message): void;

public function createAssistantMessage(
ThreadInterface $thread,
string $openAiId,
string $text,
array $annotations,
\DateTimeInterface $date,
?AssistantInterface $assistant,
?RunInterface $run
): MessageInterface;
}
34 changes: 0 additions & 34 deletions src/Repository/Chatbot/MessageRepository.php
Original file line number Diff line number Diff line change
@@ -2,14 +2,8 @@

namespace App\Repository\Chatbot;

use App\Chatbot\Enum\MessageRoleEnum;
use App\Entity\Chatbot\Message;
use App\Entity\Chatbot\Run;
use App\Entity\OpenAI\Assistant;
use App\OpenAI\Model\AssistantInterface;
use App\OpenAI\Model\MessageInterface;
use App\OpenAI\Model\RunInterface;
use App\OpenAI\Model\ThreadInterface;
use App\OpenAI\Provider\MessageProviderInterface;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
@@ -26,32 +20,4 @@ public function save(MessageInterface $message): void
$this->_em->persist($message);
$this->_em->flush();
}

public function createAssistantMessage(
ThreadInterface $thread,
string $openAiId,
string $text,
array $annotations,
\DateTimeInterface $date,
?AssistantInterface $assistant,
?RunInterface $run
): MessageInterface {
$message = new Message();
$message->thread = $thread;
$message->role = MessageRoleEnum::ASSISTANT;
$message->openAiId = $openAiId;
$message->text = $text;
$message->entities = $annotations;
$message->date = $date;

if ($assistant instanceof Assistant) {
$message->assistant = $assistant;
}

if ($run instanceof Run) {
$message->run = $run;
}

return $message;
}
}