Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/ai-bundle/config/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@
->end()
->end()
->end()
->arrayNode('huggingface')
->children()
->stringNode('api_key')->isRequired()->end()
->stringNode('provider')->defaultValue('hf-inference')->end()
->stringNode('http_client')
->defaultValue('http_client')
->info('Service ID of the HTTP client to use')
->end()
->end()
->end()
->arrayNode('vertexai')
->children()
->stringNode('location')->isRequired()->end()
Expand Down
3 changes: 3 additions & 0 deletions src/ai-bundle/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Symfony\AI\Platform\Bridge\Gemini\Contract\GeminiContract;
use Symfony\AI\Platform\Bridge\Gemini\ModelCatalog as GeminiModelCatalog;
use Symfony\AI\Platform\Bridge\Gemini\TokenOutputProcessor as GeminiTokenOutputProcessor;
use Symfony\AI\Platform\Bridge\HuggingFace\Contract\HuggingFaceContract;
use Symfony\AI\Platform\Bridge\HuggingFace\ModelCatalog as HuggingFaceModelCatalog;
use Symfony\AI\Platform\Bridge\LmStudio\ModelCatalog as LmStudioModelCatalog;
use Symfony\AI\Platform\Bridge\Meta\ModelCatalog as MetaModelCatalog;
Expand Down Expand Up @@ -76,6 +77,8 @@
->factory([AnthropicContract::class, 'create'])
->set('ai.platform.contract.gemini', Contract::class)
->factory([GeminiContract::class, 'create'])
->set('ai.platform.contract.huggingface', Contract::class)
->factory([HuggingFaceContract::class, 'create'])
->set('ai.platform.contract.vertexai.gemini', Contract::class)
->factory([VertexAiGeminiContract::class, 'create'])
->set('ai.platform.contract.ollama', Contract::class)
Expand Down
22 changes: 22 additions & 0 deletions src/ai-bundle/src/AiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use Symfony\AI\Platform\Bridge\DockerModelRunner\PlatformFactory as DockerModelRunnerPlatformFactory;
use Symfony\AI\Platform\Bridge\ElevenLabs\PlatformFactory as ElevenLabsPlatformFactory;
use Symfony\AI\Platform\Bridge\Gemini\PlatformFactory as GeminiPlatformFactory;
use Symfony\AI\Platform\Bridge\HuggingFace\PlatformFactory as HuggingFacePlatformFactory;
use Symfony\AI\Platform\Bridge\LmStudio\PlatformFactory as LmStudioPlatformFactory;
use Symfony\AI\Platform\Bridge\Mistral\PlatformFactory as MistralPlatformFactory;
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory as OllamaPlatformFactory;
Expand Down Expand Up @@ -397,6 +398,27 @@ private function processPlatformConfig(string $type, array $platform, ContainerB
return;
}

if ('huggingface' === $type) {
$platformId = 'ai.platform.huggingface';
$definition = (new Definition(Platform::class))
->setFactory(HuggingFacePlatformFactory::class.'::create')
->setLazy(true)
->addTag('proxy', ['interface' => PlatformInterface::class])
->setArguments([
$platform['api_key'],
$platform['provider'],
new Reference($platform['http_client'], ContainerInterface::NULL_ON_INVALID_REFERENCE),
new Reference('ai.platform.model_catalog.huggingface'),
new Reference('ai.platform.contract.huggingface'),
new Reference('event_dispatcher'),
])
->addTag('ai.platform', ['name' => 'huggingface']);

$container->setDefinition($platformId, $definition);

return;
}

if ('vertexai' === $type && isset($platform['location'], $platform['project_id'])) {
if (!class_exists(ApplicationDefaultCredentials::class)) {
throw new RuntimeException('For using the Vertex AI platform, google/auth package is required. Try running "composer require google/auth".');
Expand Down
2 changes: 1 addition & 1 deletion src/ai-bundle/templates/data_collector.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@
{% endfor %}
</ol>
{% else %}
{{ call.result }}
{{ dump(call.result) }}
{% endif %}
{% if call.metadata|length > 0 %}
<br />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\AI\Platform\Bridge\HuggingFace\Contract;

use Symfony\AI\Platform\Contract;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class HuggingFaceContract extends Contract
{
public static function create(NormalizerInterface ...$normalizer): Contract
{
return parent::create(
new FileNormalizer(),
new MessageBagNormalizer(),
...$normalizer,
);
}
}
12 changes: 5 additions & 7 deletions src/platform/src/Bridge/HuggingFace/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
namespace Symfony\AI\Platform\Bridge\HuggingFace;

use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\AI\Platform\Bridge\HuggingFace\Contract\FileNormalizer;
use Symfony\AI\Platform\Bridge\HuggingFace\Contract\MessageBagNormalizer;
use Symfony\AI\Platform\Bridge\HuggingFace\Contract\HuggingFaceContract;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
use Symfony\AI\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -28,6 +28,7 @@ public static function create(
#[\SensitiveParameter] string $apiKey,
string $provider = Provider::HF_INFERENCE,
?HttpClientInterface $httpClient = null,
ModelCatalogInterface $modelCatalog = new ModelCatalog(),
?Contract $contract = null,
?EventDispatcherInterface $eventDispatcher = null,
): Platform {
Expand All @@ -36,11 +37,8 @@ public static function create(
return new Platform(
[new ModelClient($httpClient, $provider, $apiKey)],
[new ResultConverter()],
new ModelCatalog(),
$contract ?? Contract::create(
new FileNormalizer(),
new MessageBagNormalizer(),
),
$modelCatalog,
$contract ?? HuggingFaceContract::create(),
$eventDispatcher,
);
}
Expand Down