Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
0bce4a7
Remove unused
agustinbusso Jun 1, 2023
a358c61
Add preimport and import endpoints
agustinbusso Jun 1, 2023
3ace7e9
Add import method in controller to show view
agustinbusso Jun 1, 2023
b6a5445
Add get import data and import methods
agustinbusso Jun 1, 2023
da2336c
Add ui for import
agustinbusso Jun 1, 2023
0152acf
Fix download export file
agustinbusso Jun 1, 2023
92874b2
Add routes
agustinbusso Jun 1, 2023
689c340
Add code in endpoint to perform import
agustinbusso Jun 2, 2023
fcd55ce
Add logic to handle import
agustinbusso Jun 2, 2023
9e8e492
Fix routes in breadcrumb
agustinbusso Jun 2, 2023
2d101f2
Apply translations to web entries
agustinbusso Jun 2, 2023
c886fb8
Add screen title translations
agustinbusso Jun 2, 2023
fdd6357
Revert "Add screen title translations"
agustinbusso Jun 2, 2023
dcf8eab
Redirect after import
agustinbusso Jun 5, 2023
bb4545d
Fix error in console
agustinbusso Jun 5, 2023
3eac8aa
Fix prompt
agustinbusso Jun 5, 2023
9ad8bdb
Verify if keys exists
agustinbusso Jun 5, 2023
de4597b
Move token calculation library to a class
agustinbusso Jun 5, 2023
7b1e601
Remove gpt3-encoder library. Update openAiClient
agustinbusso Jun 5, 2023
bac2e4b
UPdate composer lock
agustinbusso Jun 5, 2023
9feb313
Stream OpenAi response
agustinbusso Jun 5, 2023
07a98e6
Add translated languages to process import/export
agustinbusso Jun 6, 2023
9aa565b
Remove log
agustinbusso Jun 7, 2023
68145ab
Add event for real time streamed responses
agustinbusso Jun 7, 2023
9677299
Add cancel translation endpoint
agustinbusso Jun 7, 2023
02bbc31
Cancel translation and emit event for streamed response
agustinbusso Jun 7, 2023
d94a3b4
Add process id parameter for event emit
agustinbusso Jun 7, 2023
a408439
Cancel translation and subscribe to streamed event
agustinbusso Jun 7, 2023
fa15aec
Change attribute name
agustinbusso Jun 7, 2023
6e963f4
Add route for cancel
agustinbusso Jun 7, 2023
99c3669
Add permissions
agustinbusso Jun 7, 2023
6d06775
Fix faling test
agustinbusso Jun 7, 2023
8d7c909
Fix failing test
agustinbusso Jun 7, 2023
b2e1ae4
Update available languages list
agustinbusso Jun 7, 2023
e4dd179
Do not execute if chunk is empty
agustinbusso Jun 8, 2023
523bd6a
Add retry empty for all process
agustinbusso Jun 8, 2023
a34eab4
Change button order
agustinbusso Jun 8, 2023
15d9a03
Fix for interstitials
agustinbusso Jun 8, 2023
91a4690
Change text
agustinbusso Jun 8, 2023
1b5ee12
Fix issue when delete job
agustinbusso Jun 8, 2023
63f5ce5
Update progress of batch
agustinbusso Jun 8, 2023
98a95aa
Fix progress and stream in front end
agustinbusso Jun 8, 2023
a88acb1
Remove to avoid flickering
agustinbusso Jun 8, 2023
ccd48df
Search screens in webentry elements
agustinbusso Jun 9, 2023
a8090fc
Improve progress to have more feedback
agustinbusso Jun 9, 2023
4dd0410
Show created and updated for pending translations
agustinbusso Jun 9, 2023
5b9abd7
Update composer to allow use created stream responses from OpenAI
agustinbusso Jun 9, 2023
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
50 changes: 43 additions & 7 deletions ProcessMaker/Ai/Handlers/LanguageTranslationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

namespace ProcessMaker\Ai\Handlers;

use Illuminate\Support\Facades\Auth;
use OpenAI\Client;
use ProcessMaker\Events\ProcessTranslationChunkEvent;

