Skip to content

Commit

Permalink
Cleaned up Hub code
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Mar 16, 2024
1 parent 92a20a0 commit 18d05b5
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 158 deletions.
14 changes: 7 additions & 7 deletions config/hub.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Cognesy\InstructorHub\Commands\ShowExample;
use Cognesy\InstructorHub\Core\CommandRegistry;
use Cognesy\InstructorHub\Services\DocGenerator;
use Cognesy\InstructorHub\Services\Examples;
use Cognesy\InstructorHub\Services\ExampleRepository;
use Cognesy\InstructorHub\Services\Runner;


Expand All @@ -31,7 +31,7 @@ class: CommandRegistry::class,
);

$config->declare(
class: Examples::class,
class: ExampleRepository::class,
context: [
'baseDir' => __DIR__ . '/../examples/',
],
Expand All @@ -40,9 +40,9 @@ class: Examples::class,
$config->declare(
class: Runner::class,
context: [
'examples' => $config->reference(Examples::class),
'examples' => $config->reference(ExampleRepository::class),
'displayErrors' => true,
'stopAfter' => 2,
'stopAfter' => 0,
'stopOnError' => true,
],
);
Expand All @@ -60,7 +60,7 @@ class: GenerateDocs::class,
$config->declare(
class: ListAllExamples::class,
context: [
'examples' => $config->reference(Examples::class),
'examples' => $config->reference(ExampleRepository::class),
],
);

Expand All @@ -75,14 +75,14 @@ class: RunAllExamples::class,
class: RunOneExample::class,
context: [
'runner' => $config->reference(Runner::class),
'examples' => $config->reference(Examples::class),
'examples' => $config->reference(ExampleRepository::class),
],
);

$config->declare(
class: ShowExample::class,
context: [
'examples' => $config->reference(Examples::class),
'examples' => $config->reference(ExampleRepository::class),
],
);

Expand Down
Empty file added docs/hub/index.md
Empty file.
6 changes: 4 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ nav:
# - Image to Ad Copy: 'examples/image_to_ad_copy.md'
# - Ollama: 'examples/ollama.md'
# - SQLModel Integration: 'examples/sqlmodel.md'
# - Hub:
# - Introducing Instructor Hub: 'hub/index.md'
- Hub:
- Introducing Instructor Hub: 'hub/index.md'
###HUB-START###
###HUB-END###
# - Single Classification Model: 'hub/single_classification.md'
# - Multiple Classification Model: 'hub/multiple_classification.md'
# - Extracting Tables using GPT-V: 'hub/tables_from_vision.md'
Expand Down
3 changes: 2 additions & 1 deletion mkdocs.yml.bak
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ nav:
# - Ollama: 'examples/ollama.md'
# - SQLModel Integration: 'examples/sqlmodel.md'
- Hub:
# - Introducing Instructor Hub: 'hub/index.md'
- Introducing Instructor Hub: 'hub/index.md'
###HUB###
# - Single Classification Model: 'hub/single_classification.md'
# - Multiple Classification Model: 'hub/multiple_classification.md'
# - Extracting Tables using GPT-V: 'hub/tables_from_vision.md'
Expand Down
16 changes: 7 additions & 9 deletions src-hub/Commands/ListAllExamples.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

use Cognesy\InstructorHub\Core\Cli;
use Cognesy\InstructorHub\Core\Command;
use Cognesy\InstructorHub\Services\Examples;
use Cognesy\InstructorHub\Data\Example;
use Cognesy\InstructorHub\Services\ExampleRepository;
use Cognesy\InstructorHub\Utils\Color;

class ListAllExamples extends Command
Expand All @@ -12,23 +13,20 @@ class ListAllExamples extends Command
public string $description = "List all examples";

public function __construct(
public Examples $examples
public ExampleRepository $examples
) {}

