Skip to content

Commit

Permalink
Fix to the translation to return json against global json format later
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusgreen committed Jun 6, 2024
1 parent 316ff9a commit 9aae1c5
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,18 @@ public function process_feedback(string $feedback) {
* @param string $text
* @return string
*/
protected function llm_translate(string $text) :string {
protected function llm_translate(string $text): string {
if (current_language() == 'en') {
return $text;
}
$ai = new ai\ai();
$cache = cache::make('qtype_aitext', 'stringdata');
if (($translation = $cache->get(current_language().'_'.$text)) === false) {
$prompt = 'translate "'.$text .'" into '.current_language();
$prompt = 'translate "'.$text .'" into '.current_language(). ' respond with json object named transltion with a single field named translated';
$llmresponse = $ai->prompt_completion($prompt);
$translation = $llmresponse['response']['choices'][0]['message']['content'];
$translation = trim($translation, '"');
$translationobject = json_decode($translation);
$translation = trim($translationobject->translated, '"');
$cache->set(current_language().'_'.$text, $translation);
}
return $translation;
Expand Down

0 comments on commit 9aae1c5

Please sign in to comment.