Skip to content

Commit 02d4a18

Browse files
committed
feat(models): add Yandex Foundation Models support and model-specific variables
- Introduce `YandexDriver` for synchronous Yandex Foundation Models API - Add `variables` section in `models.yaml` and `ModelRegistry#getVariables()` - Extend `Bblslug::runFromCli()` and `translate()` to merge env- and CLI-provided variables - Update CLI help (`--variables`) and README with example usage - Add tests in `ModelRegistryTest` for `getVariables()` on Yandex and other models - Populate all Yandex model entries (`gpt-lite`, `gpt-pro`, `gpt-32k`, `llama-lite`, `llama`) in `resources/models.yaml`
1 parent 167aa61 commit 02d4a18

File tree

7 files changed

+412
-1
lines changed

7 files changed

+412
-1
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ APIs supported:
2323
- `openai:gpt-4-turbo` - OpenAI GPT-4 Turbo
2424
- `openai:gpt-4o` - OpenAI GPT-4o
2525
- `openai:gpt-4o-mini` - OpenAI GPT-4o Mini
26+
- Yandex:
27+
- `yandex:gpt-lite` - YandexGPT Lite
28+
- `yandex:gpt-pro` - YandexGPT Pro
29+
- `yandex:gpt-32k` - YandexGPT Pro 32K
2630

2731
## Features
2832

@@ -53,8 +57,11 @@ chmod +x vendor/bin/bblslug
5357
export DEEPL_PRO_API_KEY=...
5458
export GOOGLE_API_KEY=...
5559
export OPENAI_API_KEY=...
60+
export YANDEX_API_KEY=... && export YANDEX_FOLDER_ID=...
5661
```
5762

63+
**NB!** Some vendors require additional parameters for client authentication (like Yandex)
64+
5865
3. **Input / output**:
5966

6067
- If `--source` is omitted, Bblslug reads from **STDIN**.
@@ -162,6 +169,18 @@ vendor/bin/bblslug \
162169
--translated=out.html
163170
```
164171

172+
### Use additional model/vendor-specific options
173+
174+
```bash
175+
vendor/bin/bblslug \
176+
--model=vendor:name \
177+
--variables=some=XXX,other=YYY \
178+
--format=html \
179+
--verbose \
180+
--source=input.html \
181+
--translated=out.html
182+
```
183+
165184
## PHP Library Usage
166185

167186
You can embed Bblslug in your PHP project:
@@ -189,6 +208,7 @@ $result = Bblslug::translate(
189208
proxy: getenv('BBLSLUG_PROXY'), // Optional proxy URI (http://..., socks5h://...)
190209
sourceLang: null, // Source language code (optional; autodetect if null)
191210
targetLang: null, // Target language code (optional; default from driver settings)
211+
variables: TBD, // TBD
192212
verbose: true, // If true, prints debug request/response to stderr
193213
);
194214

