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 c0d49e9
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions question.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,19 @@ 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 with a single field named transation';
$llmresponse = $ai->prompt_completion($prompt);
$translation = $llmresponse['response']['choices'][0]['message']['content'];
$translation = trim($translation, '"');
$translationobject = json_decode($translation);
$translation = trim($translationobject->translation, '"');
$cache->set(current_language().'_'.$text, $translation);
}
return $translation;
Expand Down

0 comments on commit c0d49e9

Please sign in to comment.