Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit916e195

Browse files
authored
Merge pull requestopenai-php#1 from GromNaN/symfony-http-client
Use symfony/http-client
2 parents702d05d +ecf6173 commit916e195

File tree

5 files changed

+49
-20
lines changed

5 files changed

+49
-20
lines changed

‎composer.json‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@
1212
],
1313
"require": {
1414
"php":"^8.1.0",
15-
"openai-php/client":"^0.3.5",
15+
"nyholm/psr7":"^1.5",
16+
"openai-php/client":"^0.4.0",
17+
"psr/http-client":"^1.0.1",
18+
"psr/http-factory":"^1.0.1",
1619
"symfony/config":"^5.4.21|^6.2.7",
1720
"symfony/dependency-injection":"^5.4.21|^6.2.7",
21+
"symfony/http-client":"^5.4.21|^6.2.7",
1822
"symfony/http-kernel":"^5.4.21|^6.2.7"
1923
},
2024
"require-dev": {
@@ -37,7 +41,10 @@
3741
"prefer-stable":true,
3842
"config": {
3943
"sort-packages":true,
40-
"preferred-install":"dist"
44+
"preferred-install":"dist",
45+
"allow-plugins": {
46+
"php-http/discovery":false
47+
}
4148
},
4249
"scripts": {
4350
"lint":"pint -v",

‎src/DependencyInjection/Configuration.php‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,8 @@ public function getConfigTreeBuilder(): TreeBuilder
2828

2929
assert($childreninstanceof NodeBuilder);
3030

31-
$children =$children->scalarNode('api_key')->defaultValue('%env(OPENAI_API_KEY)%')->end();
32-
33-
assert($childreninstanceof NodeBuilder);
34-
35-
$children =$children->scalarNode('organization')->defaultValue('%env(default::OPENAI_ORGANIZATION)%')->end();
36-
37-
assert($childreninstanceof NodeBuilder);
38-
39-
$children->end();
31+
$children->scalarNode('api_key')->defaultValue('%env(OPENAI_API_KEY)%')->end();
32+
$children->scalarNode('organization')->defaultValue('%env(default::OPENAI_ORGANIZATION)%')->end();
4033

4134
return$treeBuilder;
4235
}

‎src/DependencyInjection/OpenAIExtension.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespaceOpenAI\Symfony\DependencyInjection;
66

7-
useOpenAI\Client;
7+
useOpenAI\Factory;
88
useSymfony\Component\Config\Definition\ConfigurationInterface;
99
useSymfony\Component\Config\FileLocator;
1010
useSymfony\Component\DependencyInjection\ContainerBuilder;
@@ -30,8 +30,10 @@ public function load(array $configs, ContainerBuilder $container): void
3030

3131
$config =$this->processConfiguration($configuration,$configs);
3232

33-
$definition =$container->getDefinition(Client::class);
34-
$definition->setArgument(0,$config['api_key']);
35-
$definition->setArgument(1,$config['organization']);
33+
$definition =$container->getDefinition(Factory::class);
34+
$definition->addMethodCall('withApiKey', [$config['api_key']]);
35+
if ($config['organization']) {
36+
$definition->addMethodCall('withOrganization', [$config['organization']]);
37+
}
3638
}
3739
}

‎src/Resources/config/services.php‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,20 @@
66

77
useOpenAI;
88
useOpenAI\Client;
9+
useOpenAI\Factory;
10+
useSymfony\Component\HttpClient\Psr18Client;
911

1012
returnstaticfunction (ContainerConfigurator$container) {
1113
$container->services()
14+
->set('openai.http_client', Psr18Client::class)
15+
->arg(0,service('http_client'))
16+
17+
->set(Factory::class)
18+
->factory([OpenAI::class,'factory'])
19+
->call('withHttpClient', [service('openai.http_client')])
20+
1221
->set(Client::class)
13-
->factory([OpenAI::class,'client'])
14-
->args([
15-
abstract_arg('API Key'),
16-
abstract_arg('Organisation'),
17-
])
22+
->factory([service(Factory::class),'make'])
23+
1824
->alias('openai', Client::class);
1925
};

‎tests/DependencyInjection/OpenAIExtensionTest.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,30 @@
88
useOpenAI\Symfony\DependencyInjection\OpenAIExtension;
99
usePHPUnit\Framework\TestCase;
1010
useSymfony\Component\DependencyInjection\ContainerBuilder;
11+
useSymfony\Component\HttpClient\MockHttpClient;
12+
useSymfony\Component\HttpClient\Response\MockResponse;
1113

1214
finalclass OpenAIExtensionTestextends TestCase
1315
{
1416
publicfunctiontestService():void
1517
{
18+
// Using a mock to test the service configuration
19+
$httpClient =newMockHttpClient(function (string$method,string$url,array$options = []) {
20+
self::assertSame('DELETE',$method);
21+
self::assertSame('https://api.openai.com/v1/files/file.txt',$url);
22+
self::assertContains('Authorization: Bearer pk-123456789',$options['headers']);
23+
24+
returnnewMockResponse('{"id":"file.txt","object":"file","deleted":true}', [
25+
'http_code' =>200,
26+
'response_headers' => [
27+
'content-type' =>'application/json',
28+
],
29+
]);
30+
});
31+
1632
$container =newContainerBuilder();
33+
$container->set('http_client',$httpClient);
34+
1735
$extension =newOpenAIExtension();
1836
$extension->load([
1937
'openai' => [
@@ -23,5 +41,8 @@ public function testService(): void
2341

2442
$openai =$container->get('openai');
2543
self::assertInstanceOf(Client::class,$openai);
44+
45+
$response =$openai->files()->delete('file.txt');
46+
self::assertSame('file.txt',$response->id);
2647
}
2748
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp