moe-mizrak / laravel-openrouter

Laravel package for OpenRouter (A unified interface for LLMs)
https://moe-mizrak.gitbook.io/laravel-openrouter/
MIT License
10 stars 4 forks source link

Trouble using Facade #23

Closed Sarimarcus closed 19 hours ago

Sarimarcus commented 1 day ago

Hello, thanks for the package! I'm having this error when using the Facade : "A facade root has not been set".

This is part of my code :

<?php
namespace App\Services\AI;

use App\Services\AI;
use MoeMizrak\LaravelOpenrouter\Facades\LaravelOpenRouter;
use MoeMizrak\LaravelOpenrouter\DTO\ChatData;
use MoeMizrak\LaravelOpenrouter\DTO\MessageData;
use MoeMizrak\LaravelOpenrouter\Types\RoleType;

class OpenRouterEngine extends AI
{
    public function executePrompt($prompt)
    {
        $model = 'mistralai/mistral-7b-instruct:free';
        $messageData = new MessageData([
            'content' => $prompt,
            'role' => RoleType::USER,
        ]);

        $chatData = new ChatData([
            'messages' => [
                $messageData,
            ],
            'model' => $model,
            'max_tokens' => 100, // Adjust this value as needed
        ]);

        $chatResponse = LaravelOpenRouter::chatRequest($chatData);

        return $chatResponse;
    }
}

I've tried to add the Facade in the aliases, but still no luck:

'providers' => [
    // ...
    MoeMizrak\LaravelOpenrouter\OpenRouterServiceProvider::class,
],

'aliases' => [
    // ...
    'LaravelOpenRouter' => MoeMizrak\LaravelOpenrouter\Facades\LaravelOpenRouter::class,
]

Sorry for the question, I'm quite new to Laravel, I'm probably doing something wrong...

Thanks for your help anyway

Sarimarcus commented 19 hours ago

The error occurs because the unit test is using PHPUnit\Framework\TestCase instead of Laravel's Tests\TestCase, which means Laravel's container and facades aren't initialized....