-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChat.php
63 lines (49 loc) · 1.96 KB
/
Chat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
declare(strict_types=1);
namespace App\Blog;
use PhpLlm\LlmChain\ChainInterface;
use PhpLlm\LlmChain\Model\Message\Message;
use PhpLlm\LlmChain\Model\Message\MessageBag;
use PhpLlm\LlmChain\Model\Response\TextResponse;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\RequestStack;
final class Chat
{
private const SESSION_KEY = 'blog-chat';
public function __construct(
private readonly RequestStack $requestStack,
#[Autowire(service: 'llm_chain.chain.blog')]
private readonly ChainInterface $chain,
) {
}
public function loadMessages(): MessageBag
{
$messages = new MessageBag(
Message::forSystem(<<<PROMPT
You are an helpful assistant that knows about the latest blog content of the Symfony's framework website.
To search for content you use the tool 'similarity_search' for generating the answer. Only use content
that you get from searching with that tool or you previous answers. Don't make up information and if you
can't find something, just say so. Also provide links to the blog posts you use as sources.
PROMPT
)
);
return $this->requestStack->getSession()->get(self::SESSION_KEY, $messages);
}
public function submitMessage(string $message): void
{
$messages = $this->loadMessages();
$messages->add(Message::ofUser($message));
$response = $this->chain->call($messages);
assert($response instanceof TextResponse);
$messages->add(Message::ofAssistant($response->getContent()));
$this->saveMessages($messages);
}
public function reset(): void
{
$this->requestStack->getSession()->remove(self::SESSION_KEY);
}
private function saveMessages(MessageBag $messages): void
{
$this->requestStack->getSession()->set(self::SESSION_KEY, $messages);
}
}