Skip to content

Commit

Permalink
feat(proofread): decouple the system prompt for the chat endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Julien Veyssier <[email protected]>
  • Loading branch information
julien-nc committed Jan 7, 2025
1 parent cc8e936 commit d554856
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/TaskProcessing/ProofreadProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
throw new RuntimeException('Invalid prompt');
}
$textInput = $input['input'];
$prompt = 'Proofread the following text. List all spelling and grammar mistakes and how to correct them. Output only the list. Here is the text:' . "\n\n" . $textInput;
$systemPrompt = 'Proofread the following text. List all spelling and grammar mistakes and how to correct them. Output only the list.';

$maxTokens = null;
if (isset($input['max_tokens']) && is_int($input['max_tokens'])) {
Expand All @@ -116,9 +116,10 @@ public function process(?string $userId, array $input, callable $reportProgress)

try {
if ($this->openAiAPIService->isUsingOpenAi() || $this->openAiSettingsService->getChatEndpointEnabled()) {
$completion = $this->openAiAPIService->createChatCompletion($userId, $model, $prompt, null, null, 1, $maxTokens);
$completion = $this->openAiAPIService->createChatCompletion($userId, $model, $textInput, $systemPrompt, null, 1, $maxTokens);
$completion = $completion['messages'];
} else {
$prompt = $systemPrompt . ' Here is the text:' . "\n\n" . $textInput;
$completion = $this->openAiAPIService->createCompletion($userId, $prompt, 1, $model, $maxTokens);
}
} catch (Exception $e) {
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/Providers/OpenAiProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,14 @@ public function testProofreadProvider(): void {
$url = self::OPENAI_API_BASE . 'chat/completions';

$options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']];
$message = 'Proofread the following text. List all spelling and grammar mistakes and how to correct them. Output only the list. Here is the text:' . "\n\n" . $prompt;
$options['body'] = json_encode(['model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [['role' => 'user', 'content' => $message]], 'max_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS, 'n' => $n, 'user' => self::TEST_USER1]);
$systemPrompt = 'Proofread the following text. List all spelling and grammar mistakes and how to correct them. Output only the list.';
$options['body'] = json_encode([
'model' => Application::DEFAULT_COMPLETION_MODEL_ID,
'messages' => [['role' => 'system', 'content' => $systemPrompt],['role' => 'user', 'content' => $prompt]],
'max_tokens' => Application::DEFAULT_MAX_NUM_OF_TOKENS,
'n' => $n,
'user' => self::TEST_USER1,
]);

$iResponse = $this->createMock(\OCP\Http\Client\IResponse::class);
$iResponse->method('getBody')->willReturn($response);
Expand Down

0 comments on commit d554856

Please sign in to comment.