Skip to content

Commit fbc3e44

Browse files
authored
Merge pull request #1 from llm-agents-php/feature/laravel-support
Adds Laravel support
2 parents b846992 + a1aa99a commit fbc3e44

File tree

4 files changed

+91
-28
lines changed

4 files changed

+91
-28
lines changed

Diff for: README.md

+41-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
This package is your go-to solution for integrating OpenAI's powerful API into your LLM Agents projects.
88

9-
## What's in the box?
9+
## What's in the box?
1010

1111
- Easy setup with Spiral framework
1212
- Smooth integration with OpenAI's API
@@ -22,13 +22,15 @@ composer require llm-agents/openai-client
2222

2323
2. That's it! You're ready to roll.
2424

25-
## Setting it up 🔧
25+
### Setting it up in Spiral
2626

27-
To get the OpenAI client up and running in your Spiral app, you need to register the bootloader. Here's how:
27+
To get the OpenAI client up and running in your Spiral app, you need to register the bootloader.
28+
29+
**Here's how:**
2830

2931
1. Open up your `app/src/Application/Kernel.php` file.
3032

31-
2. In your `Kernel` class, find or create the `defineBootloaders()` method and add the `OpenAIClientBootloader`:
33+
2. In your `Kernel` class add the `LLM\Agents\OpenAI\Client\Integration\Spiral\OpenAIClientBootloader` bootloader:
3234

3335
```php
3436
class Kernel extends \Spiral\Framework\Kernel
@@ -37,22 +39,51 @@ class Kernel extends \Spiral\Framework\Kernel
3739
{
3840
return [
3941
// ... other bootloaders ...
40-
\LLM\Agents\OpenAI\Client\Bootloader\OpenAIClientBootloader::class,
42+
\LLM\Agents\OpenAI\Client\Integration\Spiral\OpenAIClientBootloader::class,
4143
];
4244
}
4345
}
4446
```
4547

46-
## Configuration ⚙️
48+
The package uses your OpenAI API key and organization (if you have one) to authenticate.
4749

48-
The package uses your OpenAI API key and organization (if you have one) to authenticate. Set these up in your `.env`
49-
file:
50+
Set these up in your `.env` file:
5051

5152
```
5253
OPENAI_KEY=your_api_key_here
5354
```
5455

55-
## Contributing
56+
### Setting it up in Laravel
57+
58+
If you're using the Laravel framework, you'll need to install the `openai-php/laravel` package register the Service
59+
provider.
60+
61+
**Here's how:**
62+
63+
1. Install the `openai-php/laravel` package:
64+
65+
```bash
66+
composer require openai-php/laravel
67+
```
68+
69+
2. Next, execute the install command:
70+
71+
```bash
72+
php artisan openai:install
73+
```
74+
75+
3. Finally, add your OpenAI API key to your `.env` file:
76+
77+
```
78+
OPENAI_API_KEY=sk-...
79+
OPENAI_ORGANIZATION=org-...
80+
```
81+
82+
4. And register the `LLM\Agents\OpenAI\Client\Integration\Laravel\OpenAIClientServiceProvider`
83+
84+
And that's it! The service provider will take care of registering the `LLMInterface` for you.
85+
86+
## Contributing
5687

5788
We're always happy to get help making this package even better! Here's how you can chip in:
5889

@@ -62,7 +93,7 @@ We're always happy to get help making this package even better! Here's how you c
6293

6394
Please make sure your code follows PSR-12 coding standards and include tests for any new features.
6495

65-
## License
96+
## License
6697

6798
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
6899

Diff for: composer.json

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
"php": "^8.3",
77
"openai-php/client": "^0.10.1",
88
"llm-agents/agents": "^1.0",
9-
"spiral/boot": "^3.13",
109
"guzzlehttp/guzzle": "^7.0"
1110
},
1211
"require-dev": {
13-
"phpunit/phpunit": "^11.3"
12+
"phpunit/phpunit": "^11.3",
13+
"spiral/boot": "^3.13",
14+
"illuminate/support": "^11.0"
1415
},
1516
"autoload": {
1617
"psr-4": {
@@ -25,6 +26,13 @@
2526
"config": {
2627
"sort-packages": true
2728
},
29+
"extra": {
30+
"laravel": {
31+
"providers": [
32+
"LLM\\Agents\\OpenAI\\Client\\Integration\\Laravel\\OpenAIClientServiceProvider"
33+
]
34+
}
35+
},
2836
"minimum-stability": "dev",
2937
"prefer-stable": true
3038
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LLM\Agents\OpenAI\Client\Integration\Laravel;
6+
7+
use Illuminate\Contracts\Foundation\Application;
8+
use Illuminate\Support\ServiceProvider;
9+
use LLM\Agents\LLM\LLMInterface;
10+
use LLM\Agents\OpenAI\Client\LLM;
11+
use LLM\Agents\OpenAI\Client\Parsers\ChatResponseParser;
12+
use LLM\Agents\OpenAI\Client\StreamResponseParser;
13+
use OpenAI\Responses\Chat\CreateStreamedResponse;
14+
15+
final class OpenAIClientServiceProvider extends ServiceProvider
16+
{
17+
public function register(): void
18+
{
19+
$this->app->singleton(
20+
LLMInterface::class,
21+
LLM::class,
22+
);
23+
24+
$this->app->singleton(
25+
StreamResponseParser::class,
26+
static function (Application $app): StreamResponseParser {
27+
$parser = new StreamResponseParser();
28+
29+
// Register parsers here
30+
$parser->registerParser(
31+
CreateStreamedResponse::class,
32+
$app->make(ChatResponseParser::class),
33+
);
34+
35+
return $parser;
36+
},
37+
);
38+
}
39+
}

Diff for: src/Bootloader/OpenAIClientBootloader.php renamed to src/Integration/Spiral/OpenAIClientBootloader.php

+1-16
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22

33
declare(strict_types=1);
44

5-
namespace LLM\Agents\OpenAI\Client\Bootloader;
5+
namespace LLM\Agents\OpenAI\Client\Integration\Spiral;
66

7-
use GuzzleHttp\Client as HttpClient;
87
use LLM\Agents\LLM\LLMInterface;
98
use LLM\Agents\OpenAI\Client\LLM;
109
use LLM\Agents\OpenAI\Client\Parsers\ChatResponseParser;
1110
use LLM\Agents\OpenAI\Client\StreamResponseParser;
12-
use OpenAI\Contracts\ClientContract;
1311
use OpenAI\Responses\Chat\CreateStreamedResponse;
1412
use Spiral\Boot\Bootloader\Bootloader;
15-
use Spiral\Boot\EnvironmentInterface;
1613

1714
final class OpenAIClientBootloader extends Bootloader
1815
{
@@ -21,18 +18,6 @@ public function defineSingletons(): array
2118
return [
2219
LLMInterface::class => LLM::class,
2320

24-
ClientContract::class => static fn(
25-
EnvironmentInterface $env,
26-
): ClientContract => \OpenAI::factory()
27-
->withApiKey($env->get('OPENAI_KEY'))
28-
->withHttpHeader('OpenAI-Beta', 'assistants=v1')
29-
->withHttpClient(
30-
new HttpClient([
31-
'timeout' => (int) $env->get('OPENAI_HTTP_CLIENT_TIMEOUT', 2 * 60),
32-
]),
33-
)
34-
->make(),
35-
3621
StreamResponseParser::class => static function (
3722
ChatResponseParser $chatResponseParser,
3823
): StreamResponseParser {

0 commit comments

Comments
 (0)