public function execute(array $params = []) : void {
Cli::outln("Listing all examples...", [Color::BOLD, Color::YELLOW]);
$this->examples->forEachFile(function($file, $index) {
$this->examples->forEachExample(function(Example $example) {
Cli::grid([
[1, '(', STR_PAD_LEFT, Color::DARK_GRAY],
[2, $index, STR_PAD_LEFT, Color::WHITE],
[2, $example->index, STR_PAD_LEFT, Color::WHITE],
[1, ')', STR_PAD_LEFT, Color::DARK_GRAY],
[32, $file, STR_PAD_RIGHT, Color::GREEN],
[32, $example->name, STR_PAD_RIGHT, Color::GREEN],
[2, '-', STR_PAD_LEFT, Color::WHITE],
[50, $this->examples->getHeader($file), STR_PAD_RIGHT, Color::DARK_GRAY]
[50, $example->title, STR_PAD_RIGHT, Color::DARK_GRAY]
]);
// Cli::out($file, Color::GREEN);
// //Cli::out(" - ");
// //Cli::out($this->examples->getHeader($file), Color::GRAY)
Cli::outln();
return true;
});
Expand Down
12 changes: 6 additions & 6 deletions src-hub/Commands/RunOneExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Cognesy\InstructorHub\Core\Cli;
use Cognesy\InstructorHub\Core\Command;
use Cognesy\InstructorHub\Services\Examples;
use Cognesy\InstructorHub\Services\ExampleRepository;
use Cognesy\InstructorHub\Services\Runner;
use Cognesy\InstructorHub\Utils\Color;

Expand All @@ -13,22 +13,22 @@ class RunOneExample extends Command
public string $description = "Run one example";

public function __construct(
private Runner $runner,
private Examples $examples,
private Runner $runner,
private ExampleRepository $examples,
) {}

public function execute(array $params = []) {
$file = $params[0] ?? '';
$file = $this->examples->exampleName($file);
if (empty($file)) {
Cli::outln("Please specify an example to run");
Cli::outln("You can list available examples with `list` command.\n", Color::DARK_GRAY);
return;
}
if (!$this->examples->exampleExists($file)) {
$example = $this->examples->resolveToExample($file);
if (empty($example)) {
Cli::outln("Example not found", [Color::RED]);
return;
}
$this->runner->runSingle($file);
$this->runner->runSingle($example);
}
}
21 changes: 11 additions & 10 deletions src-hub/Commands/ShowExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Cognesy\InstructorHub\Core\Cli;
use Cognesy\InstructorHub\Core\Command;
use Cognesy\InstructorHub\Services\Examples;
use Cognesy\InstructorHub\Services\ExampleRepository;
use Cognesy\InstructorHub\Utils\CliMarkdown;
use Cognesy\InstructorHub\Utils\Color;

Expand All @@ -13,36 +13,37 @@ class ShowExample extends Command
public string $description = "Show example";

public function __construct(
private Examples $examples,
private ExampleRepository $examples,
) {}

public function execute(array $params = []) {
$file = $params[0] ?? '';
//$showErrors = ($params[1]=='--debug') ?? false;
$file = $this->examples->exampleName($file);
if (empty($file)) {
Cli::outln("Please specify an example to show");
Cli::outln("You can list available examples with `list` command.\n", Color::DARK_GRAY);
return;
}
if (!$this->examples->exampleExists($file)) {
//$showErrors = ($params[1]=='--debug') ?? false;
$example = $this->examples->resolveToExample($file);
if (empty($example)) {
Cli::outln("Example not found", [Color::RED]);
return;
}

Cli::out("Example: ", [Color::DARK_GRAY]);
Cli::outln($file, [Color::BOLD, Color::WHITE]);
Cli::outln($example->name, [Color::BOLD, Color::WHITE]);
Cli::outln("---");
Cli::outln();
Cli::outln();
$content = $this->examples->getContent($file);

$parser = new CliMarkdown;
$md = $parser->parse($content);
$md = $parser->parse($example->content);
Cli::outln($md);
Cli::outln("---");
Cli::outln();
Cli::outln("Run this example:", [Color::DARK_YELLOW]);
Cli::out("> ", [Color::DARK_GRAY]);
Cli::outln("./hub.sh run {$params[0]}", [Color::BOLD, Color::WHITE]);
Cli::outln("./hub.sh run {$example->name}", [Color::BOLD, Color::WHITE]);
Cli::outln();
}
}
}
17 changes: 17 additions & 0 deletions src-hub/Data/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Cognesy\InstructorHub\Data;

class Example
{
public function __construct(
public int $index,
public string $name,
public bool $hasTitle,
public string $title,
public string $content,
public string $directory,
public string $relativePath,
public string $runPath,
) {}
}
11 changes: 2 additions & 9 deletions src-hub/Services/DocGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@

class DocGenerator
{
private string $baseDir = '';
private CliMarkdown $parser;

public function __construct(
private Examples $examples,
) {
$this->baseDir = $this->examples->getBaseDir();
$this->parser = new CliMarkdown();
}
private ExampleRepository $examples,
) {}

public function copyAsMd(mixed $file) : void {
$output = file_get_contents($this->baseDir . '/' . $file . '/run.php');
throw new \Exception('Not implemented');
}
}
128 changes: 128 additions & 0 deletions src-hub/Services/ExampleRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
namespace Cognesy\InstructorHub\Services;

use Cognesy\InstructorHub\Data\Example;

class ExampleRepository {
public string $baseDir = '';

public function __construct(string $baseDir) {
$this->baseDir = $baseDir ?: ($this->guessBaseDir() . '/');
}

public function forEachExample(callable $callback) : void {
$directories = $this->getExampleDirectories();
// loop through the files and select only directories
$index = 1;
foreach ($directories as $file) {
// check if run.php exists in the directory
if (!$this->exampleExists($file)) {
continue;
}
$example = $this->getExample($file, $index);
if (!$callback($example)) {
break;
}
$index++;
}
}

public function resolveToExample(string $input) : ?Example {
// handle example provided by index
$example = (int) ($input ?? '');
if ($example > 0) {
$list = $this->getExampleDirectories();
$index = $example - 1;
if (isset($list[$index])) {
return $this->getExample($list[$index], $index);
}
}
if (!$this->exampleExists($input)) {
return null;
}
return $this->getExample($input);
}

////////////////////////////////////////////////////////////////////////////////////////////////////////

private function getExample(string $file, int $index = 0) : Example {
$content = $this->getContent($file);
return new Example(
index: $index,
name: $file,
hasTitle: $this->hasTitle($content),
title: $this->getTitle($content),
content: $content,
directory: $this->baseDir . $file,
relativePath: './examples/' . $file . '/run.php',
runPath: $this->getRunPath($file),
);
}

private function getRunPath(string $file) : string {
return $this->baseDir . $file . '/run.php';
}

private function getContent(string $file) : string {
$path = $this->getRunPath($file);
return file_get_contents($path);
}

private function getTitle(string $content) : string {
$header = $this->findMdH1Line($content);
return $this->cleanStr($header, 60);
}

private function hasTitle(string $content) : bool {
$title = $this->getTitle($content);
return ($title !== '');
}

private function exampleExists(string $file) : bool {
$path = $this->getRunPath($file);
return file_exists($path);
}

private function guessBaseDir() : string {
// get current directory of this script
return dirname(__DIR__).'/../examples';
}

private function getExampleDirectories() : array {
// get all files in the directory
$files = scandir($this->baseDir);
// remove . and .. from the list
$files = array_diff($files, array('.', '..'));
$directories = [];
foreach ($files as $key => $file) {
// check if the file is a directory
if (!is_dir($this->baseDir . '/' . $file)) {
continue;
}
$directories[] = $file;
}
return $directories;
}

private function cleanStr(string $input, int $limit) : string {
// remove any \n, \r, PHP tags, md hashes
$output = str_replace(array("\n", "\r", '<?php', '?>', '#'), array(' ', '', '', '', ''), $input);
// remove leading and trailing spaces
$output = trim($output);
// remove double spaces
$output = preg_replace('/\s+/', ' ', $output);
// remove any ANSI codes
$output = preg_replace('/\e\[[\d;]*m/', '', $output);
return substr(trim($output), 0, $limit);
}

private function findMdH1Line(string $input) : string {
$lines = explode("\n", $input);
foreach ($lines as $line) {
if (substr($line, 0, 2) === '# ') {
return $line;
}
}
return '';
}
}
Loading

0 comments on commit 18d05b5

Please sign in to comment.