Skip to content

Commit b6717bc

Browse files
committed
Fixes after ongoing refactoring
1 parent ee80e47 commit b6717bc

File tree

20 files changed

+286
-148
lines changed

20 files changed

+286
-148
lines changed

config/debug.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828
'http_responseStreamByLine' => false, // dump stream data as full lines (true) or as raw received chunks (false)
2929
],
3030

31+
'url-only' => [
32+
'http_enabled' => true, // enable/disable debug
33+
'http_trace' => false, // dump HTTP trace information (available for some clients - e.g. Guzzle)
34+
'http_requestUrl' => true, // dump request URL to console
35+
'http_requestHeaders' => false, // dump request headers to console
36+
'http_requestBody' => false, // dump request body to console
37+
'http_responseHeaders' => false, // dump response headers to console
38+
'http_responseBody' => false, // dump response body to console
39+
'http_responseStream' => false, // dump stream data to console
40+
'http_responseStreamByLine' => false, // dump stream data as full lines (true) or as raw received chunks (false)
41+
],
42+
3143
'off' => [
3244
'http_enabled' => false, // enable/disable debug
3345
'http_trace' => false, // dump HTTP trace information (available for some clients - e.g. Guzzle)

evals/LLMModes/run.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
),
4343
);
4444

45-
//Debug::setEnabled();
4645
$presets = array_keys(Settings::get('llm', 'presets'));
4746
//$presets = [
4847
// 'deepseek-r', // stream > text - will be solved by removing outputmode

evals/SimpleExtraction/run.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
responseModel: Company::class,
2222
);
2323

