|
| 1 | +# OpenRouter Client for Laravel |
| 2 | + |
| 3 | +A simple Laravel wrapper for the [OpenRouter API](https://openrouter.ai/) to easily perform chat completions using models like `openai/gpt-4o`. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 🧰 Features |
| 8 | + |
| 9 | +- Easy integration with Laravel via Service Provider |
| 10 | +- Customizable via `.env` or `config/openrouter.php` |
| 11 | +- Uses Guzzle for HTTP requests |
| 12 | +- Supports `max_tokens` for response control |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## 🚀 Installation |
| 17 | + |
| 18 | +Require the package via Composer: |
| 19 | + |
| 20 | +```bash |
| 21 | +composer require level7up/openrouter-client |
| 22 | +``` |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## 🛠 Configuration |
| 27 | + |
| 28 | +Publish the configuration file: |
| 29 | + |
| 30 | +```bash |
| 31 | +php artisan vendor:publish --tag=config |
| 32 | +``` |
| 33 | + |
| 34 | +Add your credentials to `.env`: |
| 35 | + |
| 36 | +```env |
| 37 | +OPENROUTER_API_KEY=your_openrouter_api_key |
| 38 | +OPENROUTER_BASE_URL=https://openrouter.ai/api/v1 |
| 39 | +OPENROUTER_REFERER=https://your-site.com # optional |
| 40 | +OPENROUTER_SITE_TITLE=Your Site Name # optional |
| 41 | +``` |
| 42 | + |
| 43 | +--- |
| 44 | + |
| 45 | +## 🧪 Usage |
| 46 | + |
| 47 | +Inject the client into your controller or service: |
| 48 | + |
| 49 | +```php |
| 50 | +use Level7up\OpenRouter\OpenRouterClient; |
| 51 | + |
| 52 | +class ChatController extends Controller |
| 53 | +{ |
| 54 | + public function ask(OpenRouterClient $client) |
| 55 | + { |
| 56 | + $response = $client->getCompletion('Tell me a joke.', 50); |
| 57 | + return response()->json(['reply' => $response]); |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## ⚙️ Configuration File |
| 65 | + |
| 66 | +After publishing, you can modify `config/openrouter.php`: |
| 67 | + |
| 68 | +```php |
| 69 | +return [ |
| 70 | + 'api_key' => env('OPENROUTER_API_KEY'), |
| 71 | + 'base_url' => env('OPENROUTER_BASE_URL', 'https://openrouter.ai/api/v1'), |
| 72 | + 'referer' => env('OPENROUTER_REFERER'), |
| 73 | + 'site_title' => env('OPENROUTER_SITE_TITLE'), |
| 74 | +]; |
| 75 | +``` |
| 76 | + |
| 77 | +--- |
| 78 | + |
| 79 | +## 📚 API Reference |
| 80 | + |
| 81 | +### `getCompletion(string $prompt, int $maxTokens = 100): ?string` |
| 82 | + |
| 83 | +Sends a prompt to the OpenRouter API and returns the response string. |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## ✅ Requirements |
| 88 | + |
| 89 | +- PHP 8.0+ |
| 90 | +- Laravel 8+ |
| 91 | +- Guzzle 7+ |
| 92 | + |
| 93 | +--- |
| 94 | + |
| 95 | +## 📄 License |
| 96 | + |
| 97 | +This package is open-sourced software licensed under the [MIT license](LICENSE). |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## 🧠 About |
| 102 | + |
| 103 | +Maintained by [Level7up](https://github.com/level7up). Contributions and issues are welcome! |
0 commit comments