Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 14 additions & 26 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,33 @@ jobs:
fail-fast: true
matrix:
php: [8.2, 8.3, 8.4]
symfony: [5.4.*, 6.4.*, 7.0.*]
dependency-version: [prefer-lowest, prefer-stable]
exclude:
- php: 8.4
symfony: 5.4.*
- php: 8.4
symfony: 6.4.*
- php: 8.4
symfony: 7.0.*

name: Tests P${{ matrix.php }} - SF${{ matrix.symfony }} - ubuntu-latest - ${{ matrix.dependency-version }}
symfony: [^6.4, false]
dependency: [stable]
include:
- { php: 8.4, symfony: ^7.4, dependency: highest }
- { php: 8.4, symfony: ^8.0, dependency: highest }

env:
SYMFONY_REQUIRE: ${{ matrix.symfony }}

name: Tests P${{ matrix.php }} - SF${{ matrix.symfony }} - ubuntu-latest - ${{ matrix.dependency }}
steps:

- name: Checkout
uses: actions/checkout@v5

- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.composer/cache/files
key: dependencies-php-${{ matrix.php }}-SF${{ matrix.symfony }}-${{ matrix.dependency-version }}-composer-${{ hashFiles('composer.json') }}

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, mbstring, zip
coverage: none

- name: Require Symfony Version
run: >
composer require
"symfony/config:${{ matrix.symfony }}"
"symfony/dependency-injection:${{ matrix.symfony }}"
"symfony/http-kernel:${{ matrix.symfony }}"
--no-interaction --no-update
tools: flex

- name: Install Composer dependencies
run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist
uses: ramsey/composer-install@v2
with:
dependency-versions: ${{ matrix.dependency }}

- name: Integration Tests
run: php ./vendor/bin/simple-phpunit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
/composer.lock
/phpunit.xml
/vendor/
/var/
*.swp
*.swo
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## v0.17.0
### Changed
- Refactored into a single `OpenAI\Symfony\OpenAIBundle` class
- Add `project` and `base_uri` configuration options
- Drop support for unsupported Symfony versions. Now requires Symfony 6.4 or 7.3+
- Add support for Symfony 8.0

## v0.12.0 (2025-05-06)
### Changed
- Changed underlying `openai/client` package version to 0.12.0
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ OPENAI_API_KEY=sk-...
OPENAI_ORGANIZATION=...
```

For more configuration options, take a look at the [Configuration Reference](#configuration-reference).

Finally, you may use the `openai` service to access the OpenAI API:

```php
Expand All @@ -57,6 +59,18 @@ echo $result['choices'][0]['text']; // an open-source, widely-used, server-side

