omiya0555/laravel-prism-upstage-solar 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

omiya0555/laravel-prism-upstage-solar

最新稳定版本:v1.0.0

Composer 安装命令:

composer require omiya0555/laravel-prism-upstage-solar

包简介

Laravel Prism provider for Upstage Solar models

README 文档

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.

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-15