class LanguageTranslationHandler extends OpenAiHandler
{
protected $targetLanguage = 'Spanish';

protected $processId = null;

protected $json_list = '';

public function __construct()
{
parent::__construct();
Expand All @@ -28,17 +34,22 @@ public function setTargetLanguage($language)
$this->targetLanguage = $language;
}

public function setProcessId($processId)
{
$this->processId = $processId;
}

public function getPromptFile($type = null)
{
return file_get_contents($this->getPromptsPath() . 'language_translation_' . $type . '.md');
}

public function generatePrompt(String $type = null, String $json_list) : Object
{
$this->$json_list = $json_list;
$this->json_list = $json_list;
$prompt = $this->getPromptFile($type);
$prompt = $this->replaceJsonList($prompt, $json_list);
$prompt = $this->replaceLanguage($prompt, $this->targetLanguage);
$prompt = $this->replaceLanguage($prompt, $this->targetLanguage['humanLanguage']);
$prompt = $this->replaceStopSequence($prompt);
$this->config['prompt'] = $prompt;

Expand All @@ -47,21 +58,32 @@ public function generatePrompt(String $type = null, String $json_list) : Object

public function execute()
{
$listCharCount = strlen($this->json_list);
$totalChars = $listCharCount * 3;
$currentChunkCount = 0;

$client = app(Client::class);
$response = $client
$stream = $client
->completions()
->create(array_merge($this->getConfig()));
->createStreamed(array_merge($this->getConfig()));

return $this->formatResponse($response);
$fullResponse = '';
foreach ($stream as $response) {
$currentChunkCount += strlen($response->choices[0]->text);
self::sendResponse($response->choices[0]->text, $currentChunkCount, $totalChars);
$fullResponse .= $response->choices[0]->text;
}

return $this->formatResponse($fullResponse);
}

private function formatResponse($response)
{
$result = ltrim($response->choices[0]->text);
$result = ltrim($response);
$result = rtrim(rtrim(str_replace("\n", '', $result)));
$result = str_replace('\'', '', $result);

return [$result, $response->usage, $this->question];
return [$result, 0, $this->question];
}

public function replaceStopSequence($prompt)
Expand All @@ -84,4 +106,18 @@ public function replaceLanguage($prompt, $language)

return $replaced;
}

private function sendResponse($response, $currentChunkCount, $totalChars)
{
$percentage = $currentChunkCount * 100 / $totalChars;

if ($percentage > 100) {
$percentage = 100;
}

$progress = ['currentChunkCount' => $currentChunkCount, 'totalChars' => $totalChars, 'percentage' => $percentage];
if ($this->processId) {
event(new ProcessTranslationChunkEvent($this->processId, $this->targetLanguage['language'], $response, $progress));
}
}
}
6 changes: 3 additions & 3 deletions ProcessMaker/Ai/Prompts/language_translation_html.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ You are an i18n-compatible translation service. You know how to do high quality

###
Example: If I give you the following strings to translate in a json format:
["<p>Hello {{user_name}} please complete the form<\p>","<p>This is my text HTML<\p>"]
["<p>Hello {{user_name}} please complete the form</p>","<p>This is my text HTML</p>"]

It is imperative you return the original strings as KEY and add a VALUE with the translation. The original JSON does not have KEY VALUE. Do not forget to format it as KEY VALUE. For example the item "<p>Hello {{user_name}} please complete the form<\p>" are going to be {"key":"<p>Hello {{user_name}} please complete the form<\p>","value":"<p>Hola {{user_name}} por favor complete el formulario<\p>"}. And "<p>This is my text HTML<\p>" are going to be {"key":"<p>This is my text HTML<\p>","value":"<p>Este es mi texto HTML<\p>"} So the initial JSON will be converted to:
[{"key":"<p>Hello {{user_name}} please complete the form<\p>","value":"<p>Hola {{user_name}} por favor complete el formulario<\p>"},{"key":"<p>This is my text HTML<\p>","value":"<p>Este es mi texto HTML<\p>"}]
It is imperative you return the original strings as KEY and add a VALUE with the translation. The original JSON does not have KEY VALUE. Do not forget to format it as KEY VALUE. For example the item "<p>Hello {{user_name}} please complete the form</p>" are going to be {"key":"<p>Hello {{user_name}} please complete the form</p>","value":"<p>Hola {{user_name}} por favor complete el formulario</p>"}. And "<p>This is my text HTML</p>" are going to be {"key":"<p>This is my text HTML</p>","value":"<p>Este es mi texto HTML</p>"} So the initial JSON will be converted to:
[{"key":"<p>Hello {{user_name}} please complete the form</p>","value":"<p>Hola {{user_name}} por favor complete el formulario</p>"},{"key":"<p>This is my text HTML</p>","value":"<p>Este es mi texto HTML</p>"}]

The previous example was translated to spanish but from now you must translate the following list to {language} language. In any case you need to keep the original capital letters. If the capital letters for some string are like "Account open date" you need to keep the capital letters like "Account open date". Another example if the text is "Account Open date" then should be "Account Open date". Another example if the text is "Account Open Date" you need to keep the capital letters like "Account Open Date". Or for example if "ACCount opEn Date" you need to keep the capital letters like "ACCount opEn Date".

Expand Down
21 changes: 21 additions & 0 deletions ProcessMaker/Ai/TokensCalculation/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Szabolcs-Istvan Kisded

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading