Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions lib/TaskProcessing/AnalyzeImagesProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ public function getOutputShapeEnumValues(): array {
}

public function getOptionalOutputShape(): array {
return [];
return [
'reasoning' => new ShapeDescriptor(
$this->l->t('Reasoning content'),
$this->l->t('The model reasoning behind the output'),
EShapeType::Text,
),
];
}

public function getOptionalOutputShapeEnumValues(): array {
Expand Down Expand Up @@ -198,29 +204,46 @@ public function process(
if ($preferStreaming) {
$chunks = $this->openAiAPIService->createStreamedChatCompletion($userId, $model, $prompt, $systemPrompt, $history, 1, $maxTokens);
$time = microtime(true);
$fullOutput = '';
$streamedOutput = '';
$streamedReasoning = '';
foreach ($chunks as $chunk) {
if (($chunk['kind'] ?? null) !== 'content') {
if (!in_array($chunk['kind'] ?? null, ['content', 'reasoning_content'], true)) {
continue;
}
$fullOutput .= $chunk['text'];
if ($chunk['kind'] === 'reasoning_content') {
$streamedReasoning .= $chunk['text'];
} elseif ($chunk['kind'] === 'content') {
$streamedOutput .= $chunk['text'];
}
// we don't report more often than every 250ms
if (microtime(true) - $time >= 0.25) {
$reportOutput(['output' => $fullOutput]);
$reportOutput([
'output' => $streamedOutput,
'reasoning' => $streamedReasoning,
]);
$time = microtime(true);
}
}
if ($fullOutput !== '') {
$reportOutput(['output' => $fullOutput]);
if ($streamedOutput !== '' || $streamedReasoning !== '') {
$reportOutput([
'output' => $streamedOutput,
'reasoning' => $streamedReasoning,
]);
}
$completion = $chunks->getReturn()['messages'];
$returnValue = $chunks->getReturn();
$completion = $returnValue['messages'];
$reasoning = $returnValue['reasoning_messages'];
} else {
$completion = $this->openAiAPIService->createChatCompletion($userId, $model, $prompt, $systemPrompt, $history, 1, $maxTokens);
$completion = $completion['messages'];
$returnValue = $this->openAiAPIService->createChatCompletion($userId, $model, $prompt, $systemPrompt, $history, 1, $maxTokens);
$completion = $returnValue['messages'];
$reasoning = $returnValue['reasoning_messages'];
}

if (count($completion) > 0) {
return ['output' => array_pop($completion)];
return [
'output' => array_pop($completion),
'reasoning' => count($reasoning) > 0 ? array_pop($reasoning) : '',
];
}

throw new ProcessingException('No result in OpenAI/LocalAI response.');
Expand Down
54 changes: 41 additions & 13 deletions lib/TaskProcessing/ChangeToneProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,13 @@ public function getOutputShapeEnumValues(): array {
}

public function getOptionalOutputShape(): array {
return [];
return [
'reasoning' => new ShapeDescriptor(
$this->l->t('Reasoning content'),
$this->l->t('The model reasoning behind the output'),
EShapeType::Text,
),
];
}

public function getOptionalOutputShapeEnumValues(): array {
Expand Down Expand Up @@ -136,8 +142,10 @@ public function process(
}

$chunks = $this->chunkService->chunkSplitPrompt($textInput, true, $maxTokens);
$streamedResult = '';
$result = '';
$streamedOutput = '';
$streamedReasoning = '';
$fullOutput = '';
$fullReasoning = '';
$increase = 1.0 / (float)count($chunks);
$progress = 0.0;
foreach ($chunks as $textInput) {
Expand All @@ -148,26 +156,40 @@ public function process(
$chunks = $this->openAiAPIService->createStreamedChatCompletion($userId, $model, $prompt, null, null, 1, $maxTokens);
$time = microtime(true);
foreach ($chunks as $chunk) {
if (($chunk['kind'] ?? null) !== 'content') {
if (!in_array($chunk['kind'] ?? null, ['content', 'reasoning_content'], true)) {
continue;
}
$streamedResult .= $chunk['text'];
if ($chunk['kind'] === 'reasoning_content') {
$streamedReasoning .= $chunk['text'];
} elseif ($chunk['kind'] === 'content') {
$streamedOutput .= $chunk['text'];
}
// we don't report more often than every 250ms
if (microtime(true) - $time >= 0.25) {
$reportOutput(['output' => $streamedResult]);
$reportOutput([
'output' => $streamedOutput,
'reasoning' => $streamedReasoning,
]);
$time = microtime(true);
}
}
if ($streamedResult !== '') {
$reportOutput(['output' => $streamedResult]);
if ($streamedOutput !== '' || $streamedReasoning !== '') {
$reportOutput([
'output' => $streamedOutput,
'reasoning' => $streamedReasoning,
]);
}
$completion = $chunks->getReturn()['messages'];
$returnValue = $chunks->getReturn();
$completion = $returnValue['messages'];
$reasoning = $returnValue['reasoning_messages'];
} else {
$completion = $this->openAiAPIService->createChatCompletion($userId, $model, $prompt, null, null, 1, $maxTokens);
$completion = $completion['messages'];
$returnValue = $this->openAiAPIService->createChatCompletion($userId, $model, $prompt, null, null, 1, $maxTokens);
$completion = $returnValue['messages'];
$reasoning = $returnValue['reasoning_messages'];
}
} else {
$completion = $this->openAiAPIService->createCompletion($userId, $prompt, 1, $model, $maxTokens);
$reasoning = [];
}
} catch (UserFacingProcessingException $e) {
throw $e;
Expand All @@ -176,15 +198,21 @@ public function process(
}
$progress += $increase;
$reportProgress($progress);
if (count($reasoning) > 0) {
$fullReasoning .= array_pop($reasoning);
}
if (count($completion) > 0) {
$result .= array_pop($completion);
$fullOutput .= array_pop($completion);
continue;
}

throw new ProcessingException('No result in OpenAI/LocalAI response.');
}
$endTime = time();
$this->openAiAPIService->updateExpTextProcessingTime($endTime - $startTime);
return ['output' => $result];
return [
'output' => $fullOutput,
'reasoning' => $fullReasoning,
];
}
}
55 changes: 42 additions & 13 deletions lib/TaskProcessing/ContextWriteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ public function getOutputShapeEnumValues(): array {
}

public function getOptionalOutputShape(): array {
return [];
return [
'reasoning' => new ShapeDescriptor(
$this->l->t('Reasoning content'),
$this->l->t('The model reasoning behind the output'),
EShapeType::Text,
),
];
}

public function getOptionalOutputShapeEnumValues(): array {
Expand Down Expand Up @@ -128,10 +134,13 @@ public function process(
}

$chunks = $this->chunkService->chunkSplitPrompt($sourceMaterial, true, $maxTokens);
$result = '';
$fullOutput = '';
$fullReasoning = '';

$increase = 1.0 / (float)count($chunks);
$progress = 0.0;
$fullOutput = '';
$streamedOutput = '';
$streamedReasoning = '';

foreach ($chunks as $sourceMaterial) {
$prompt = 'You\'re a professional copywriter tasked with copying an instructed or demonstrated *WRITING STYLE*'
Expand All @@ -148,34 +157,51 @@ public function process(
$chunks = $this->openAiAPIService->createStreamedChatCompletion($userId, $model, $prompt, null, null, 1, $maxTokens);
$time = microtime(true);
foreach ($chunks as $chunk) {
if (($chunk['kind'] ?? null) !== 'content') {
if (!in_array($chunk['kind'] ?? null, ['content', 'reasoning_content'], true)) {
continue;
}
$fullOutput .= $chunk['text'];
if ($chunk['kind'] === 'reasoning_content') {
$streamedReasoning .= $chunk['text'];
} elseif ($chunk['kind'] === 'content') {
$streamedOutput .= $chunk['text'];
}
// we don't report more often than every 250ms
if (microtime(true) - $time >= 0.25) {
$reportOutput(['output' => $fullOutput]);
$reportOutput([
'output' => $streamedOutput,
'reasoning' => $streamedReasoning,
]);
$time = microtime(true);
}
}
if ($fullOutput !== '') {
$reportOutput(['output' => $fullOutput]);
if ($streamedOutput !== '' || $streamedReasoning !== '') {
$reportOutput([
'output' => $streamedOutput,
'reasoning' => $streamedReasoning,
]);
}
$completion = $chunks->getReturn()['messages'];
$returnValue = $chunks->getReturn();
$completion = $returnValue['messages'];
$reasoning = $returnValue['reasoning_messages'];
} else {
$completion = $this->openAiAPIService->createChatCompletion($userId, $model, $prompt, null, null, 1, $maxTokens);
$completion = $completion['messages'];
$returnValue = $this->openAiAPIService->createChatCompletion($userId, $model, $prompt, null, null, 1, $maxTokens);
$completion = $returnValue['messages'];
$reasoning = $returnValue['reasoning_messages'];
}
} else {
$completion = $this->openAiAPIService->createCompletion($userId, $prompt, 1, $model, $maxTokens);
$reasoning = [];
}
} catch (UserFacingProcessingException $e) {
throw $e;
} catch (\Throwable $e) {
throw new ProcessingException('OpenAI/LocalAI request failed: ' . $e->getMessage());
}
if (count($reasoning) > 0) {
$fullReasoning .= array_pop($reasoning);
}
if (count($completion) > 0) {
$result .= array_pop($completion);
$fullOutput .= array_pop($completion);
$progress += $increase;
$reportProgress($progress);
continue;
Expand All @@ -185,6 +211,9 @@ public function process(
}
$endTime = time();
$this->openAiAPIService->updateExpTextProcessingTime($endTime - $startTime);
return ['output' => $result];
return [
'output' => $fullOutput,
'reasoning' => $fullReasoning,
];
}
}
54 changes: 41 additions & 13 deletions lib/TaskProcessing/ReformulateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ public function getOutputShapeEnumValues(): array {
}

public function getOptionalOutputShape(): array {
return [];
return [
'reasoning' => new ShapeDescriptor(
$this->l->t('Reasoning content'),
$this->l->t('The model reasoning behind the output'),
EShapeType::Text,
),
];
}

public function getOptionalOutputShapeEnumValues(): array {
Expand Down Expand Up @@ -122,10 +128,12 @@ public function process(
$model = $this->openAiSettingsService->getAdminDefaultCompletionModelId();
}
$chunks = $this->chunkService->chunkSplitPrompt($prompt, true, $maxTokens);
$result = '';
$fullOutput = '';
$fullReasoning = '';
$increase = 1.0 / (float)count($chunks);
$progress = 0.0;
$fullOutput = '';
$streamedOutput = '';
$streamedReasoning = '';

foreach ($chunks as $chunk) {
$prompt = 'Reformulate the following text. Use the same language as the original text. Output only the reformulation. Here is the text:' . "\n\n" . $chunk . "\n\n" . 'Do not mention the used language in your reformulation. Here is your reformulation in the same language:';
Expand All @@ -135,34 +143,51 @@ public function process(
$chunks = $this->openAiAPIService->createStreamedChatCompletion($userId, $model, $prompt, null, null, 1, $maxTokens);
$time = microtime(true);
foreach ($chunks as $chunk) {
if (($chunk['kind'] ?? null) !== 'content') {
if (!in_array($chunk['kind'] ?? null, ['content', 'reasoning_content'], true)) {
continue;
}
$fullOutput .= $chunk['text'];
if ($chunk['kind'] === 'reasoning_content') {
$streamedReasoning .= $chunk['text'];
} elseif ($chunk['kind'] === 'content') {
$streamedOutput .= $chunk['text'];
}
// we don't report more often than every 250ms
if (microtime(true) - $time >= 0.25) {
$reportOutput(['output' => $fullOutput]);
$reportOutput([
'output' => $streamedOutput,
'reasoning' => $streamedReasoning,
]);
$time = microtime(true);
}
}
if ($fullOutput !== '') {
$reportOutput(['output' => $fullOutput]);
if ($streamedOutput !== '' || $streamedReasoning !== '') {
$reportOutput([
'output' => $streamedOutput,
'reasoning' => $streamedReasoning,
]);
}
$completion = $chunks->getReturn()['messages'];
$returnValue = $chunks->getReturn();
$completion = $returnValue['messages'];
$reasoning = $returnValue['reasoning_messages'];
} else {
$completion = $this->openAiAPIService->createChatCompletion($userId, $model, $prompt, null, null, 1, $maxTokens);
$completion = $completion['messages'];
$returnValue = $this->openAiAPIService->createChatCompletion($userId, $model, $prompt, null, null, 1, $maxTokens);
$completion = $returnValue['messages'];
$reasoning = $returnValue['reasoning_messages'];
}
} else {
$completion = $this->openAiAPIService->createCompletion($userId, $prompt, 1, $model, $maxTokens);
$reasoning = [];
}
} catch (UserFacingProcessingException $e) {
throw $e;
} catch (\Throwable $e) {
throw new ProcessingException('OpenAI/LocalAI request failed: ' . $e->getMessage());
}
if (count($reasoning) > 0) {
$fullReasoning .= array_pop($reasoning);
}
if (count($completion) > 0) {
$result .= array_pop($completion);
$fullOutput .= array_pop($completion);
$progress += $increase;
$reportProgress($progress);
continue;
Expand All @@ -173,6 +198,9 @@ public function process(

$endTime = time();
$this->openAiAPIService->updateExpTextProcessingTime($endTime - $startTime);
return ['output' => $result];
return [
'output' => $fullOutput,
'reasoning' => $fullReasoning,
];
}
}
Loading
Loading