Skip to content

Commit

Permalink
Merge pull request #16 from llm-agents-php/feature/options-docs
Browse files Browse the repository at this point in the history
Adds default implementation for options and also provides information on what options are
  • Loading branch information
butschster authored Sep 6, 2024
2 parents e549cdd + 1011046 commit c62aced
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/LLM/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace LLM\Agents\LLM;

class Options implements OptionsInterface
{
private array $options;

public function __construct(array $options = [])
{
$this->options = $options;
}

public function has(string $option): bool
{
return isset($this->options[$option]);
}

public function get(string $option, mixed $default = null): mixed
{
return $this->options[$option] ?? $default;
}

public function with(string $option, mixed $value): static
{
$new = clone $this;
$new->options[$option] = $value;
return $new;
}

public function merge(OptionsInterface $options): static
{
$new = clone $this;
foreach ($options as $key => $value) {
$new->options[$key] = $value;
}

return $new;
}

public function getIterator(): \Traversable
{
return new \ArrayIterator($this->options);
}
}
13 changes: 13 additions & 0 deletions src/LLM/OptionsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace LLM\Agents\LLM;

final class OptionsFactory implements OptionsFactoryInterface
{
public function create(array $options = []): OptionsInterface
{
return new Options($options);
}
}
9 changes: 8 additions & 1 deletion src/LLM/OptionsFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

namespace LLM\Agents\LLM;

/**
* OptionsFactoryInterface is responsible for creating instances of Options.
*
* This interface allows for flexible creation of option sets, potentially with
* default values or initial configurations. It supports dependency injection
* and makes testing easier by abstracting the creation of options.
*/
interface OptionsFactoryInterface
{
public function create(): OptionsInterface;
public function create(array $options = []): OptionsInterface;
}
33 changes: 33 additions & 0 deletions src/LLM/OptionsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,46 @@

namespace LLM\Agents\LLM;

/**
* Provides a flexible and standardized way to manage configuration options for LLM client.
*
* Example usage:
* ```php
* // Create a new instance of Options
* $options = new Options();
*
* // Set common LLM options
* $options = $options
* ->with('model', 'gpt-4')
* ->with('max_tokens', 150)
* ->with('temperature', 0.7)
* ->with('top_p', 1.0)
* ->with('frequency_penalty', 0.0)
* ->with('presence_penalty', 0.0);
* ```
* An object will be passed to the LLMInterface where it can be used to configure the LLM client.
*/
interface OptionsInterface extends \IteratorAggregate
{
/**
* Check if a specific option exists.
*/
public function has(string $option): bool;

/**
* Get the value of a specific option.
*/
public function get(string $option, mixed $default = null): mixed;

/**
* Create a new instance with an additional or modified option.
* This method should not modify the current instance, but return a new one.
*/
public function with(string $option, mixed $value): static;

/**
* Merge the current options with another set of options.
* This method should not modify the current instance, but return a new one.
*/
public function merge(OptionsInterface $options): static;
}

0 comments on commit c62aced

Please sign in to comment.