Skip to content

Commit bb0421e

Browse files
committed
Update config docs
1 parent d429b19 commit bb0421e

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ OPENAI_API_KEY=sk-...
4242
OPENAI_ORGANIZATION=...
4343
```
4444

45+
For more configuration options, take a look at the [Configuration Reference](#configuration-reference).
46+
4547
Finally, you may use the `openai` service to access the OpenAI API:
4648

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

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

62+
## Configuration Reference
63+
64+
The bundle provides the following configuration options, which you can set in your `config/packages/openai.yaml` file:
65+
66+
```yaml
67+
openai:
68+
api_key: '%env(OPENAI_API_KEY)%' # Your OpenAI API key (required)
69+
organization: '%env(OPENAI_ORGANIZATION)%' # Your OpenAI organization ID (optional)
70+
project: 'proj_...' # The project ID (optional)
71+
base_uri: 'api.openai.com/v1' # The base URI for the OpenAI API (optional)
72+
```
73+
6074
---
6175
6276
OpenAI PHP for Symfony is an open-sourced software licensed under the **[MIT license](https://opensource.org/licenses/MIT)**.

src/OpenAIBundle.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,54 @@ public function configure(DefinitionConfigurator $definition): void
2525
$root = $definition->rootNode();
2626
assert($root instanceof ArrayNodeDefinition);
2727
$children = $root->children();
28-
$children->scalarNode('api_key')->defaultValue('%env(OPENAI_API_KEY)%');
29-
$children->scalarNode('organization')->defaultValue('%env(default::OPENAI_ORGANIZATION)%');
28+
$children
29+
->scalarNode('api_key')
30+
->defaultValue('%env(OPENAI_API_KEY)%')
31+
->info('OpenAI API Key used to authenticate with the OpenAI API')
32+
->isRequired();
33+
$children
34+
->scalarNode('organization')
35+
->info('OpenAI API Organization used to authenticate with the OpenAI API')
36+
->defaultValue('%env(default::OPENAI_ORGANIZATION)%')
37+
->info('');
38+
$children
39+
->scalarNode('project')
40+
->defaultNull()
41+
->info('OpenAI API project');
42+
$children
43+
->scalarNode('base_uri')
44+
->defaultNull()
45+
->info('OpenAI API base URL used to make requests. Defaults to: api.openai.com/v1');
3046
}
3147

3248
/**
33-
* @param array{api_key: string, organization: string} $config
49+
* @param array{api_key: string, organization: string, project: ?string, base_uri: ?string} $config
3450
*/
3551
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
3652
{
3753
$container->services()
3854
->set('openai.http_client', Psr18Client::class)
39-
->arg(0, service('http_client'))
55+
->arg(0, service('http_client'));
4056

57+
$factory = $container->services()
4158
->set(Factory::class)
4259
->factory([\OpenAI::class, 'factory'])
4360
->call('withHttpClient', [service('openai.http_client')])
4461
->call('withHttpHeader', ['OpenAI-Beta', 'assistants=v2'])
4562
->call('withApiKey', [$config['api_key']])
46-
->call('withOrganization', [$config['organization']])
63+
->call('withOrganization', [$config['organization']]);
64+
if ($config['project']) {
65+
$factory->call('withProject', [$config['project']]);
66+
}
67+
if ($config['base_uri']) {
68+
$factory->call('withBaseUri', [$config['base_uri']]);
69+
}
4770

71+
$container->services()
4872
->set(Client::class)
49-
->factory([service(Factory::class), 'make'])
73+
->factory([service(Factory::class), 'make']);
5074

75+
$container->services()
5176
->alias(ClientContract::class, Client::class)
5277
->alias('openai', Client::class);
5378
}

0 commit comments

Comments
 (0)