From 3c60274dc7579e4dee26f2de51764ce0d818747d Mon Sep 17 00:00:00 2001 From: "Peter Droogmans (attiks)" Date: Wed, 24 Jul 2024 14:20:39 +0200 Subject: [PATCH] Calculate embedding --- .../src/Services/OchaAiTagTagger.php | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/modules/ocha_ai_tag/src/Services/OchaAiTagTagger.php b/modules/ocha_ai_tag/src/Services/OchaAiTagTagger.php index 4f2824c..9e5c480 100644 --- a/modules/ocha_ai_tag/src/Services/OchaAiTagTagger.php +++ b/modules/ocha_ai_tag/src/Services/OchaAiTagTagger.php @@ -299,6 +299,10 @@ protected function getSimilarTerms(array $embeddings, bool $average_embeddings = // Sort by similarity score descending. foreach ($types as $type) { + if (!isset($results[$type])) { + continue; + } + foreach ($results[$type] as $vocabulary => $terms) { if (!empty($terms)) { arsort($results[$type][$vocabulary]); @@ -431,23 +435,19 @@ public function embedDocument(int $id): bool { */ public function getSimilarDocuments($id, string $text = '') { $query_embedding = []; - $doc = $this->getVectorStorePlugin()->getDocument('vector_jobs', $id); - if (empty($doc)) { - // Doc doesn't exist in vector store. - $data = $this->getSourcePlugin()->getDocument('jobs', $id); - if (empty($data)) { - $query_embedding = $this->getEmbeddings($text); - } - else { - $job = reset($data['jobs']); - $job = $this->processDocument($job); - $query_embedding = $job['embedding'] ?? []; - } + // Will fail for unpublished documents. + $doc = NULL; + try { + $doc = $this->getVectorStorePlugin()->getDocument('vector_jobs', $id); } - else { - $doc = $doc['_source']; - $query_embedding = $doc['embedding'] ?? []; + catch (\Throwable $e) { + $doc = NULL; + } + + if (empty($doc)) { + $query_embedding = $this->getEmbeddings($text); + $query_embedding = reset($query_embedding); } if (!is_array($query_embedding) || empty($query_embedding)) {