Skip to content

Commit 890af42

Browse files
committed
refactor(app): improve GeneratorManager
- Refactor the `GeneratorManager` class to enhance readability and maintainability. - Update the method for creating drivers. - Add support for multiple drivers. - Throw an exception if the driver is not supported.
1 parent 25d0131 commit 890af42

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

app/GeneratorManager.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@ protected function createDriver($driver)
4646
}
4747

4848
$config = $this->config->get("ai-commit.generators.$driver");
49-
$studlyName = Str::studly($driver);
49+
$drivers = array_filter([$driver, $config['driver'] ?? null]);
5050

51-
if (method_exists($this, $method = "create{$studlyName}Driver")) {
52-
return $this->{$method}($config);
53-
}
51+
foreach ($drivers as $d) {
52+
$studlyName = Str::studly($d);
53+
54+
if (method_exists($this, $method = "create{$studlyName}Driver")) {
55+
return $this->{$method}($config);
56+
}
5457

55-
if (class_exists($class = "App\\Generators\\{$studlyName}Generator")) {
56-
return new $class($config);
58+
if (class_exists($class = "App\\Generators\\{$studlyName}Generator")) {
59+
return new $class($config);
60+
}
5761
}
5862

5963
throw new InvalidArgumentException("Driver [$driver] not supported.");

app/Support/Ernie.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ final class Ernie extends FoundationSDK
5151
* data: {'id':'as-rx9g6c5sqp','object':'chat.completion','created':1692253330,'sentence_id':2,'is_end':false,'is_truncated':false,'result':'PHP的语法借鉴吸收C语言、Java和语言的特点,易于一般程序员学习。','need_clear_history':false,'usage':{'prompt_tokens':4,'completion_tokens':35,'total_tokens':87}}
5252
*
5353
* data: {'id':'as-rx9g6c5sqp','object':'chat.completion','created':1692253331,'sentence_id':3,'is_end':false,'is_truncated':false,'result':'PHP的主要目标是允许网络开发人P也被用于其他很多领域。','need_clear_history':false,'usage':{'prompt_tokens':4,'completion_tokens':35,'total_tokens':122}}
54+
*
55+
* {'error_code':336003,'error_msg':'the max length of current question is 2000','id':'as-fxhfe8n53r'}
5456
* ```
5557
*
5658
* ```error
@@ -209,6 +211,10 @@ static function (PendingRequest $pendingRequest) use (&$rowData, $writer): Pendi
209211
);
210212
}
211213

212-
return $response->throw();
214+
return tap($response->throw(), function (Response $response): void {
215+
if (isset($response['error_code'])) {
216+
throw new RequestException($response);
217+
}
218+
});
213219
}
214220
}

config/ai-commit.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,10 @@
8989
'frequency_penalty' => 0,
9090
'best_of' => 1,
9191
// 'logit_bias' => null,
92-
// 'user' => Illuminate\Support\Str::uuid()->toString(),
9392
],
9493
],
9594
'openai_chat' => [
96-
'driver' => 'openai',
95+
'driver' => 'openai_chat',
9796
'http_options' => [
9897
// GuzzleHttp\RequestOptions::PROXY => 'https://proxy.com/v1',
9998
],
@@ -110,11 +109,10 @@
110109
'presence_penalty' => 0,
111110
'frequency_penalty' => 0,
112111
// 'logit_bias' => null,
113-
// 'user' => Illuminate\Support\Str::uuid()->toString(),
114112
],
115113
],
116114
'ernie_bot' => [
117-
'driver' => 'ernie',
115+
'driver' => 'ernie_bot',
118116
'http_options' => [],
119117
'api_key' => env('ERNIE_API_KEY', '...'),
120118
'secret_key' => env('ERNIE_SECRET_KEY', '...'),
@@ -124,11 +122,10 @@
124122
'top_p' => 0.8,
125123
'penalty_score' => 1.0,
126124
'stream' => true,
127-
// 'user_id' => Illuminate\Support\Str::uuid()->toString(),
128125
],
129126
],
130127
'ernie_bot_turbo' => [
131-
'driver' => 'ernie',
128+
'driver' => 'ernie_bot_turbo',
132129
'http_options' => [],
133130
'api_key' => env('ERNIE_API_KEY', '...'),
134131
'secret_key' => env('ERNIE_SECRET_KEY', '...'),
@@ -138,7 +135,6 @@
138135
'top_p' => 0.8,
139136
'penalty_score' => 1.0,
140137
'stream' => true,
141-
// 'user_id' => Illuminate\Support\Str::uuid()->toString(),
142138
],
143139
],
144140
],

0 commit comments

Comments
 (0)