-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b082abf
commit 066a205
Showing
26 changed files
with
493 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
namespace Cognesy\Instructor\Extras\Evals; | ||
|
||
use ArrayIterator; | ||
use Cognesy\Instructor\Extras\Evals\Contracts\CanMapValues; | ||
use InvalidArgumentException; | ||
use Iterator; | ||
use Generator; | ||
|
||
/** | ||
* Class responsible for generating combinations of multiple iterables. | ||
*/ | ||
class Combination | ||
{ | ||
/** | ||
* Generates all possible combinations of the provided sources and maps them to instances of the mapping class. | ||
* | ||
* @template T | ||
* | ||
* @param class-string<CanMapValues> $mapping The fully qualified class name that implements CanMapValues. | ||
* @param array<string, iterable> $sources Associative array mapping keys to their respective iterables. | ||
* | ||
* @return Generator<int, T> Yields instances of the mapping class for each combination. | ||
* | ||
* @throws InvalidArgumentException If any key in order is missing from sources. | ||
*/ | ||
public static function generator( | ||
string $mapping, | ||
array $sources | ||
): Generator { | ||
$order = array_keys($sources); | ||
// Ensure all keys in order exist in sources | ||
foreach ($order as $key) { | ||
if (!array_key_exists($key, $sources)) { | ||
throw new InvalidArgumentException("Source for key '{$key}' not provided."); | ||
} | ||
} | ||
|
||
// Initialize iterators for each key in the specified order | ||
$iterators = []; | ||
foreach ($order as $key) { | ||
$iterators[$key] = self::getIterator($sources[$key]); | ||
} | ||
|
||
// Start the recursive generation of combinations | ||
yield from self::generateCombinations($mapping, $order, $iterators, []); | ||
} | ||
|
||
/** | ||
* Recursively generates combinations of values. | ||
* | ||
* @template T | ||
* | ||
* @param class-string<CanMapValues> $mapping | ||
* @param string[] $keys | ||
* @param array<string, Iterator> $iterators | ||
* @param array<string, mixed> $currentCombination | ||
* | ||
* @return Generator<int, T> | ||
*/ | ||
private static function generateCombinations( | ||
string $mapping, | ||
array $keys, | ||
array $iterators, | ||
array $currentCombination | ||
): Generator { | ||
$key = array_shift($keys); | ||
|
||
if ($key === null) { | ||
// Base case: all keys have been processed | ||
yield $mapping::map($currentCombination); | ||
return; | ||
} | ||
|
||
foreach ($iterators[$key] as $value) { | ||
$newCombination = $currentCombination; | ||
$newCombination[$key] = $value; | ||
|
||
// Recursively generate combinations for the remaining keys | ||
yield from self::generateCombinations($mapping, $keys, $iterators, $newCombination); | ||
} | ||
} | ||
|
||
/** | ||
* Converts an iterable into an Iterator. | ||
* | ||
* @param iterable $iterable | ||
* @return Iterator | ||
*/ | ||
private static function getIterator(iterable $iterable): Iterator | ||
{ | ||
if ($iterable instanceof Iterator) { | ||
return $iterable; | ||
} | ||
|
||
return new ArrayIterator(array: $iterable); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Cognesy\Instructor\Extras\Evals\Contracts; | ||
|
||
use Cognesy\Instructor\Extras\Evals\Data\EvalInput; | ||
use Cognesy\Instructor\Extras\Evals\Data\EvalOutput; | ||
|
||
interface CanEvaluate | ||
{ | ||
public function evaluate(EvalInput $input) : EvalOutput; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace Cognesy\Instructor\Extras\Evals\Contracts; | ||
|
||
/** | ||
* Interface for mapping a set of values to an object. | ||
* | ||
* @template T | ||
*/ | ||
interface CanMapValues | ||
{ | ||
/** | ||
* Maps an associative array of values to an instance of T. | ||
* | ||
* @param array<string, mixed> $values | ||
* @return T | ||
*/ | ||
public static function map(array $values); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Cognesy\Instructor\Extras\Evals\Contracts; | ||
|
||
interface Metric | ||
{ | ||
public function value() : mixed; | ||
public function toLoss() : float; | ||
public function toScore() : float; | ||
} |
Oops, something went wrong.