Files
opentextsummaries/app/Console/Commands/TestOllama.php
T

193 lines
6.0 KiB
PHP
Raw Normal View History

2026-03-29 16:14:18 +03:00
<?php
namespace App\Console\Commands;
use App\Services\OllamaService;
use Illuminate\Console\Command;
class TestOllama extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ollama:test
{--model= : Specific model to test (overrides config)}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Test connection to Ollama API and run a sample summary generation';
/**
* Execute the console command.
*/
public function handle(OllamaService $ollamaService): int
{
$this->info('Ollama API Test');
$this->info('===============');
$this->newLine();
// Override model if specified
$modelOption = $this->option('model');
if ($modelOption) {
$ollamaService->setModel($modelOption);
$this->comment("Using model: {$modelOption}");
$this->newLine();
}
// Display configuration
$this->displayConfiguration();
$this->newLine();
// Test connection
if (!$this->testConnection($ollamaService)) {
return self::FAILURE;
}
$this->newLine();
// List available models
$this->listAvailableModels($ollamaService);
$this->newLine();
// Run test generation
if ($this->confirm('Do you want to run a test summary generation?', true)) {
$this->newLine();
$this->testSummaryGeneration($ollamaService);
}
return self::SUCCESS;
}
/**
* Display Ollama configuration.
*/
protected function displayConfiguration(): void
{
$this->info('Configuration:');
$this->table(
['Setting', 'Value'],
[
['API URL', config('ollama.api_url')],
['Model', config('ollama.model')],
['Timeout', config('ollama.timeout') . ' seconds'],
['Temperature', config('ollama.temperature')],
['Max Tokens', config('ollama.max_tokens')],
]
);
}
/**
* Test connection to Ollama API.
*/
protected function testConnection(OllamaService $ollamaService): bool
{
$this->info('Testing connection to Ollama API...');
if ($ollamaService->checkConnection()) {
$this->info('✓ Connection successful!');
return true;
} else {
$this->error('✗ Connection failed!');
$this->newLine();
$this->warn('Possible issues:');
$this->comment(' 1. Ollama is not running (start with: ollama serve)');
$this->comment(' 2. Wrong API URL in config/ollama.php or .env');
$this->comment(' 3. Network/firewall issues');
$this->newLine();
$this->comment('Current API URL: ' . config('ollama.api_url'));
return false;
}
}
/**
* List available models.
*/
protected function listAvailableModels(OllamaService $ollamaService): void
{
$this->info('Available Models:');
$models = $ollamaService->listModels();
if (empty($models)) {
$this->warn('No models found or unable to fetch models.');
$this->newLine();
$this->comment('Pull a model with: ollama pull llama3');
return;
}
$currentModel = $ollamaService->getModel();
$rows = [];
foreach ($models as $model) {
$isCurrent = $model === $currentModel ? '✓' : '';
$rows[] = [$model, $isCurrent];
}
$this->table(['Model Name', 'Current'], $rows);
if (!in_array($currentModel, $models)) {
$this->warn("⚠ Configured model '{$currentModel}' is not available!");
$this->comment("Pull it with: ollama pull {$currentModel}");
}
}
/**
* Test summary generation.
*/
protected function testSummaryGeneration(OllamaService $ollamaService): void
{
$this->info('Running test summary generation...');
$this->newLine();
$testResult = $ollamaService->testGeneration();
if ($testResult['success']) {
$this->info('✓ Test generation successful!');
$this->newLine();
$this->table(
['Metric', 'Value'],
[
['Duration', $testResult['duration'] . ' seconds'],
['Model Used', $testResult['model']],
['Word Count', $testResult['word_count']],
]
);
$this->newLine();
$this->info('Generated Summary:');
$this->line('------------------');
$this->line($testResult['summary']);
$this->line('------------------');
$this->newLine();
// Quality assessment
$wordCount = $testResult['word_count'];
$minWords = config('ollama.quality_checks.min_word_count', 100);
$maxWords = config('ollama.quality_checks.max_word_count', 1000);
if ($wordCount < $minWords) {
$this->warn("⚠ Summary is shorter than recommended minimum ({$minWords} words)");
} elseif ($wordCount > $maxWords) {
$this->warn("⚠ Summary is longer than recommended maximum ({$maxWords} words)");
} else {
$this->info('✓ Summary length is within recommended range');
}
$avgTimePerWord = $testResult['duration'] / max($wordCount, 1);
$this->comment("Average: " . round($avgTimePerWord, 2) . " seconds per word");
} else {
$this->error('✗ Test generation failed!');
$this->newLine();
$this->warn('This could indicate:');
$this->comment(' 1. Model is not properly loaded');
$this->comment(' 2. Insufficient system resources');
$this->comment(' 3. Timeout issues (increase OLLAMA_TIMEOUT in .env)');
}
}
}