Skip to content

Commit 330eddc

Browse files
committed
🎉 🚀
0 parents  commit 330eddc

File tree

6 files changed

+225
-0
lines changed

6 files changed

+225
-0
lines changed

.vscode/settings.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"workbench.colorCustomizations": {
3+
"activityBar.activeBackground": "#3a0000",
4+
"activityBar.background": "#3a0000",
5+
"activityBar.foreground": "#e7e7e7",
6+
"activityBar.inactiveForeground": "#e7e7e799",
7+
"activityBarBadge.background": "#006000",
8+
"activityBarBadge.foreground": "#e7e7e7",
9+
"commandCenter.border": "#e7e7e799",
10+
"sash.hoverBorder": "#3a0000",
11+
"statusBar.background": "#070000",
12+
"statusBar.foreground": "#e7e7e7",
13+
"statusBarItem.hoverBackground": "#3a0000",
14+
"statusBarItem.remoteBackground": "#070000",
15+
"statusBarItem.remoteForeground": "#e7e7e7",
16+
"titleBar.activeBackground": "#070000",
17+
"titleBar.activeForeground": "#e7e7e7",
18+
"titleBar.inactiveBackground": "#07000099",
19+
"titleBar.inactiveForeground": "#e7e7e799"
20+
}
21+
}

README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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!

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "level7up/openrouter-client",
3+
"description": "OpenRouter API client for Laravel",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"OpenRouter\\": "src/"
9+
}
10+
},
11+
"extra": {
12+
"laravel": {
13+
"providers": [
14+
"Level7up\\OpenRouter\\OpenRouterServiceProvider"
15+
]
16+
}
17+
},
18+
"require": {
19+
"php": "^8.0",
20+
"guzzlehttp/guzzle": "^7.0|^8.0"
21+
}
22+
}

config/openrouter.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
return [
3+
'api_key' => env('OPENROUTER_API_KEY'),
4+
'base_url' => env('OPENROUTER_BASE_URL', 'https://openrouter.ai/api/v1'),
5+
];

src/OpenRouterClient.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Level7up\OpenRouter;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Exception\GuzzleException;
7+
8+
class OpenRouterClient
9+
{
10+
protected Client $client;
11+
protected string $apiKey;
12+
protected string $baseUrl;
13+
14+
public function __construct()
15+
{
16+
$this->client = new Client();
17+
$this->apiKey = env('OPENROUTER_API_KEY');
18+
$this->baseUrl = 'https://openrouter.ai/api/v1';
19+
}
20+
21+
public function getCompletion(string $prompt): ?string
22+
{
23+
try {
24+
$headers = [
25+
'Authorization' => 'Bearer ' . $this->apiKey,
26+
'Content-Type' => 'application/json',
27+
];
28+
29+
$response = $this->client->post("{$this->baseUrl}/chat/completions", [
30+
'headers' => $headers,
31+
'json' => [
32+
'model' => 'mistralai/mistral-7b-instruct:free',
33+
'messages' => [
34+
[
35+
'role' => 'user',
36+
'content' => $prompt,
37+
],
38+
],
39+
],
40+
]);
41+
42+
$data = json_decode($response->getBody(), true);
43+
return $data['choices'][0]['message']['content'] ?? null;
44+
45+
} catch (GuzzleException $e) {
46+
throw new \RuntimeException('OpenRouter request failed: ' . $e->getMessage(), 0, $e);
47+
}
48+
}
49+
}

src/OpenRouterServiceProvider.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Level7up\OpenRouter;
5+
6+
use Illuminate\Support\ServiceProvider;
7+
8+
class OpenRouterServiceProvider extends ServiceProvider
9+
{
10+
public function register()
11+
{
12+
$this->mergeConfigFrom(__DIR__ . '/../config/openrouter.php', 'openrouter');
13+
14+
$this->app->singleton(OpenRouterClient::class, function ($app) {
15+
return new OpenRouterClient(config('openrouter'));
16+
});
17+
}
18+
19+
public function boot()
20+
{
21+
$this->publishes([
22+
__DIR__ . '/../config/openrouter.php' => config_path('openrouter.php'),
23+
], 'config');
24+
}
25+
}

0 commit comments

Comments
 (0)