amber-laravel/app/Repositories/LLM/History.php

53 lines
884 B
PHP
Raw Normal View History

2024-07-24 17:16:41 +00:00
<?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;
}
}