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.
Install the Package
Add to your composer.json:
composer require symfony/ai-lm-studio-platform
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
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();
}
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
/chat/completions, /embeddings).symfony/ai-lm-studio-platform Tests – Example usage in tests/.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.
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' => [...],
]);
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'
Model Fallbacks Combine providers for resilience:
try {
$response = $chatCompletion->create(['model' => 'lm_studio', ...]);
} catch (Exception $e) {
$response = $chatCompletion->create(['model' => 'openai', ...]);
}
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: ~
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
| 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. |
LM Studio API Changes
Resource Exhaustion
llama-13b) may crash your machine.mistral-7b).No Built-in Retries
use Symfony\Component\HttpClient\RetryStrategy;
$client = HttpClient::create([
'timeout' => 30,
'retry' => RetryStrategy::fromOptions([
'max_retries' => 3,
'delay' => 100,
]),
]);
Model Name Mismatches
"TheBloke/Llama-2-7B-Chat-GGUF") do not match OpenAI’s. Misconfiguration will return 404.curl http://localhost:1234/v1/models
Symfony AI Version Lock
composer.json:
"symfony/ai": "^0.9"
No Input Sanitization
$sanitizedPrompt = filter_var($userInput, FILTER_SANITIZE_STRING);
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".
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.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
}
}
AbstractProviderHow can I help you explore Laravel packages today?