Skip to content

Commit 1aa713e

Browse files
committed
Add Hugging Face as Platform to Bundle config
1 parent 119260f commit 1aa713e

File tree

6 files changed

+68
-8
lines changed

6 files changed

+68
-8
lines changed

src/ai-bundle/config/options.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@
8383
->end()
8484
->end()
8585
->end()
86+
->arrayNode('huggingface')
87+
->children()
88+
->stringNode('api_key')->isRequired()->end()
89+
->stringNode('provider')->defaultValue('hf-inference')->end()
90+
->stringNode('http_client')
91+
->defaultValue('http_client')
92+
->info('Service ID of the HTTP client to use')
93+
->end()
94+
->end()
95+
->end()
8696
->arrayNode('vertexai')
8797
->children()
8898
->stringNode('location')->isRequired()->end()

src/ai-bundle/config/services.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Symfony\AI\Platform\Bridge\Gemini\Contract\GeminiContract;
3737
use Symfony\AI\Platform\Bridge\Gemini\ModelCatalog as GeminiModelCatalog;
3838
use Symfony\AI\Platform\Bridge\Gemini\TokenOutputProcessor as GeminiTokenOutputProcessor;
39+
use Symfony\AI\Platform\Bridge\HuggingFace\Contract\HuggingFaceContract;
3940
use Symfony\AI\Platform\Bridge\HuggingFace\ModelCatalog as HuggingFaceModelCatalog;
4041
use Symfony\AI\Platform\Bridge\LmStudio\ModelCatalog as LmStudioModelCatalog;
4142
use Symfony\AI\Platform\Bridge\Meta\ModelCatalog as MetaModelCatalog;
@@ -76,6 +77,8 @@
7677
->factory([AnthropicContract::class, 'create'])
7778
->set('ai.platform.contract.gemini', Contract::class)
7879
->factory([GeminiContract::class, 'create'])
80+
->set('ai.platform.contract.huggingface', Contract::class)
81+
->factory([HuggingFaceContract::class, 'create'])
7982
->set('ai.platform.contract.vertexai.gemini', Contract::class)
8083
->factory([VertexAiGeminiContract::class, 'create'])
8184
->set('ai.platform.contract.ollama', Contract::class)

src/ai-bundle/src/AiBundle.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
use Symfony\AI\Platform\Bridge\DockerModelRunner\PlatformFactory as DockerModelRunnerPlatformFactory;
5252
use Symfony\AI\Platform\Bridge\ElevenLabs\PlatformFactory as ElevenLabsPlatformFactory;
5353
use Symfony\AI\Platform\Bridge\Gemini\PlatformFactory as GeminiPlatformFactory;
54+
use Symfony\AI\Platform\Bridge\HuggingFace\PlatformFactory as HuggingFacePlatformFactory;
5455
use Symfony\AI\Platform\Bridge\LmStudio\PlatformFactory as LmStudioPlatformFactory;
5556
use Symfony\AI\Platform\Bridge\Mistral\PlatformFactory as MistralPlatformFactory;
5657
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory as OllamaPlatformFactory;
@@ -397,6 +398,27 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
397398
return;
398399
}
399400

401+
if ('huggingface' === $type) {
402+
$platformId = 'ai.platform.huggingface';
403+
$definition = (new Definition(Platform::class))
404+
->setFactory(HuggingFacePlatformFactory::class.'::create')
405+
->setLazy(true)
406+
->addTag('proxy', ['interface' => PlatformInterface::class])
407+
->setArguments([
408+
$platform['api_key'],
409+
$platform['provider'],
410+
new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE),
411+
new Reference('ai.platform.model_catalog.huggingface'),
412+
new Reference('ai.platform.contract.huggingface'),
413+
new Reference('event_dispatcher'),
414+
])
415+
->addTag('ai.platform', ['name' => 'huggingface']);
416+
417+
$container->setDefinition($platformId, $definition);
418+
419+
return;
420+
}
421+
400422
if ('vertexai' === $type && isset($platform['location'], $platform['project_id'])) {
401423
if (!class_exists(ApplicationDefaultCredentials::class)) {
402424
throw new RuntimeException('For using the Vertex AI platform, google/auth package is required. Try running "composer require google/auth".');

src/ai-bundle/templates/data_collector.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@
166166
{% endfor %}
167167
</ol>
168168
{% else %}
169-
{{ call.result }}
169+
{{ dump(call.result) }}
170170
{% endif %}
171171
{% if call.metadata|length > 0 %}
172172
<br />
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\AI\Platform\Bridge\HuggingFace\Contract;
13+
14+
use Symfony\AI\Platform\Contract;
15+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
16+
17+
final class HuggingFaceContract extends Contract
18+
{
19+
public static function create(NormalizerInterface ...$normalizer): Contract
20+
{
21+
return parent::create(
22+
new FileNormalizer(),
23+
new MessageBagNormalizer(),
24+
...$normalizer,
25+
);
26+
}
27+
}

src/platform/src/Bridge/HuggingFace/PlatformFactory.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
namespace Symfony\AI\Platform\Bridge\HuggingFace;
1313

1414
use Psr\EventDispatcher\EventDispatcherInterface;
15-
use Symfony\AI\Platform\Bridge\HuggingFace\Contract\FileNormalizer;
16-
use Symfony\AI\Platform\Bridge\HuggingFace\Contract\MessageBagNormalizer;
15+
use Symfony\AI\Platform\Bridge\HuggingFace\Contract\HuggingFaceContract;
1716
use Symfony\AI\Platform\Contract;
17+
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
1818
use Symfony\AI\Platform\Platform;
1919
use Symfony\Component\HttpClient\EventSourceHttpClient;
2020
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -28,6 +28,7 @@ public static function create(
2828
#[\SensitiveParameter] string $apiKey,
2929
string $provider = Provider::HF_INFERENCE,
3030
?HttpClientInterface $httpClient = null,
31+
ModelCatalogInterface $modelCatalog = new ModelCatalog(),
3132
?Contract $contract = null,
3233
?EventDispatcherInterface $eventDispatcher = null,
3334
): Platform {
@@ -36,11 +37,8 @@ public static function create(
3637
return new Platform(
3738
[new ModelClient($httpClient, $provider, $apiKey)],
3839
[new ResultConverter()],
39-
new ModelCatalog(),
40-
$contract ?? Contract::create(
41-
new FileNormalizer(),
42-
new MessageBagNormalizer(),
43-
),
40+
$modelCatalog,
41+
$contract ?? HuggingFaceContract::create(),
4442
$eventDispatcher,
4543
);
4644
}

0 commit comments

Comments
 (0)