Skip to content

Commit c9ecd9c

Browse files
committed
Translation caching is done by chunk
Signed-off-by: Lukas Schaefer <lukas@lschaefer.xyz>
1 parent 6b42432 commit c9ecd9c

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

lib/TaskProcessing/TranslateProvider.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,6 @@ public function process(?string $userId, array $input, callable $reportProgress)
153153
$maxTokens = $input['max_tokens'];
154154
}
155155

156-
$cacheKey = ($input['origin_language'] ?? '') . '/' . $input['target_language'] . '/' . md5($inputText);
157-
158-
$cache = $this->cacheFactory->createDistributed('integration_openai');
159-
if ($cached = $cache->get($cacheKey)) {
160-
return ['output' => $cached];
161-
}
162156
$chunks = $this->chunkService->chunkSplitPrompt($inputText, true, $maxTokens);
163157
$result = '';
164158
$increase = 1.0 / (float)count($chunks);
@@ -176,6 +170,16 @@ public function process(?string $userId, array $input, callable $reportProgress)
176170
}
177171

178172
foreach ($chunks as $chunk) {
173+
$progress += $increase;
174+
$cacheKey = ($input['origin_language'] ?? '') . '/' . $input['target_language'] . '/' . md5($chunk);
175+
176+
$cache = $this->cacheFactory->createDistributed('integration_openai');
177+
if ($cached = $cache->get($cacheKey)) {
178+
$this->logger->debug('Using cached translation', ['cached' => $cached, 'cacheKey' => $cacheKey]);
179+
$result .= $cached;
180+
$reportProgress($progress);
181+
continue;
182+
}
179183
$prompt = $promptStart . $chunk;
180184

181185
if ($this->openAiAPIService->isUsingOpenAi() || $this->openAiSettingsService->getChatEndpointEnabled()) {
@@ -185,11 +189,12 @@ public function process(?string $userId, array $input, callable $reportProgress)
185189
$completion = $this->openAiAPIService->createCompletion($userId, $prompt, 1, $model, $maxTokens);
186190
}
187191

188-
$progress += $increase;
189192
$reportProgress($progress);
190193

191194
if (count($completion) > 0) {
192-
$result .= array_pop($completion);
195+
$completion = array_pop($completion);
196+
$result .= $completion;
197+
$cache->set($cacheKey, $completion);
193198
continue;
194199
}
195200

0 commit comments

Comments
 (0)