Skip to content

Commit 41ceadd

Browse files
committed
feat(generators): add ErnieBotTurbo generator
1 parent 2b7085a commit 41ceadd

File tree

4 files changed

+78
-14
lines changed

4 files changed

+78
-14
lines changed

app/Generators/ErnieBotGenerator.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,14 @@ public function __construct(array $config)
4747
*/
4848
public function generate(string $prompt): string
4949
{
50-
$prompt = 'PHP是什么?';
51-
5250
$parameters = [
5351
'messages' => [
5452
['role' => 'user', 'content' => $prompt],
5553
],
56-
'user' => Str::uuid()->toString(),
54+
'user_id' => Str::uuid()->toString(),
5755
] + Arr::get($this->config, 'parameters', []);
5856

59-
$response = $this->ernie->ernieBot($parameters, $this->buildWriter($messages));
57+
[$messages, $response] = $this->getResponse($parameters);
6058

6159
// fake 响应
6260
return (string) ($messages ?? $this->getCompletionMessages($response));
@@ -76,15 +74,17 @@ protected function getCompletionMessages($response): string
7674
protected function buildWriter(?string &$messages): \Closure
7775
{
7876
return function (string $data) use (&$messages): void {
79-
// 流响应完成
80-
if (str($data)->startsWith('data: [DONE]')) {
81-
return;
82-
}
83-
8477
// (正常|错误|流)响应
8578
$rowResponse = (array) json_decode(Ernie::sanitizeData($data), true);
8679
$messages .= $text = $this->getCompletionMessages($rowResponse);
8780
$this->outputStyle->write($text);
8881
};
8982
}
83+
84+
protected function getResponse(array $parameters): array
85+
{
86+
$response = $this->ernie->ernieBot($parameters, $this->buildWriter($messages));
87+
88+
return [$messages, $response];
89+
}
9090
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of the guanguans/ai-commit.
7+
*
8+
* (c) guanguans <ityaozm@gmail.com>
9+
*
10+
* This source file is subject to the MIT license that is bundled.
11+
*/
12+
13+
namespace App\Generators;
14+
15+
class ErnieBotTurboGenerator extends ErnieBotGenerator
16+
{
17+
protected function getResponse(array $parameters): array
18+
{
19+
$response = $this->ernie->ernieBotTurbo($parameters, $this->buildWriter($messages));
20+
21+
return [$messages, $response];
22+
}
23+
}

app/Support/Ernie.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,38 @@ static function (Stringable $data) use ($prefix): Stringable {
4040
}
4141

4242
/**
43-
* @throws RequestException
43+
* ```php
44+
* [
45+
* 'id' => 'chatcmpl-6pqDoRwRGQAlRvJnesR9QMG9rxpyK',
46+
* 'object' => 'chat.completion',
47+
* 'created' => 1677813488,
48+
* 'model' => 'gpt-3.5-turbo-0301',
49+
* 'usage' => [
50+
* 'prompt_tokens' => 8,
51+
* 'completion_tokens' => 16,
52+
* 'total_tokens' => 24,
53+
* ],
54+
* 'choices' => [
55+
* [
56+
* 'message' => [
57+
* 'role' => 'assistant',
58+
* 'content' => 'PHP (Hypertext Preprocessor) is a server-side scripting language used',
59+
* ],
60+
* 'finish_reason' => 'length',
61+
* 'index' => 0,
62+
* ],
63+
* ],
64+
* ];
65+
* ```.
66+
*
67+
* ```stream
68+
* 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}}
69+
*
70+
* 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}}
71+
* ```
72+
*
4473
* @throws BindingResolutionException
74+
* @throws RequestException
4575
*/
4676
public function ernieBot(array $parameters, ?callable $writer = null): Response
4777
{
@@ -155,10 +185,7 @@ static function (PendingRequest $pendingRequest) use (&$rowData, $writer): Pendi
155185
return $pendingRequest->withOptions([
156186
'curl' => [
157187
CURLOPT_WRITEFUNCTION => static function ($ch, string $data) use (&$rowData, $writer): int {
158-
if (! str($data)->startsWith('data: [DONE]')) {
159-
$rowData = $data;
160-
}
161-
188+
$rowData = $data;
162189
$writer($data, $ch);
163190

164191
return \strlen($data);

config/ai-commit.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@
127127
// 'user_id' => Illuminate\Support\Str::uuid()->toString(),
128128
],
129129
],
130+
'ernie_bot_turbo' => [
131+
'driver' => 'ernie',
132+
'http_options' => [],
133+
'api_key' => env('ERNIE_API_KEY', '...'),
134+
'secret_key' => env('ERNIE_SECRET_KEY', '...'),
135+
'parameters' => [
136+
// 'messages' => 'required|array',
137+
'temperature' => 0.95,
138+
'top_p' => 0.8,
139+
'penalty_score' => 1.0,
140+
'stream' => true,
141+
// 'user_id' => Illuminate\Support\Str::uuid()->toString(),
142+
],
143+
],
130144
],
131145

132146
/**

0 commit comments

Comments
 (0)