resources/models.yaml

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,138 @@
347347
defaults:
348348
model: gpt-4o-mini
349349
temperature: 0.0
350+
351+
'yandex:gpt-lite':
352+
vendor: yandex
353+
name: 'Yandex GPT Lite'
354+
endpoint: 'https://llm.api.cloud.yandex.net/foundationModels/v1/completion'
355+
format: text
356+
limits:
357+
max_tokens: 8192
358+
token_estimator: gpt
359+
estimated_max_chars: 32768
360+
requirements:
361+
auth:
362+
help_url: 'https://yandex.cloud/en/docs/foundation-models/api-ref/authentication'
363+
type: header
364+
key_name: Authorization
365+
prefix: Bearer
366+
env: YANDEX_API_KEY
367+
variables:
368+
folder_id: YANDEX_FOLDER_ID
369+
headers:
370+
- 'Content-Type: application/json'
371+
defaults:
372+
model: yandexgpt-lite/latest
373+
temperature: 0.0
374+
max_tokens: 8192
375+
http_error_handling: true
376+
notes: 'Low-latency model with up to 8 k tokens sync or 32 k async.'
377+
378+
'yandex:gpt-pro':
379+
vendor: yandex
380+
name: 'Yandex GPT Pro'
381+
endpoint: 'https://llm.api.cloud.yandex.net/foundationModels/v1/completion'
382+
format: text
383+
limits:
384+
max_tokens: 8192
385+
token_estimator: gpt
386+
estimated_max_chars: 32768
387+
requirements:
388+
auth:
389+
help_url: 'https://yandex.cloud/en/docs/foundation-models/api-ref/authentication'
390+
type: header
391+
key_name: Authorization
392+
prefix: Bearer
393+
env: YANDEX_API_KEY
394+
variables:
395+
folder_id: YANDEX_FOLDER_ID
396+
headers:
397+
- 'Content-Type: application/json'
398+
defaults:
399+
model: yandexgpt/latest
400+
temperature: 0.0
401+
max_tokens: 8192
402+
http_error_handling: true
403+
notes: 'High-capacity 5th-gen model, up to 8 k tokens sync or 32 k async.'
404+
405+
'yandex:gpt-32k':
406+
vendor: yandex
407+
name: 'Yandex GPT Pro 32K'
408+
endpoint: 'https://llm.api.cloud.yandex.net/foundationModels/v1/completion'
409+
format: text
410+
limits:
411+
max_tokens: 32000
412+
token_estimator: gpt
413+
estimated_max_chars: 128000
414+
requirements:
415+
auth:
416+
help_url: 'https://yandex.cloud/en/docs/foundation-models/api-ref/authentication'
417+
type: header
418+
key_name: Authorization
419+
prefix: Bearer
420+
env: YANDEX_API_KEY
421+
variables:
422+
folder_id: YANDEX_FOLDER_ID
423+
headers:
424+
- 'Content-Type: application/json'
425+
defaults:
426+
model: yandexgpt-32k/latest
427+
temperature: 0.0
428+
max_tokens: 32000
429+
http_error_handling: true
430+
notes: 'Extended-context model: up to 32 k tokens (sync only).'
431+
432+
'yandex:llama-lite':
433+
vendor: yandex
434+
name: 'Llama 8B (Lite)'
435+
endpoint: 'https://llm.api.cloud.yandex.net/foundationModels/v1/completion'
436+
format: text
437+
limits:
438+
max_tokens: 8192
439+
token_estimator: gpt
440+
estimated_max_chars: 32768
441+
requirements:
442+
auth:
443+
help_url: 'https://yandex.cloud/en/docs/foundation-models/api-ref/authentication'
444+
type: header
445+
key_name: Authorization
446+
prefix: Bearer
447+
env: YANDEX_API_KEY
448+
variables:
449+
folder_id: YANDEX_FOLDER_ID
450+
headers:
451+
- 'Content-Type: application/json'
452+
defaults:
453+
model: llama-lite/latest
454+
temperature: 0.0
455+
max_tokens: 8192
456+
http_error_handling: true
457+
notes: 'Meta Llama 8 B variant, up to 8 k tokens sync or async.'
458+
459+
'yandex:llama':
460+
vendor: yandex
461+
name: 'Llama 70B'
462+
endpoint: 'https://llm.api.cloud.yandex.net/foundationModels/v1/completion'
463+
format: text
464+
limits:
465+
max_tokens: 8192
466+
token_estimator: gpt
467+
estimated_max_chars: 32768
468+
requirements:
469+
auth:
470+
help_url: 'https://yandex.cloud/en/docs/foundation-models/api-ref/authentication'
471+
type: header
472+
key_name: Authorization
473+
prefix: Bearer
474+
env: YANDEX_API_KEY
475+
variables:
476+
folder_id: YANDEX_FOLDER_ID
477+
headers:
478+
- 'Content-Type: application/json'
479+
defaults:
480+
model: llama/latest
481+
temperature: 0.0
482+
max_tokens: 8192
483+
http_error_handling: true
484+
notes: 'Full-scale Meta Llama 70 B, up to 8 k tokens sync or async.'

src/Bblslug/Bblslug.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Bblslug
2222
* @param string|null $proxy Optional proxy URL (from env or CLI).
2323
* @param string|null $sourceLang Optional source language code.
2424
* @param string|null $targetLang Optional target language code.
25+
* @param array<string,string> $variables Model-specific vars (e.g. ['some'=>'...'])
2526
* @param bool $verbose If true: include request/response logs.
2627
*
2728
* @return array{
@@ -51,6 +52,7 @@ public static function translate(
5152
?string $proxy = null,
5253
?string $sourceLang = null,
5354
?string $targetLang = null,
55+
array $variables = [],
5456
bool $verbose = false
5557
): array {
5658
// Validate model
@@ -84,11 +86,21 @@ public static function translate(
8486
$prepared = $filterManager->apply($text);
8587
$preparedLength = mb_strlen($prepared);
8688

89+
// Prepare options for driver, merging in any CLI-provided variables
90+
$options = array_merge(
91+
['format' => $format, 'dryRun' => $dryRun, 'verbose' => $verbose],
92+
$variables
93+
);
94+
95+
if ($context !== null) {
96+
$options['context'] = $context;
97+
}
98+
8799
// Build request
88100
$req = $driver->buildRequest(
89101
$model,
90102
$prepared,
91-
['format' => $format, 'dryRun' => $dryRun, 'verbose' => $verbose]
103+
$options
92104
);
93105

94106
// API auth key handling
@@ -209,6 +221,7 @@ public static function runFromCli()
209221
"source-lang:", // override source language
210222
"target-lang:", // override target language
211223
"translated:", // output file (default = STDOUT)
224+
"variables:", // model-specific overrides
212225
"verbose", // enable debug logs
213226
]);
214227

@@ -282,6 +295,19 @@ public static function runFromCli()
282295
);
283296
}
284297

