omiya0555/laravel-prism-upstage-solar

Laravel Prism provider for Upstage Solar models

v1.0.0 2025-09-15 17:34 UTC

This package is auto-updated.

Last update: 2025-09-15 17:49:15 UTC


README

A Laravel package that provides Upstage Solar text generation models integration with Laravel Prism.

Latest Version on Packagist Total Downloads MIT Licensed

Features

  • πŸš€ Text Generation: Use Upstage Solar models for text generation
  • πŸ“‘ Streaming: Real-time streaming responses
  • βš™οΈ Laravel Integration: Seamless integration with Laravel applications
  • πŸ”§ Auto-Discovery: Automatic service provider discovery
  • πŸ“ Configurable: Flexible configuration options

Requirements

  • PHP 8.2 or higher
  • Laravel 10.0, 11.0, or 12.0
  • Prism PHP 0.89.0 or higher

Installation

You can install the package via composer:

composer require omiya0555/laravel-prism-upstage-solar

Publish Configuration

Publish the configuration file:

php artisan vendor:publish --tag=config --provider="Omiya0555\LaravelPrismUpstage\PrismUpstageSolarServiceProvider"

This will create a config/prism_upstage.php file in your Laravel application.

Configuration

Environment Variables

Add the following environment variables to your .env file:

# Required: Your Upstage API Key
UPSTAGE_API_KEY=your_upstage_api_key_here

# Optional: Custom endpoint (default shown)
UPSTAGE_BASE_URL=https://api.upstage.ai/v1/solar

# Optional: Default model
UPSTAGE_TEXT_MODEL=solar-mini

# Optional: HTTP configuration
UPSTAGE_HTTP_TIMEOUT=30
UPSTAGE_HTTP_RETRY_ATTEMPTS=3
UPSTAGE_HTTP_RETRY_DELAY=1000

Getting Your API Key

  1. Sign up at Upstage Console
  2. Create a new API key
  3. Add the API key to your .env file

Usage

Basic Text Generation

use Prism\Prism\Prism;

$response = Prism::text()
    ->using('upstage', 'solar-mini')
    ->withPrompt('Explain quantum computing in simple terms.')
    ->asText();

echo $response->text;

With System Prompt

$response = Prism::text()
    ->using('upstage', 'solar-mini')
    ->withSystemPrompt('You are a helpful assistant that responds in Japanese.')
    ->withPrompt('量子コンピγƒ₯γƒΌγ‚Ώγ«γ€γ„γ¦η°‘ε˜γ«θͺ¬ζ˜Žγ—てください。')
    ->asText();

echo $response->text;

Advanced Configuration

$response = Prism::text()
    ->using('upstage', 'solar-mini')
    ->withPrompt('Write a creative story about AI.')
    ->withMaxTokens(500)
    ->withTemperature(0.7)
    ->withTopP(0.9)
    ->asText();

echo $response->text;
echo "Tokens used: " . $response->usage->totalTokens();

Streaming Responses

$stream = Prism::text()
    ->using('upstage', 'solar-mini')
    ->withPrompt('Tell me a story about space exploration.')
    ->asStream();

foreach ($stream as $chunk) {
    echo $chunk->text;
    flush(); // Send output to browser immediately
}

Available Models

Text Generation Models

  • solar-mini: Fast and efficient model for general tasks
  • solar-1-mini: Improved version with better performance
  • solar-pro: Advanced model for complex tasks

Laravel Integration Examples

Controller Example

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Prism\Prism\Prism;

class ChatController extends Controller
{
    public function chat(Request $request)
    {
        $request->validate([
            'message' => 'required|string|max:1000',
        ]);

        try {
            $response = Prism::text()
                ->using('upstage', config('prism_upstage.default_model'))
                ->withPrompt($request->input('message'))
                ->asText();

            return response()->json([
                'response' => $response->text,
                'usage' => [
                    'prompt_tokens' => $response->usage->promptTokens,
                    'completion_tokens' => $response->usage->completionTokens,
                ],
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'error' => 'Failed to generate response',
                'message' => $e->getMessage(),
            ], 500);
        }
    }
}

Artisan Command Example

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Prism\Prism\Prism;

class GenerateText extends Command
{
    protected $signature = 'upstage:generate {prompt}';
    protected $description = 'Generate text using Upstage Solar model';

    public function handle()
    {
        $prompt = $this->argument('prompt');

        try {
            $response = Prism::text()
                ->using('upstage', config('prism_upstage.default_model'))
                ->withPrompt($prompt)
                ->asText();

            $this->info('Generated text:');
            $this->line($response->text);
            $this->info('Tokens used: ' . $response->usage->totalTokens());

        } catch (\Exception $e) {
            $this->error('Failed to generate text: ' . $e->getMessage());
            return 1;
        }

        return 0;
    }
}

Error Handling

The package includes comprehensive error handling:

use Prism\Prism\Exceptions\PrismException;
use Prism\Prism\Exceptions\PrismRateLimitedException;
use Prism\Prism\Exceptions\PrismRequestTooLargeException;

try {
    $response = Prism::text()
        ->using('upstage', 'solar-mini')
        ->withPrompt('Your prompt here')
        ->asText();

} catch (PrismRateLimitedException $e) {
    // Handle rate limiting
    logger()->warning('Rate limit exceeded', ['exception' => $e]);

} catch (PrismRequestTooLargeException $e) {
    // Handle request too large
    logger()->error('Request too large', ['exception' => $e]);

} catch (PrismException $e) {
    // Handle general Prism exceptions
    logger()->error('Prism error', ['exception' => $e]);

} catch (\Exception $e) {
    // Handle any other exceptions
    logger()->error('Unexpected error', ['exception' => $e]);
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.