Skip to content

Commit 7fba7a3

Browse files
committed
Init OpenAI Symfony Bundle
0 parents  commit 7fba7a3

18 files changed

+458
-0
lines changed

Diff for: .editorconfig

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
indent_style = space
11+
indent_size = 4
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
16+
17+
[*.yml]
18+
indent_size = 2

Diff for: .gitattributes

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/art export-ignore
2+
/docs export-ignore
3+
/tests export-ignore
4+
/scripts export-ignore
5+
/.github export-ignore
6+
/.php_cs export-ignore
7+
.editorconfig export-ignore
8+
.gitattributes export-ignore
9+
.gitignore export-ignore
10+
phpstan.neon.dist export-ignore
11+
phpunit.xml.dist export-ignore
12+
CHANGELOG.md export-ignore
13+
CONTRIBUTING.md export-ignore
14+
README.md export-ignore

Diff for: .github/FUNDING.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# These are supported funding model platforms
2+
3+
github: nunomaduro
4+
patreon: nunomaduro
5+
custom: https://www.paypal.com/paypalme/enunomaduro

Diff for: .github/workflows/formats.yml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Formats
2+
3+
on: ['push', 'pull_request']
4+
5+
jobs:
6+
ci:
7+
runs-on: ${{ matrix.os }}
8+
9+
strategy:
10+
fail-fast: true
11+
matrix:
12+
os: [ubuntu-latest]
13+
php: [8.2]
14+
dependency-version: [prefer-lowest, prefer-stable]
15+
16+
name: Formats P${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}
17+
18+
steps:
19+
20+
- name: Checkout
21+
uses: actions/checkout@v2
22+
23+
- name: Cache dependencies
24+
uses: actions/cache@v1
25+
with:
26+
path: ~/.composer/cache/files
27+
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
28+
29+
- name: Setup PHP
30+
uses: shivammathur/setup-php@v2
31+
with:
32+
php-version: ${{ matrix.php }}
33+
extensions: dom, mbstring, zip
34+
tools: prestissimo
35+
coverage: pcov
36+
37+
- name: Install Composer dependencies
38+
run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist
39+
40+
- name: Coding Style Checks
41+
run: composer test:lint
42+
43+
- name: Type Checks
44+
run: composer test:types

Diff for: .github/workflows/tests.yml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Tests
2+
3+
on: ['push', 'pull_request']
4+
5+
jobs:
6+
ci:
7+
runs-on: ${{ matrix.os }}
8+
strategy:
9+
fail-fast: true
10+
matrix:
11+
os: [ubuntu-latest]
12+
php: [8.1, 8.2]
13+
symfony: [5.4.*, 6.2.*]
14+
dependency-version: [prefer-lowest, prefer-stable]
15+
16+
name: Tests P${{ matrix.php }} - SF${{ matrix.symfony }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}
17+
steps:
18+
19+
- name: Checkout
20+
uses: actions/checkout@v2
21+
22+
- name: Cache dependencies
23+
uses: actions/cache@v1
24+
with:
25+
path: ~/.composer/cache/files
26+
key: dependencies-php-${{ matrix.php }}-SF${{ matrix.symfony }}-${{ matrix.dependency-version }}-composer-${{ hashFiles('composer.json') }}
27+
28+
- name: Setup PHP
29+
uses: shivammathur/setup-php@v2
30+
with:
31+
php-version: ${{ matrix.php }}
32+
extensions: dom, mbstring, zip
33+
coverage: none
34+
35+
- name: Require Symfony Version
36+
run: >
37+
composer require
38+
"symfony/config:${{ matrix.symfony }}"
39+
"symfony/dependency-injection:${{ matrix.symfony }}"
40+
"symfony/http-kernel:${{ matrix.symfony }}"
41+
--no-interaction --no-update
42+
- name: Install Composer dependencies
43+
run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist
44+
45+
- name: Integration Tests
46+
run: php ./vendor/bin/simple-phpunit

Diff for: .gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/.phpunit.cache
2+
/.php-cs-fixer.cache
3+
/.php-cs-fixer.php
4+
/composer.lock
5+
/phpunit.xml
6+
/vendor/
7+
*.swp
8+
*.swo

