-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathLLaVaTextToTextActionHandler.php
More file actions
74 lines (61 loc) · 2.41 KB
/
Copy pathLLaVaTextToTextActionHandler.php
File metadata and controls
74 lines (61 loc) · 2.41 KB
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
64
65
66
67
68
69
70
71
72
73
74
<?php
declare(strict_types=1);
namespace App\AI\Handler;
use Ibexa\Contracts\ConnectorAi\Action\ActionHandlerInterface;
use Ibexa\Contracts\ConnectorAi\Action\DataType\Text;
use Ibexa\Contracts\ConnectorAi\Action\Response\TextResponse;
use Ibexa\Contracts\ConnectorAi\Action\TextToText\Action as TextToTextAction;
use Ibexa\Contracts\ConnectorAi\ActionInterface;
use Ibexa\Contracts\ConnectorAi\ActionResponseInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
final readonly class LLaVaTextToTextActionHandler implements ActionHandlerInterface
{
public const string IDENTIFIER = 'LLaVATextToText';
public function __construct(private HttpClientInterface $client, private string $host = 'http://localhost:8080')
{
}
public function supports(ActionInterface $action): bool
{
return $action instanceof TextToTextAction;
}
public function handle(ActionInterface $action, array $context = []): ActionResponseInterface
{
/** @var \Ibexa\Contracts\ConnectorAi\Action\DataType\Text */
$input = $action->getInput();
$text = $this->sanitizeInput($input->getText());
$systemMessage = $action->hasActionContext() ? $action->getActionContext()->getActionHandlerOptions()->get('system_prompt', '') : '';
$response = $this->client->request(
'POST',
sprintf('%s/v1/chat/completions', $this->host),
[
'headers' => [
'Authorization: Bearer no-key',
],
'json' => [
'model' => 'LLaMA_CPP',
'messages' => [
(object)[
'role' => 'system',
'content' => $systemMessage,
],
(object)[
'role' => 'user',
'content' => $text,
],
],
'temperature' => 0.7,
],
]
);
$output = strip_tags((string) json_decode($response->getContent(), true)['choices'][0]['message']['content']);
return new TextResponse(new Text([$output]));
}
public static function getIdentifier(): string
{
return self::IDENTIFIER;
}
private function sanitizeInput(string $text): string
{
return str_replace(["\n", "\r"], ' ', $text);
}
}