PHP code example of saqqal / llm-integration-bundle

1. Go to this page and download the library: Download saqqal/llm-integration-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

saqqal / llm-integration-bundle example snippets



return [
    // ...
    Saqqal\LlmIntegrationBundle\LlmIntegrationBundle::class => ['all' => true],
];

use Saqqal\LlmIntegrationBundle\Interface\AiServiceInterface;

class YourService
{
    private AiServiceInterface $aiService;

    public function __construct(AiServiceInterface $aiService)
    {
        $this->aiService = $aiService;
    }

    // ...
}

public function generateResponse(string $prompt): string
{
    $response = $this->aiService->generate($prompt);
    return $response->getData()['content'];
}

public function generateDynamicResponse(string $prompt): mixed
{
    $response = $this->aiService->generate($prompt, [], true);
    return $response->choices[0]->message->content;
}

use Saqqal\LlmIntegrationBundle\Attribute\AiClient;
use Saqqal\LlmIntegrationBundle\Client\AbstractAiClient;

#[AiClient('your_provider')]
class YourProviderClient extends AbstractAiClient
{
    protected function getApiUrl(): string
    {
        return 'https://api.yourprovider.com/v1/chat/completions';
    }

    protected function getAdditionalRequestData(string $prompt, ?string $model): array
    {
        return [
            // Add provider-specific options here
        ];
    }
}

use Saqqal\LlmIntegrationBundle\Event\LlmIntegrationExceptionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class LlmIntegrationExceptionSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            LlmIntegrationExceptionEvent::class => 'onLlmIntegrationException',
        ];
    }

    public function onLlmIntegrationException(LlmIntegrationExceptionEvent $event): void
    {
        $exception = $event->getException();
        // Handle the exception
    }
}
bash
php bin/console llm:list-ai-services