Skip to content

Commit 7f5fea4

Browse files
author
ityaozm@gmail.com
committed
Update(Generators/MoonshotGenerator): Remove unused code and fix getCompletionMessages method
- Remove unused code in MoonshotGenerator. - Fix getCompletionMessages method to return correct content based on the config parameter 'stream'.
1 parent 77d8fd1 commit 7f5fea4

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

app/Generators/MoonshotGenerator.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,14 @@ public function __construct(array $config)
4545

4646
/**
4747
* @psalm-suppress RedundantCast
48-
*
49-
* @noinspection MissingParentCallInspection
5048
*/
5149
public function generate(string $prompt): string
5250
{
5351
$parameters = [
5452
'messages' => [
5553
['role' => 'user', 'content' => $prompt],
5654
],
57-
'user' => Str::uuid()->toString(),
55+
// 'user' => Str::uuid()->toString(),
5856
] + Arr::get($this->config, 'parameters', []);
5957

6058
$response = $this->moonshot->chatCompletions($parameters, $this->buildWriter($messages));
@@ -63,12 +61,11 @@ public function generate(string $prompt): string
6361
return (string) ($messages ?? $this->getCompletionMessages($response));
6462
}
6563

66-
/**
67-
* {@inheritDoc}
68-
*/
6964
private function getCompletionMessages($response): string
7065
{
71-
return Arr::get($response, 'choices.0.delta.content', '');
66+
return Arr::get($this->config, 'parameters.stream', false)
67+
? Arr::get($response, 'choices.0.delta.content', '')
68+
: Arr::get($response, 'choices.0.message.content', '');
7269
}
7370

7471
/**

tests/Pest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function setup_http_fake(): void
113113
json
114114

115115
: <<<'json'
116-
{"id":"cmpl-64d18ab895224e74b9c78a9a8c233585","object":"chat.completion","created":3943160,"model":"moonshot-v1-8k","choices":[{"index":0,"delta":{"role":"assistant","content":"你好,李雷!1+1等于2。如果你有更复杂的数学问题或者其他问题,也可以随时问我。"},"finish_reason":"stop"}],"usage":{"prompt_tokens":83,"completion_tokens":25,"total_tokens":108}}
116+
{"id":"cmpl-64d18ab895224e74b9c78a9a8c233585","object":"chat.completion","created":3943160,"model":"moonshot-v1-8k","choices":[{"index":0,"message":{"role":"assistant","content":"你好,李雷!1+1等于2。如果你有更复杂的数学问题或者其他问题,也可以随时问我。"},"finish_reason":"stop"}],"usage":{"prompt_tokens":83,"completion_tokens":25,"total_tokens":108}}
117117
json;
118118

119119
return Http::response($body, $status);

0 commit comments

Comments
 (0)