53 lines
884 B
PHP
53 lines
884 B
PHP
|
<?php
|
||
|
|
||
|
namespace App\Repositories\LLM;
|
||
|
|
||
|
class History
|
||
|
{
|
||
|
protected array $history = [];
|
||
|
|
||
|
public function addMessage(BaseMessage $message): void
|
||
|
{
|
||
|
$this->history[] = $message;
|
||
|
}
|
||
|
|
||
|
public function getMessages(): array
|
||
|
{
|
||
|
return $this->history;
|
||
|
}
|
||
|
|
||
|
public function setHistory(array $history): void
|
||
|
{
|
||
|
$this->history = $history;
|
||
|
}
|
||
|
|
||
|
public function clearHistory(): void
|
||
|
{
|
||
|
$this->history = [];
|
||
|
}
|
||
|
|
||
|
public function getForApi(): array
|
||
|
{
|
||
|
$history = [];
|
||
|
|
||
|
|
||
|
foreach ($this->history as $h) {
|
||
|
|
||
|
$a = [
|
||
|
'role' => $h->role->value,
|
||
|
'content' => $h->content,
|
||
|
];
|
||
|
|
||
|
if (isset($h->tool_calls)) {
|
||
|
$a['tool_calls'] = $h->tool_calls;
|
||
|
}
|
||
|
|
||
|
$history[] = $a;
|
||
|
}
|
||
|
|
||
|
return $history;
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|