24-
//Debug::setEnabled();
25-
2624
$experiment = new Experiment(
2725
cases: InferenceCases::except(
2826
presets: ['ollama'],

evals/UseExamples/run.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
responseModel: Company::class,
2222
);
2323

24-
//Debug::setEnabled();
25-
2624
$experiment = new Experiment(
2725
cases: InferenceCases::except(
2826
presets: ['ollama'],

examples/A02_Advanced/ConfigProviders/run.php

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,41 @@ class User {
3333
// Let's build a set of custom configuration providers.
3434
// You can use those examples to build your framework-specific providers.
3535

36+
3637
class CustomConfigProvider implements CanProvideConfig
3738
{
3839
public function getConfig(string $group, ?string $preset = ''): array {
3940
return match($group) {
40-
'http' => $this->http() ?? [],
41-
'debug' => $this->debug()[$preset] ?? [],
42-
'llm' => $this->llm() ?? [],
43-
'structured' => $this->struct() ?? [],
41+
'http' => $this->http($preset),
42+
'debug' => $this->debug($preset),
43+
'llm' => $this->llm($preset),
4444
default => [],
4545
};
4646
}
4747

48-
private function llm() : array {
49-
return [
50-
'apiUrl' => 'https://api.deepseek.com',
51-
'apiKey' => Env::get('DEEPSEEK_API_KEY'),
52-
'endpoint' => '/chat/completions',
53-
'defaultModel' => 'deepseek-chat',
54-
'defaultMaxTokens' => 128,
55-
'driver' => 'deepseek',
56-
// ...
57-
];
58-
}
59-
60-
private function http() : array {
61-
return [
62-
'driver' => 'symfony',
63-
'connectTimeout' => 10,
64-
'requestTimeout' => 30,
65-
'idleTimeout' => -1,
66-
'maxConcurrent' => 5,
67-
'poolTimeout' => 60,
68-
'failOnError' => true,
48+
private function http(?string $preset) : array {
49+
$config = [
50+
'default' => 'symfony',
51+
'presets' => [
52+
'symfony' => [
53+
'driver' => 'symfony',
54+
'connectTimeout' => 10,
55+
'requestTimeout' => 30,
56+
'idleTimeout' => -1,
57+
'maxConcurrent' => 5,
58+
'poolTimeout' => 60,
59+
'failOnError' => true,
60+
]
61+
//
62+
],
6963
];
64+
$default = $config['default'];
65+
$preset = $preset ?: $default;
66+
return $config['presets'][$preset] ?? $config['presets'][$default] ?? [];
7067
}
7168

72-
private function debug(): array {
73-
return [
69+
private function debug(?string $preset): array {
70+
$data = [
7471
'off' => [
7572
'httpEnabled' => true,
7673
],
@@ -84,11 +81,31 @@ private function debug(): array {
8481
'httpResponseBody' => true,
8582
'httpResponseStream' => true,
8683
'httpResponseStreamByLine' => true,
87-
]
84+
],
85+
// ...
86+
];
87+
$preset = $preset ?: 'off';
88+
return $data[$preset] ?? $data['off'];
89+
}
90+
91+
private function llm(?string $preset): array {
92+
$data = [
93+
'deepseek' => [
94+
'apiUrl' => 'https://api.deepseek.com',
95+
'apiKey' => Env::get('DEEPSEEK_API_KEY'),
96+
'endpoint' => '/chat/completions',
97+
'defaultModel' => 'deepseek-chat',
98+
'defaultMaxTokens' => 128,
99+
'driver' => 'deepseek',
100+
'httpClientPreset' => 'symfony',
101+
],
102+
// ...
88103
];
104+
$preset = $preset ?: 'deepseek';
105+
return $data[$preset] ?? $data['deepseek'];
89106
}
90107

91-
private function struct(): array {
108+
private function struct(?string $preset): array {
92109
return [
93110
'outputMode' => OutputMode::Tools,
94111
'useObjectReferences' => true,
@@ -120,6 +137,7 @@ private function struct(): array {
120137
];
121138
}
122139
}
140+
123141
$configProvider = new CustomConfigProvider();
124142

125143
$customClient = (new HttpClientBuilder(

examples/A05_Extras/ComplexExtractionGemini/run.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ enum StakeholderRole: string {
9595
case Other = 'other';
9696
}
9797

98-
//Debug::setEnabled();
9998
$structuredOutput = (new StructuredOutput)->using('gemini');
10099

101100
echo "PROJECT EVENTS:\n\n";

examples/B02_LLMAdvanced/ConfigProviders/run.php

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
require 'examples/boot.php';
1616

1717
use Cognesy\Http\HttpClientBuilder;
18-
use Cognesy\Polyglot\LLM\Config\LLMConfig;
1918
use Cognesy\Polyglot\LLM\Inference;
2019
use Cognesy\Utils\Config\Contracts\CanProvideConfig;
2120
use Cognesy\Utils\Config\Env;
@@ -30,26 +29,36 @@ class CustomConfigProvider implements CanProvideConfig
3029
{
3130
public function getConfig(string $group, ?string $preset = ''): array {
3231
return match($group) {
33-
'http' => $this->httpPresets(),
34-
'debug' => $this->debugPresets()[$preset] ?? [],
32+
'http' => $this->http($preset),
33+
'debug' => $this->debug($preset),
34+
'llm' => $this->llm($preset),
3535
default => [],
3636
};
3737
}
3838

39-
private function httpPresets() : array {
40-
return [
41-
'driver' => 'symfony',
42-
'connectTimeout' => 10,
43-
'requestTimeout' => 30,
44-
'idleTimeout' => -1,
45-
'maxConcurrent' => 5,
46-
'poolTimeout' => 60,
47-
'failOnError' => true,
39+
private function http(?string $preset) : array {
40+
$config = [
41+
'default' => 'symfony',
42+
'presets' => [
43+
'symfony' => [
44+
'driver' => 'symfony',
45+
'connectTimeout' => 10,
46+
'requestTimeout' => 30,
47+
'idleTimeout' => -1,
48+
'maxConcurrent' => 5,
49+
'poolTimeout' => 60,
50+
'failOnError' => true,
51+
]
52+
//
53+
],
4854
];
55+
$default = $config['default'];
56+
$preset = $preset ?: $default;
57+
return $config['presets'][$preset] ?? $config['presets'][$default] ?? [];
4958
}
5059

51-
private function debugPresets(): array {
52-
return [
60+
private function debug(?string $preset): array {
61+
$data = [
5362
'off' => [
5463
'httpEnabled' => true,
5564
],
@@ -63,20 +72,28 @@ private function debugPresets(): array {
6372
'httpResponseBody' => true,
6473
'httpResponseStream' => true,
6574
'httpResponseStreamByLine' => true,
66-
]
75+
],
76+
// ...
6777
];
78+
$preset = $preset ?: 'off';
79+
return $data[$preset] ?? $data['off'];
6880
}
6981

70-
private function presets(): array {
71-
return [
72-
'deepseek' => new LLMConfig(
73-
apiUrl : 'https://api.deepseek.com',
74-
apiKey : Env::get('DEEPSEEK_API_KEY'),
75-
endpoint: '/chat/completions', defaultModel: 'deepseek-chat', defaultMaxTokens: 128,
76-
driver : 'deepseek',
77-
),
82+
private function llm(?string $preset): array {
83+
$data = [
84+
'deepseek' => [
85+
'apiUrl' => 'https://api.deepseek.com',
86+
'apiKey' => Env::get('DEEPSEEK_API_KEY'),
87+
'endpoint' => '/chat/completions',
88+
'defaultModel' => 'deepseek-chat',
89+
'defaultMaxTokens' => 128,
90+
'driver' => 'deepseek',
91+
'httpClientPreset' => 'symfony',
92+
],
7893
// ...
7994
];
95+
$preset = $preset ?: 'deepseek';
96+
return $data[$preset] ?? $data['deepseek'];
8097
}
8198
}
8299

examples/B02_LLMAdvanced/ContextCacheLLM/run.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
$data = file_get_contents(__DIR__ . '/../../../README.md');
3434

3535
$inference = (new Inference)
36+
//->wiretap(fn($e) => $e->print()) // wiretap to print all events
37+
//->withDebugPreset('on') // debug HTTP traffic
3638
->using('anthropic')
3739
->withCachedContext(
3840
messages: [
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
title: 'Embeddings utils'
3+
docname: 'embed_utils'
4+
---
5+
6+
## Overview
7+
8+
`EmbedUtils` class offers convenient methods to find top K vectors
9+
or documents most similar to provided query.
10+
11+
Check out the `EmbedUtils` class for more details.
12+
- `EmbedUtils::findTopK()`
13+
- `EmbedUtils::findSimilar()`
14+
15+
Embeddings providers access details can be found and modified via
16+
`/config/embed.php`.
17+
18+
19+
## Example
20+
21+
```php
22+
<?php
23+
require 'examples/boot.php';
24+
25+
use Cognesy\Polyglot\Embeddings\EmbeddingsProvider;
26+
use Cognesy\Polyglot\Embeddings\EmbedUtils;
27+
28+
$documents = [
29+
'Computer vision models are used to analyze images and videos.',
30+
'The bakers at the Nashville Bakery baked 200 loaves of bread on Monday morning.',
31+
'The new movie starring Tom Hanks is now playing in theaters.',
32+
'Famous soccer player Lionel Messi has arrived in town.',
33+
'News about the latest iPhone model has been leaked.',
34+
'New car model by Tesla is now available for pre-order.',
35+
'Philip K. Dick is an author of many sci-fi novels.',
36+
];
37+
38+
$query = "technology news";
39+
40+
$presets = [
41+
'azure',
42+
'cohere2',
43+
'gemini',
44+
'jina',
45+
'mistral',
46+
//'ollama',
47+
'openai'
48+
];
49+
50+
foreach($presets as $preset) {
51+
$bestMatches = EmbedUtils::findSimilar(
52+
provider: EmbeddingsProvider::using($preset),
53+
query: $query,
54+
documents: $documents,
55+
topK: 3
56+
);
57+
58+
echo "\n[$preset]\n";
59+
dump($bestMatches);
60+
}
61+
?>
62+
```

0 commit comments

Comments
 (0)