Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/FOUR-12952: Update process translation project to use another model instead of deprecated text-davinci-003 #5845

Merged
merged 5 commits into from
Dec 21, 2023
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
26 changes: 19 additions & 7 deletions ProcessMaker/Ai/Handlers/LanguageTranslationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public function __construct()
{
parent::__construct();
$this->config = [
'model' => 'text-davinci-003',
'max_tokens' => 2200,
'model' => 'gpt-3.5-turbo-16k',
'max_tokens' => 6000,
'temperature' => 0,
'top_p' => 1,
'n' => 1,
Expand Down Expand Up @@ -52,6 +52,10 @@ public function generatePrompt(String $type = null, String $json_list) : Object
$prompt = $this->replaceLanguage($prompt, $this->targetLanguage['humanLanguage']);
$prompt = $this->replaceStopSequence($prompt);
$this->config['prompt'] = $prompt;
$this->config['messages'] = [[
'role' => 'user',
'content' => $prompt,
]];

return $this;
}
Expand All @@ -61,17 +65,25 @@ public function execute()
$listCharCount = strlen($this->json_list);
$totalChars = $listCharCount * 3;
$currentChunkCount = 0;
$config = $this->getConfig();
unset($config['prompt']);

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

$fullResponse = '';
foreach ($stream as $response) {
$currentChunkCount += strlen($response->choices[0]->text);
self::sendResponse($response->choices[0]->text, $currentChunkCount, $totalChars);
$fullResponse .= $response->choices[0]->text;
if (array_key_exists('content', $response->choices[0]->toArray()['delta'])) {
$currentChunkCount += strlen($response->choices[0]->toArray()['delta']['content']);
self::sendResponse(
$response->choices[0]->toArray()['delta']['content'],
$currentChunkCount,
$totalChars
);
$fullResponse .= $response->choices[0]->toArray()['delta']['content'];
}
}

return $this->formatResponse($fullResponse);
Expand Down
16 changes: 11 additions & 5 deletions ProcessMaker/Ai/Handlers/NlqToCategoryHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __construct()
{
parent::__construct();
$this->config = [
'model' => 'text-davinci-003',
'model' => 'gpt-3.5-turbo',
'max_tokens' => 20,
'temperature' => 0,
'top_p' => 1,
Expand All @@ -34,7 +34,10 @@ public function generatePrompt(String $type = null, String $question) : Object
$prompt = $this->replaceStopSequence($prompt);
$prompt = $this->replaceDefaultType($prompt, $type);

$this->config['prompt'] = $prompt;
$this->config['messages'] = [[
'role' => 'user',
'content' => $prompt,
]];

return $this;
}
Expand All @@ -43,15 +46,18 @@ public function execute()
{
$client = app(Client::class);
$response = $client
->completions()
->create(array_merge($this->getConfig()));
->chat()
->create(
array_merge($this->getConfig()
)
);

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

private function formatResponse($response)
{
$result = ltrim($response->choices[0]->text);
$result = ltrim($response->choices[0]->message->content);

return [strtolower($result), $response->usage, $this->question];
}
Expand Down
16 changes: 11 additions & 5 deletions ProcessMaker/Ai/Handlers/NlqToPmqlHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __construct()
{
parent::__construct();
$this->config = [
'model' => 'text-davinci-003',
'model' => 'gpt-3.5-turbo',
'max_tokens' => 1900,
'temperature' => 0,
'top_p' => 1,
Expand All @@ -34,7 +34,10 @@ public function generatePrompt(String $type = null, String $question) : Object
$prompt = $this->replaceStopSequence($prompt);
$prompt = $this->replaceWithCurrentYear($prompt);

$this->config['prompt'] = $prompt;
$this->config['messages'] = [[
'role' => 'user',
'content' => $prompt,
]];

return $this;
}
Expand All @@ -43,15 +46,18 @@ public function execute()
{
$client = app(Client::class);
$response = $client
->completions()
->create(array_merge($this->getConfig()));
->chat()
->create(
array_merge($this->getConfig()
)
);

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

private function formatResponse($response)
{
$result = ltrim($response->choices[0]->text);
$result = ltrim($response->choices[0]->message->content);
$result = explode('Question:', $result)[0];
$result = rtrim(rtrim(str_replace("\n", '', $result)));
$result = str_replace('\'', '', $result);
Expand Down
Loading