Skip to content

Commit

Permalink
Add debug command, tweak AI models
Browse files Browse the repository at this point in the history
  • Loading branch information
RikudouSage committed Apr 15, 2024
1 parent 8eff740 commit 6876967
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
71 changes: 71 additions & 0 deletions src/Command/DebugAiResponseCommand.php
@@ -0,0 +1,71 @@
<?php

namespace App\Command;

use App\Enum\AiModel;
use App\Service\AiHorde\AiHorde;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand('app:debug-ai')]
final class DebugAiResponseCommand extends Command
{
public function __construct(
private readonly AiHorde $aiHorde,
) {
parent::__construct();
}

protected function configure(): void
{
$this
->addOption('model', 'm', InputOption::VALUE_REQUIRED, 'Model name')
->addArgument('prompt', InputArgument::OPTIONAL)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$question = new Question('Prompt');
$question->setMultiline(true);
$prompt = $input->getArgument('prompt') ?? $io->askQuestion($question);
$requestedModel = $input->getOption('model');

if (!$prompt) {
$io->error('No prompt');
return Command::FAILURE;
}

$models = array_filter(AiModel::cases(), function (AiModel $model) use ($requestedModel) {
return !$requestedModel || $model->value === $requestedModel;
});
$models = array_filter($models, function (AiModel $model) {
return count($this->aiHorde->findModels($model)) > 0;
});
shuffle($models);

if (!count($models)) {
$io->error("No available worker for given model");
return Command::FAILURE;
}
$model = $models[array_key_first($models)];

$io->comment("Prompt: {$prompt}");
$io->comment("Using model: {$model->value}");

$io->success($this->aiHorde->getResponse(
$prompt,
$model,
));

return Command::SUCCESS;
}
}
8 changes: 3 additions & 5 deletions src/Service/AiHorde/AiHorde.php
Expand Up @@ -28,7 +28,7 @@ public function getResponse(
string $message,
AiModel $model,
MessageHistory $history = new MessageHistory(),
): string {
): Message {
if (!$this->apiKey) {
throw new LogicException('There is no api key set, cannot use AI actions');
}
Expand Down Expand Up @@ -74,9 +74,7 @@ public function getResponse(
throw new LogicException('Missing generations output');
}

$output = $formatter->formatOutput($json['generations'][0]['text']);

return $output->content;
return $formatter->formatOutput($json['generations'][0]['text']);
}

/**
Expand Down Expand Up @@ -115,7 +113,7 @@ private function getMaxLength(AiModel $model): array
fn (string $modelName) => fnmatch("*/{$model->value}", $modelName),
)) > 0,
);
$targetLength = 1024;
$targetLength = 512;
$targetContext = 2048;

if (!count(array_filter($workers, fn(array $worker) => $worker['max_length'] >= $targetLength))) {
Expand Down
9 changes: 8 additions & 1 deletion src/Service/AiHorde/Message/Message.php
Expand Up @@ -3,8 +3,10 @@
namespace App\Service\AiHorde\Message;

use App\Enum\AiActor;
use JsonSerializable;
use Stringable;

final class Message implements \JsonSerializable
final class Message implements JsonSerializable, Stringable
{
public function __construct(
public AiActor $role,
Expand All @@ -22,4 +24,9 @@ public function jsonSerialize(): array
'content' => $this->content,
];
}

public function __toString()
{
return $this->content;
}
}
2 changes: 1 addition & 1 deletion src/Service/Expression/ExpressionLanguageAiFunctions.php
Expand Up @@ -34,7 +34,7 @@ private function aiAnalyzeFunction(array $context, string $message, ?string $sys
if ($systemPrompt !== null) {
$history[] = new Message(role: AiActor::System, content: $systemPrompt);
}
$models = array_filter([AiModel::OpenHermesMistral7B, AiModel::Fimbulvetr11Bv2, AiModel::LLaMA213BEstopia], fn (AiModel $model) => count($this->aiHorde->findModels($model)));
$models = array_filter([AiModel::OpenHermesMistral7B], fn (AiModel $model) => count($this->aiHorde->findModels($model)));
if (!count($models)) {
throw new RuntimeException('There are no models online available to service your request.');
}
Expand Down

0 comments on commit 6876967

Please sign in to comment.