298+
// Parse CLI --variables (takes precedence over env)
299+
$cliVars = [];
300+
if (!empty($options['variables'])) {
301+
foreach (explode(',', $options['variables']) as $pair) {
302+
[$k, $v] = array_map('trim', explode('=', $pair, 2)) + [null, null];
303+
if ($k !== null && $v !== null) {
304+
$cliVars[$k] = $v;
305+
} else {
306+
Help::error("Invalid --variables format: '{$pair}'");
307+
}
308+
}
309+
}
310+
285311
// in interactive mode without --source, warn user about STDIN and EOF
286312
if ($sourceFile === null && function_exists('stream_isatty') && stream_isatty(STDIN)) {
287313
Help::warning("Reading from STDIN; press Ctrl-D to finish input and continue.");
@@ -341,6 +367,18 @@ public static function runFromCli()
341367
// Read optional proxy setting (CLI flag has priority)
342368
$proxy = $options['proxy'] ?? getenv('BBLSLUG_PROXY') ?: null;
343369

370+
// Collect any model-specific variables
371+
$variables = [];
372+
foreach ($registry->getVariables($modelKey) as $name => $envVar) {
373+
$val = getenv($envVar);
374+
if ($val === false) {
375+
Help::error("Missing required env var {$envVar} for model {$modelKey}");
376+
}
377+
$variables[$name] = $val;
378+
}
379+
// Override by CLI
380+
$variables = array_merge($variables, $cliVars);
381+
344382
// Perform translation
345383
$res = [];
346384
try {
@@ -355,6 +393,7 @@ public static function runFromCli()
355393
proxy: $proxy,
356394
sourceLang: $sourceLang,
357395
targetLang: $targetLang,
396+
variables: $variables,
358397
verbose: $verbose,
359398
);
360399
} catch (\Throwable $e) {

src/Bblslug/Help.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public static function printHelp(?int $exitCode = 1): void
5252
echo "\t{$bold}--source-lang=LANG{$reset} Source language code (e.g. EN, DE) - default autodetect\n";
5353
echo "\t{$bold}--target-lang=LANG{$reset} Target language (e.g. EN, DE) - default EN\n";
5454
echo "\t{$bold}--translated=FILE{$reset} Output file for translated content (omit to write to STDOUT)\n";
55+
echo "\t{$bold}--variables=K=V,...{$reset} Comma-separated model-specific overrides\n";
56+
echo "\t{$bold}--variables=k=v[,k2=v2,...]{$reset} Comma-separated list of model-specific variables\n";
5557
echo "\t{$bold}--verbose{$reset} Show extra debug info after processing\n";
5658

5759
echo "\nEnvironment:\n";
@@ -63,6 +65,7 @@ public static function printHelp(?int $exitCode = 1): void
6365
echo "\t export OPENAI_API_KEY=\"...\" # for OpenAI (GPT)\n";
6466
echo "\t (See each model's required variable with --list-models)\n";
6567
echo "\tSome models may not require API keys at all.\n";
68+
echo "\tModel-specific variables can be passed via --variables or read from env\n";
6669
echo "\n\tYou may also set a proxy globally via the BBLSLUG_PROXY environment variable:\n";
6770
echo "\t export BBLSLUG_PROXY=\"http://localhost:3128\" # HTTP proxy\n";
6871
echo "\t export BBLSLUG_PROXY=\"socks5h://127.0.0.1:9050\" # SOCKS5 proxy\n";
@@ -90,6 +93,11 @@ public static function printHelp(?int $exitCode = 1): void
9093
echo "\tphp bblslug.php --model=deepl:pro --format=html \\\n";
9194
echo "\t --source=doc.html --translated=out.html --filters=url,html_code,html_pre\n";
9295

96+
echo "\n\tUsing model-specific variables (comma-separated):\n";
97+
echo "\tphp bblslug.php --model=yandex:gpt-lite --format=text \\\n";
98+
echo "\t --variables=folder_id=...,foo=bar \\\n";
99+
echo "\t --source=in.txt --translated=out.txt\n";
100+
93101
echo "\n\tUse HTTP proxy\n";
94102
echo "\tphp bblslug.php --model=openai:gpt-4o --format=text \\\n";
95103
echo "\t --source=in.txt --translated=out.txt \\\n";

0 commit comments

Comments
 (0)