omiya0555/laravel-prism-upstage-solar
A Laravel package that provides Upstage Solar text generation models integration with Laravel Prism.
You can install the package via composer:
composer require omiya0555/laravel-prism-upstage-solar
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.
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
.env fileuse Prism\Prism\Prism;
$response = Prism::text()
->using('upstage', 'solar-mini')
->withPrompt('Explain quantum computing in simple terms.')
->asText();
echo $response->text;
$response = Prism::text()
->using('upstage', 'solar-mini')
->withSystemPrompt('You are a helpful assistant that responds in Japanese.')
->withPrompt('ιεγ³γ³γγ₯γΌγΏγ«γ€γγ¦η°‘εγ«θͺ¬ζγγ¦γγ γγγ')
->asText();
echo $response->text;
$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();
$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
}
solar-mini: Fast and efficient model for general taskssolar-1-mini: Improved version with better performancesolar-pro: Advanced model for complex tasks<?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);
}
}
}
<?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;
}
}
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]);
}
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.
How can I help you explore Laravel packages today?