From 10110463b70704d7cb385a06fcd85612aa2effa6 Mon Sep 17 00:00:00 2001 From: Pavel Buchnev Date: Fri, 6 Sep 2024 10:48:17 +0400 Subject: [PATCH] Adds default implementation for options and also provides information on what options are --- src/LLM/Options.php | 47 +++++++++++++++++++++++++++++ src/LLM/OptionsFactory.php | 13 ++++++++ src/LLM/OptionsFactoryInterface.php | 9 +++++- src/LLM/OptionsInterface.php | 33 ++++++++++++++++++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/LLM/Options.php create mode 100644 src/LLM/OptionsFactory.php diff --git a/src/LLM/Options.php b/src/LLM/Options.php new file mode 100644 index 0000000..3e49fbe --- /dev/null +++ b/src/LLM/Options.php @@ -0,0 +1,47 @@ +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); + } +} diff --git a/src/LLM/OptionsFactory.php b/src/LLM/OptionsFactory.php new file mode 100644 index 0000000..2601cd4 --- /dev/null +++ b/src/LLM/OptionsFactory.php @@ -0,0 +1,13 @@ +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; }