For usage examples, take a look at the [openai-php/client](https://github.com/openai-php/client) repository.

## Configuration Reference

The bundle provides the following configuration options, which you can set in your `config/packages/openai.yaml` file:

```yaml
openai:
api_key: '%env(OPENAI_API_KEY)%' # Your OpenAI API key (required)
organization: '%env(OPENAI_ORGANIZATION)%' # Your OpenAI organization ID (optional)
project: 'proj_...' # The project ID (optional)
base_uri: 'api.openai.com/v1' # The base URI for the OpenAI API (optional)
```

---

OpenAI PHP for Symfony is an open-sourced software licensed under the **[MIT license](https://opensource.org/licenses/MIT)**.
19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
"openai-php/client": "^0.17.0",
"psr/http-client": "^1.0.3",
"psr/http-factory": "^1.1.0",
"symfony/config": "^5.4|^6.3|^7.1.1",
"symfony/dependency-injection": "^5.4|^6.3|^7.1.5",
"symfony/http-client": "^5.4|^6.3|^7.1.5",
"symfony/http-kernel": "^5.4|^6.3|^7.1.5"
"symfony/config": "^6.4|^7.3|^8.0",
"symfony/dependency-injection": "^6.4|^7.3|^8.0",
"symfony/http-client": "^6.4|^7.3|^8.0",
"symfony/http-kernel": "^6.4|^7.3|^8.0"
},
"require-dev": {
"laravel/pint": "^1.18.1",
"phpstan/phpstan": "^1.12.6",
"rector/rector": "^0.14.8",
"symfony/phpunit-bridge": "^5.4|^6.3|^7.1.4"
"laravel/pint": "^1.24.0",
"phpstan/phpstan": "^2.1.22",
"rector/rector": "^2.1.5",
"symfony/phpunit-bridge": "^6.4.25|^7.3|^8.0",
"symfony/framework-bundle": "^6.4|^7.3|^8.0"
},
"autoload": {
"psr-4": {
Expand All @@ -53,7 +54,7 @@
"scripts": {
"lint": "pint -v",
"refactor": "rector --debug",
"test:lint": "pint --test -v",
"test:lint": "pint --test -v ./src ./tests",
"test:types": "phpstan analyse --ansi",
"test:unit": "simple-phpunit --colors=always",
"test": [
Expand Down
11 changes: 7 additions & 4 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__.'/src',
]);

$rectorConfig->skip([
__DIR__.'/src/Resources/config/',
__DIR__.'/tests',
__DIR__.'/rector.php',
]);

$rectorConfig->rules([
Expand All @@ -28,4 +27,8 @@
SetList::TYPE_DECLARATION,
SetList::PRIVATIZATION,
]);

$rectorConfig->skip([
FirstClassCallableRector::class,
]);
};
36 changes: 0 additions & 36 deletions src/DependencyInjection/Configuration.php

This file was deleted.

39 changes: 0 additions & 39 deletions src/DependencyInjection/OpenAIExtension.php

This file was deleted.

74 changes: 72 additions & 2 deletions src/OpenAIBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,76 @@

namespace OpenAI\Symfony;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use OpenAI\Client;
use OpenAI\Contracts\ClientContract;
use OpenAI\Factory;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpClient\Psr18Client;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

final class OpenAIBundle extends Bundle {}
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

final class OpenAIBundle extends AbstractBundle
{
protected string $extensionAlias = 'openai';

public function configure(DefinitionConfigurator $definition): void
{
$root = $definition->rootNode();
assert($root instanceof ArrayNodeDefinition);
$children = $root->children();
$children
->scalarNode('api_key')
->defaultValue('%env(OPENAI_API_KEY)%')
->info('OpenAI API Key used to authenticate with the OpenAI API')
->isRequired();
$children
->scalarNode('organization')
->info('OpenAI API Organization used to authenticate with the OpenAI API')
->defaultValue('%env(default::OPENAI_ORGANIZATION)%')
->info('');
$children
->scalarNode('project')
->defaultNull()
->info('OpenAI API project');
$children
->scalarNode('base_uri')
->defaultNull()
->info('OpenAI API base URL used to make requests. Defaults to: api.openai.com/v1');
}

/**
* @param array{api_key: string, organization: string, project: ?string, base_uri: ?string} $config
*/
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
$container->services()
->set('openai.http_client', Psr18Client::class)
->arg(0, service('http_client'));

$factory = $container->services()
->set(Factory::class)
->factory([\OpenAI::class, 'factory'])
->call('withHttpClient', [service('openai.http_client')])
->call('withHttpHeader', ['OpenAI-Beta', 'assistants=v2'])
->call('withApiKey', [$config['api_key']])
->call('withOrganization', [$config['organization']]);
if ($config['project']) {
$factory->call('withProject', [$config['project']]);
}
if ($config['base_uri']) {
$factory->call('withBaseUri', [$config['base_uri']]);
}

$container->services()
->set(Client::class)
->factory([service(Factory::class), 'make']);

$container->services()
->alias(ClientContract::class, Client::class)
->alias('openai', Client::class);
}
}
28 changes: 0 additions & 28 deletions src/Resources/config/services.php

This file was deleted.

Loading
Loading