lsc 5 tháng trước cách đây
mục cha
commit
8072794d8d
1 tập tin đã thay đổi với 31 bổ sung31 xóa
  1. 31 31
      pbl.js

+ 31 - 31
pbl.js

@@ -48,37 +48,37 @@ async function calculateVectors(content) {
 }
 
 async function findMatchingContents(discipline, content) {
-    const contentVector = await calculateVectors(content);
-    const formattedContentVector = contentVector.length > 0 ? `[` + contentVector.join(', ') + `]` : null;
-
-    const client = await pool.connect();
-    try {
-        // 使用余弦相似度进行内容向量匹配
-        const query = `
-            SELECT id, content,
-                      1 - (content_vector <#> $1::vector) AS contentSimilarity
-            FROM (select id, content, content_vector from knowledgefiles as a where top_dir_id = (select id from directory_files where folder_name = $2 limit 1)) as a
-            ORDER BY contentSimilarity DESC
-            LIMIT 5;
-        `;
-
-        const result = await client.query(query, [formattedContentVector, discipline]);
-        /*
-                    console.log(result.rows);
-                    // 将查询结果转换为匹配对象
-                    const matches = result.rows.map(row => ({
-                        id: row.id,
-                        contentSimilarity: row.contentsimilarity
-                    }));
-                    console.log(matches);
-                    */
-        return result.rows; // 返回匹配结果
-    } catch (error) {
-        console.error('Error finding matching contents:', error);
-        return [];
-    } finally {
-        client.release(); // 确保释放数据库连接
-    }
+    const contentVector = await calculateVectors(content);
+    const formattedContentVector = contentVector.length > 0 ? `[` + contentVector.join(', ') + `]` : null;
+
+    const client = await pool.connect();
+    try {
+        // 使用余弦相似度进行内容向量匹配
+        const query = `
+            SELECT id, content,
+                   1 - (content_vector <#> $1::vector) AS contentSimilarity
+            FROM (select id, content, content_vector from knowledgefiles as a where top_dir_id in (select id from directory_files where folder_name = ANY(string_to_array($2, ',')))) as a
+            ORDER BY contentSimilarity DESC
+            LIMIT 5;
+        `;
+
+        const result = await client.query(query, [formattedContentVector, discipline]);
+        /*
+                console.log(result.rows);
+                // 将查询结果转换为匹配对象
+                const matches = result.rows.map(row => ({
+                    id: row.id,
+                    contentSimilarity: row.contentsimilarity
+                }));
+                console.log(matches);
+                */
+        return result.rows; // 返回匹配结果
+    } catch (error) {
+        console.error('Error finding matching contents:', error);
+        return [];
+    } finally {
+        client.release(); // 确保释放数据库连接
+    }
 }
 
 /*检索文件*/