Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Ai Lm Studio Platform Laravel Package

symfony/ai-lm-studio-platform

Symfony AI bridge for LM Studio. Connect to LM Studio’s OpenAI-compatible local endpoints to run and test LLMs from Symfony applications. Part of the Symfony AI ecosystem; issues and PRs are handled in the main symfony/ai repository.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to First Use

  1. Install the Package Add to your composer.json:

    composer require symfony/ai-lm-studio-platform
    
  2. Configure LM Studio Provider Update config/packages/ai.yaml:

    framework:
        ai:
            providers:
                lm_studio:
                    platform: lm_studio
                    endpoint: http://localhost:1234/v1/  # Default LM Studio OpenAI endpoint
                    model: "TheBloke/Llama-2-7B-Chat-GGUF"  # Your LM Studio model
                    auth: null  # LM Studio typically doesn't require auth
    
  3. First API Call Use Symfony AI’s ChatCompletion service in a controller or command:

    use Symfony\AI\Chat\ChatCompletion;
    use Symfony\AI\Message;
    
    public function __invoke(ChatCompletion $chatCompletion)
    {
        $response = $chatCompletion->create([
            'model' => 'lm_studio',  // Route to LM Studio provider
            'messages' => [
                new Message('user', 'Explain Laravel dependency injection in 3 bullet points.')
            ],
        ]);
    
        return $response->getChoices()[0]->getMessage()->getContent();
    }
    
  4. Verify LM Studio is Running Ensure LM Studio is deployed locally (e.g., via Docker) and accessible at the configured endpoint. Test with:

    curl http://localhost:1234/v1/models
    

Where to Look First

First Use Case: Local AI Prototyping

Replace cloud-based AI calls (e.g., OpenAI) with LM Studio for offline development:

// Before (OpenAI)
$response = $chatCompletion->create(['model' => 'gpt-3.5-turbo', ...]);

// After (LM Studio)
$response = $chatCompletion->create(['model' => 'lm_studio', ...]);

Use Case: Test AI features without API costs or latency during local development.


Implementation Patterns

Core Workflow: Provider Routing

Leverage Symfony AI’s provider abstraction (v0.8.0+) to dynamically route requests:

# config/packages/ai.yaml
framework:
    ai:
        providers:
            lm_studio:
                platform: lm_studio
                endpoint: http://localhost:1234/v1/
                model: "mistral-7b"
            openai:
                platform: openai
                api_key: "%env(OPENAI_API_KEY)%"
                model: "gpt-3.5-turbo"

Dynamic Routing in Code:

$provider = $request->get('provider', 'lm_studio'); // Default to LM Studio
$response = $chatCompletion->create([
    'model' => $provider,
    'messages' => [...],
]);

Integration Tips

  1. Environment-Specific Config Use Symfony’s %kernel.environment% to switch providers:

    # config/packages/ai_{env}.yaml
    framework:
        ai:
            providers:
                default:
                    platform: "%env(AI_PROVIDER)%"  # 'lm_studio' or 'openai'
    
  2. Model Fallbacks Combine providers for resilience:

    try {
        $response = $chatCompletion->create(['model' => 'lm_studio', ...]);
    } catch (Exception $e) {
        $response = $chatCompletion->create(['model' => 'openai', ...]);
    }
    
  3. LM Studio-Specific Features Extend the provider for custom LM Studio endpoints (e.g., non-OpenAI routes):

    // src/AI/LmStudioCustomProvider.php
    use Symfony\AI\Provider\AbstractProvider;
    
    class LmStudioCustomProvider extends AbstractProvider
    {
        public function getCustomEndpoint(): string
        {
            return $this->endpoint . 'custom/route'; // LM Studio-specific
        }
    }
    

    Register in config/services.yaml:

    services:
        Symfony\AI\Provider\LmStudioCustomProvider: ~
    
  4. Rate Limiting LM Studio may throttle requests. Use Symfony’s RateLimiter:

    use Symfony\Component\RateLimiter\RateLimiterFactory;
    
    $factory = new RateLimiterFactory();
    $limiter = $factory->create($this->rateLimiter->create('lm_studio', 10, '1 minute'));
    $limiter->consume(); // Enforce limit
    

Common Patterns

Pattern Example
Provider Switching Toggle between lm_studio and openai via config or runtime.
Model Routing Route specific models to LM Studio (e.g., mistral-7b) or OpenAI.
Fallback Logic Catch LM Studio failures and retry with OpenAI.
Local Testing Use LM Studio in dev env; OpenAI in prod.
Custom Endpoints Extend the provider for LM Studio’s non-OpenAI APIs.

Gotchas and Tips

Pitfalls

  1. LM Studio API Changes

    • LM Studio’s OpenAI-compatible endpoints may deprecate routes without notice. Monitor LM Studio releases.
    • Fix: Subscribe to LM Studio’s changelog or wrap calls in try-catch blocks.
  2. Resource Exhaustion

    • LM Studio consumes significant GPU/CPU. Running large models (e.g., llama-13b) may crash your machine.
    • Fix: Use Docker with resource limits or smaller models (e.g., mistral-7b).
  3. No Built-in Retries

    • The package does not retry failed requests to LM Studio. Network issues or OOM errors will propagate.
    • Fix: Implement retry logic with exponential backoff:
      use Symfony\Component\HttpClient\RetryStrategy;
      
      $client = HttpClient::create([
          'timeout' => 30,
          'retry' => RetryStrategy::fromOptions([
              'max_retries' => 3,
              'delay' => 100,
          ]),
      ]);
      
  4. Model Name Mismatches

    • LM Studio’s model names (e.g., "TheBloke/Llama-2-7B-Chat-GGUF") do not match OpenAI’s. Misconfiguration will return 404.
    • Fix: Verify model names in LM Studio’s UI or API:
      curl http://localhost:1234/v1/models
      
  5. Symfony AI Version Lock

    • The package requires Symfony AI v0.9+. Downgrading breaks provider routing.
    • Fix: Pin Symfony AI in composer.json:
      "symfony/ai": "^0.9"
      
  6. No Input Sanitization

    • Raw prompts are sent to LM Studio without escaping. Malicious input (e.g., SQLi) may leak.
    • Fix: Sanitize prompts before sending:
      $sanitizedPrompt = filter_var($userInput, FILTER_SANITIZE_STRING);
      

Debugging Tips

  1. Enable HTTP Debugging Configure Symfony’s HttpClient to log requests:

    # config/packages/http_client.yaml
    framework:
        http_client:
            plugins:
                - Symfony\Component\HttpClient\EventListener\ProfilerListener
    

    View logs in the Symfony Profiler under "HTTP Client".

  2. LM Studio Logs Check LM Studio’s logs (Docker: docker logs lmstudio) for errors like:

    • CUDA out of memory → Reduce model size or allocate more GPU RAM.
    • Invalid API request → Validate endpoint/payload against LM Studio’s OpenAI docs.
  3. Provider-Specific Errors Catch Symfony\AI\Exception\ProviderException to handle LM Studio failures:

    try {
        $response = $chatCompletion->create(['model' => 'lm_studio', ...]);
    } catch (ProviderException $e) {
        if (str_contains($e->getMessage(), 'LM Studio')) {
            // Log and fallback
        }
    }
    

Extension Points

  1. Custom Provider Logic Extend AbstractProvider
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky