Skip to content

Commit b81fa9c

Browse files
committed
Fixes in examples
1 parent 24f4dfa commit b81fa9c

File tree

4 files changed

+67
-75
lines changed

4 files changed

+67
-75
lines changed

examples/A02_Advanced/ConfigProviders/run.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
use Cognesy\Instructor\StructuredOutput;
2525
use Cognesy\Polyglot\LLM\Enums\OutputMode;
2626

27-
class User {
28-
public int $age;
29-
public string $name;
30-
}
31-
3227
class CustomConfigProvider implements CanProvideConfig
3328
{
3429
private Dot $dot;
@@ -161,6 +156,11 @@ public function has(string $path): bool {
161156

162157
// Call with custom model and execution mode
163158

159+
class User {
160+
public int $age;
161+
public string $name;
162+
}
163+
164164
$user = $structuredOutput
165165
// Use 'deepseek' preset defined in CustomLLMConfigProvider
166166
->using('deepseek')

examples/B02_LLMAdvanced/ConfigProviders/run.php

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,12 @@
1717
use Adbar\Dot;
1818
use Cognesy\Config\Contracts\CanProvideConfig;
1919
use Cognesy\Config\Env;
20-
use Cognesy\Events\Event;
2120
use Cognesy\Events\EventDispatcher;
2221
use Cognesy\Http\HttpClientBuilder;
2322
use Cognesy\Polyglot\LLM\Inference;
2423
use Cognesy\Utils\Str;
2524
use Symfony\Component\HttpClient\HttpClient as SymfonyHttpClient;
2625

27-
class CustomConfigProvider implements CanProvideConfig
28-
{
29-
private Dot $dot;
30-
31-
public function __construct(array $config = []) {
32-
$this->dot = new Dot($config);
33-
}
34-
35-
public function get(string $path, mixed $default = null): mixed {
36-
return $this->dot->get($path, $default);
37-
}
38-
39-
public function has(string $path): bool {
40-
return $this->dot->has($path);
41-
}
42-
}
43-
4426
$configData = [
4527
'http' => [
4628
'defaultPreset' => 'symfony',
@@ -101,33 +83,48 @@ public function has(string $path): bool {
10183
],
10284
];
10385

104-
$events = new EventDispatcher();
86+
class CustomConfigProvider implements CanProvideConfig
87+
{
88+
private Dot $dot;
89+
90+
public function __construct(array $data = []) {
91+
$this->dot = new Dot($data);
92+
}
93+
94+
public function get(string $path, mixed $default = null): mixed {
95+
return $this->dot->get($path, $default);
96+
}
97+
98+
public function has(string $path): bool {
99+
return $this->dot->has($path);
100+
}
101+
}
102+
105103
$configProvider = new CustomConfigProvider($configData);
106104

105+
$events = new EventDispatcher();
107106
$customClient = (new HttpClientBuilder(
108107
events: $events,
109108
listener: $events,
110-
configProvider: new CustomConfigProvider(),
109+
configProvider: $configProvider,
111110
))
112111
->withClientInstance(SymfonyHttpClient::create(['http_version' => '2.0']))
113-
->withDebugPreset('on')
114112
->create();
115113

116114
$inference = (new Inference(
117115
events: $events,
118116
listener: $events,
119-
configProvider: new CustomConfigProvider()
117+
configProvider: $configProvider,
120118
))
121119
->withHttpClient($customClient);
122120

123121
$answer = $inference
124122
->using('deepseek') // Use 'deepseek' preset from CustomLLMConfigProvider
125-
->wiretap(fn(Event $e) => $e->print())
126-
->with(
127-
messages: [['role' => 'user', 'content' => 'What is the capital of France']],
128-
options: ['max_tokens' => 64]
129-
)
130-
->withStreaming()
123+
//->withDebugPreset('on')
124+
//->wiretap(fn(Event $e) => $e->print())
125+
->withMessages([['role' => 'user', 'content' => 'What is the capital of France']])
126+
->withMaxTokens(256)
127+
//->withStreaming()
131128
->get();
132129

133130
echo "USER: What is capital of France\n";

packages/config/src/ConfigPresets.php

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,59 @@
77

88
class ConfigPresets
99
{
10+
private string $group;
11+
private string $defaultPresetKey;
12+
private string $presetGroupKey;
13+
private ?CanProvideConfig $configProvider;
14+
1015
public function __construct(
11-
private string $group,
12-
private string $defaultPresetKey = 'defaultPreset',
13-
private string $presetGroupKey = 'presets',
14-
private ?CanProvideConfig $config = null,
16+
string $group,
17+
string $defaultPresetKey = 'defaultPreset',
18+
string $presetGroupKey = 'presets',
19+
?CanProvideConfig $configProvider = null,
1520
) {
16-
if ($this->config === null) {
17-
$this->config = ConfigResolver::default();
18-
}
21+
$this->group = $group;
22+
$this->defaultPresetKey = $defaultPresetKey;
23+
$this->presetGroupKey = $presetGroupKey;
24+
$this->configProvider = ConfigResolver::makeWith($configProvider);
1925
}
2026

2127
// FLUENT CREATION API ///////////////////////////////////////////////////
2228

23-
public static function using(
24-
?CanProvideConfig $config,
25-
): self {
29+
public static function using(?CanProvideConfig $config): self {
2630
return new self(
2731
group: '',
28-
config: $config ?? ConfigResolver::default(),
32+
configProvider: $config,
2933
);
3034
}
3135

3236
public function for(string $group): self {
3337
return new self(
3438
group: $group,
35-
config: $this->config,
39+
defaultPresetKey: $this->defaultPresetKey,
40+
presetGroupKey: $this->presetGroupKey,
41+
configProvider: $this->configProvider,
3642
);
3743
}
3844

3945
public function withConfigProvider(CanProvideConfig $config): self {
4046
return new self(
41-
group: $this->group,
47+
group : $this->group,
4248
defaultPresetKey: $this->defaultPresetKey,
43-
presetGroupKey: $this->presetGroupKey,
44-
config: $config,
49+
presetGroupKey : $this->presetGroupKey,
50+
configProvider : ConfigResolver::makeWith($config),
4551
);
4652
}
4753

4854
// PUBLIC INTERFACE //////////////////////////////////////////////////////
4955

5056
public function get(?string $preset = null): array {
51-
// If no preset specified, get the default preset name
5257
$presetName = $preset ?? $this->defaultPresetName();
53-
54-
// Build path to the actual preset configuration
5558
$presetPath = $this->presetPath($presetName);
56-
57-
// Get the preset configuration
58-
$presetConfig = $this->config->get($presetPath);
59-
59+
$presetConfig = $this->configProvider->get($presetPath);
6060
if (!is_array($presetConfig)) {
6161
throw new ConfigPresetNotFoundException("Preset '{$presetName}' not found at path: {$presetPath}");
6262
}
63-
6463
return $presetConfig;
6564
}
6665

@@ -72,40 +71,40 @@ public function getOrDefault(?string $preset = null): array {
7271
return $this->default();
7372
}
7473
$presetPath = $this->presetPath($preset);
75-
return $this->config->get($presetPath);
74+
return $this->configProvider->get($presetPath);
7675
}
7776

7877
public function hasPreset(string $preset): bool {
7978
$presetPath = $this->presetPath($preset);
80-
return $this->config->has($presetPath);
79+
return $this->configProvider->has($presetPath);
8180
}
8281

8382
public function default(): array {
8483
if (!$this->hasDefault()) {
85-
throw new ConfigPresetNotFoundException("No default preset configured at path: {$this->defaultPresetKey}");
84+
throw new ConfigPresetNotFoundException("No default preset configured for group: {$this->group}");
8685
}
8786
$defaultPresetName = $this->defaultPresetName();
8887
return $this->get($defaultPresetName);
8988
}
9089

9190
public function presets(): array {
9291
$presetsPath = $this->path($this->presetGroupKey);
93-
$presets = $this->config->get($presetsPath, []);
92+
$presets = $this->configProvider->get($presetsPath, []);
9493
return is_array($presets) ? array_keys($presets) : [];
9594
}
9695

9796
// INTERNAL ////////////////////////////////////////////////////////
9897

9998
private function hasDefault(): bool {
10099
$defaultPath = $this->path($this->defaultPresetKey);
101-
return $this->config->has($defaultPath);
100+
return $this->configProvider->has($defaultPath);
102101
}
103102

104103
private function defaultPresetName(): string {
105-
$defaultPath = $this->path($this->defaultPresetKey);
106-
$defaultPreset = $this->config->get($defaultPath);
104+
$defaultPresetPath = $this->path($this->defaultPresetKey);
105+
$defaultPreset = $this->configProvider->get($defaultPresetPath);
107106
if (empty($defaultPreset)) {
108-
throw new ConfigPresetNotFoundException("No default preset configured at path: {$defaultPath}");
107+
throw new ConfigPresetNotFoundException("No default preset configured at path: {$defaultPresetPath} for group: {$this->group}");
109108
}
110109
return $defaultPreset;
111110
}

packages/config/src/ConfigResolver.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,16 @@ class ConfigResolver implements CanProvideConfig
1919
private array $cacheHas = [];
2020

2121
public static function default(): static {
22-
return (new static())->tryFrom(fn() => new SettingsConfigProvider());
22+
return (new static)->tryFrom(fn() => new SettingsConfigProvider);
2323
}
2424

2525
public static function makeWith(?CanProvideConfig $provider): static {
26-
return (new static())
27-
->tryFrom($provider)
28-
->thenFrom(fn() => new SettingsConfigProvider());
29-
}
30-
31-
public static function makeWithEmptyFallback(): static {
32-
return (new static())
33-
->tryFrom(fn() => new SettingsConfigProvider())
34-
->fallbackTo(fn() => [])
35-
->allowEmptyFallback(true);
26+
return match(true) {
27+
is_null($provider) => self::default(),
28+
$provider instanceof ConfigResolver => $provider, // prevent double wrapping
29+
$provider instanceof CanProvideConfig => (new static)->tryFrom($provider)->thenFrom(fn() => new SettingsConfigProvider),
30+
default => throw new InvalidArgumentException('Provider must be a CanProvideConfig instance or null.'),
31+
};
3632
}
3733

3834
/**

0 commit comments

Comments
 (0)