Diff for: CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
5+
and this project adheres to [Semantic Versioning](http://semver.org/).
6+
7+
## v0.3.4 (2023-03-04)
8+
### Added
9+
- First version

Diff for: CONTRIBUTING.md

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# CONTRIBUTING
2+
3+
Contributions are welcome, and are accepted via pull requests.
4+
Please review these guidelines before submitting any pull requests.
5+
6+
## Process
7+
8+
1. Fork the project
9+
1. Create a new branch
10+
1. Code, test, commit and push
11+
1. Open a pull request detailing your changes. Make sure to follow the [template](.github/PULL_REQUEST_TEMPLATE.md)
12+
13+
## Guidelines
14+
15+
* Please ensure the coding style running `composer lint`.
16+
* Send a coherent commit history, making sure each individual commit in your pull request is meaningful.
17+
* You may need to [rebase](https://git-scm.com/book/en/v2/Git-Branching-Rebasing) to avoid merge conflicts.
18+
* Please remember that we follow [SemVer](http://semver.org/).
19+
20+
## Setup
21+
22+
Clone your fork, then install the dev dependencies:
23+
```bash
24+
composer install
25+
```
26+
## Lint
27+
28+
Lint your code:
29+
```bash
30+
composer lint
31+
```
32+
## Tests
33+
34+
Run all tests:
35+
```bash
36+
composer test
37+
```
38+
39+
Check types:
40+
```bash
41+
composer test:types
42+
```
43+
44+
Unit tests:
45+
```bash
46+
composer test:unit
47+
```

Diff for: LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Nuno Maduro <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

Diff for: README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
**OpenAI PHP** for Symfony is a supercharged community PHP API client that allows you to interact with the [Open AI API](https://beta.openai.com/docs/api-reference/introduction).
2+
3+
> **Note:** This repository contains the integration code of the **OpenAI PHP** for Symfony. If you want to use the **OpenAI PHP** client in a framework-agnostic way, take a look at the [openai-php/client](https://github.com/openai-php/client) repository.
4+
5+
## Get Started
6+
7+
> **Requires [PHP 8.1+](https://php.net/releases/)**
8+
9+
First, install OpenAI via the [Composer](https://getcomposer.org/) package manager:
10+
11+
```bash
12+
composer require openai-php/symfony
13+
```
14+
15+
Next, register the bundle in your `config/bundles.php`:
16+
17+
```php
18+
return [
19+
// ...
20+
OpenAI\Symfony\OpenAIBundle::class => ['all' => true],
21+
]
22+
```
23+
24+
This will create a `.env` configuration file in your project, which you can modify to your needs
25+
using environment variables:
26+
27+
```env
28+
OPENAI_API_KEY=sk-...
29+
```
30+
31+
Finally, you may use the `openai` service to access the OpenAI API:
32+
33+
```php
34+
$result = $container->get('openai')->completions()->create([
35+
'model' => 'text-davinci-003',
36+
'prompt' => 'PHP is',
37+
]);
38+
39+
echo $result['choices'][0]['text']; // an open-source, widely-used, server-side scripting language.
40+
```
41+
42+
## Usage
43+
44+
For usage examples, take a look at the [openai-php/client](https://github.com/openai-php/client) repository.
45+
46+
---
47+
48+
OpenAI PHP for Symfony is an open-sourced software licensed under the **[MIT license](https://opensource.org/licenses/MIT)**.

Diff for: composer.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "openai-php/symfony",
3+
"description": "Symfony Bundle for OpenAI",
4+
"keywords": ["symfony", "php", "openai", "sdk", "codex", "GPT-3", "DALL-E", "api", "client", "natural", "language", "processing"],
5+
"type": "symfony-bundle",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Jérôme Tamarelle",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"php": "^8.1.0",
15+
"openai-php/client": "^0.3.4",
16+
"symfony/config": "^5.4|^6.0",
17+
"symfony/dependency-injection": "^5.4|^6.0",
18+
"symfony/http-kernel": "^5.4|^6.0"
19+
},
20+
"require-dev": {
21+
"laravel/pint": "^1.6",
22+
"symfony/phpunit-bridge": "^6.2"
23+
},
24+
"autoload": {
25+
"psr-4": {
26+
"OpenAI\\Symfony\\": "src/"
27+
}
28+
},
29+
"autoload-dev": {
30+
"psr-4": {
31+
"OpenAI\\Symfony\\Tests\\": "tests/"
32+
}
33+
},
34+
"minimum-stability": "dev",
35+
"prefer-stable": true,
36+
"config": {
37+
"sort-packages": true,
38+
"preferred-install": "dist"
39+
},
40+
"scripts": {
41+
"lint": "pint -v",
42+
"refactor": "rector --debug",
43+
"test:lint": "pint --test -v",
44+
"test:types": "phpstan analyse --ansi",
45+
"test:unit": "simple-phpunit --colors=always",
46+
"test": [
47+
"@test:lint",
48+
"@test:types",
49+
"@test:unit"
50+
]
51+
}
52+
}

Diff for: phpstan.neon.dist

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
level: max
3+
paths:
4+
- src
5+
6+
reportUnmatchedIgnoredErrors: true

Diff for: phpunit.xml.dist

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
backupGlobals="false"
4+
beStrictAboutTestsThatDoNotTestAnything="true"
5+
beStrictAboutOutputDuringTests="true"
6+
bootstrap="vendor/autoload.php"
7+
cacheResult="false"
8+
colors="true"
9+
failOnRisky="true"
10+
failOnWarning="true"
11+
processIsolation="false"
12+
stopOnError="false"
13+
stopOnFailure="false"
14+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
15+
>
16+
<testsuites>
17+
<testsuite name="Default Test Suite">
18+
<directory suffix=".php">./tests</directory>
19+
</testsuite>
20+
</testsuites>
21+
<coverage>
22+
<include>
23+
<directory suffix=".php">./src</directory>
24+
</include>
25+
</coverage>
26+
</phpunit>

Diff for: src/DependencyInjection/Configuration.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenAI\Symfony\DependencyInjection;
6+
7+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8+
use Symfony\Component\Config\Definition\ConfigurationInterface;
9+
10+
/**
11+
* @author Jérôme Tamarelle <[email protected]>
12+
*/
13+
class Configuration implements ConfigurationInterface
14+
{
15+
public function getConfigTreeBuilder(): TreeBuilder
16+
{
17+
$treeBuilder = new TreeBuilder('openai');
18+
$rootNode = $treeBuilder->getRootNode();
19+
20+
$rootNode
21+
->children()
22+
->scalarNode('api_key')->defaultValue('%env(OPENAI_API_KEY)%')->end()
23+
->scalarNode('organization')->defaultValue('%env(default::OPENAI_ORGANIZATION)%')->end()
24+
->end();
25+
26+
return $treeBuilder;
27+
}
28+
}

0 commit comments

Comments
 (0)