lsc 7 ماه پیش
والد
کامیت
8de72b9bd6
2فایلهای تغییر یافته به همراه72 افزوده شده و 0 حذف شده
  1. 2 0
      package.json
  2. 70 0
      pbl.js

+ 2 - 0
package.json

@@ -28,6 +28,7 @@
       "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
       "version": "1.1.1"
     },
+    "axios": "^1.7.7",
     "bcryptjs": "^2.4.3",
     "bignumber.js": {
       "integrity": "sha512-LDXpJKVzEx2/OqNbG9mXBNvHuiRL4PzHCGfnANHMJ+fv68Ads3exDVJeGDJws+AoNEuca93bU3q+S0woeUaCdg==",
@@ -291,6 +292,7 @@
       "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz",
       "version": "0.1.7"
     },
+    "pg": "^8.13.0",
     "process-nextick-args": {
       "integrity": "sha1-FQ4gt1ZZCtP5EJPyWk8q2L/zC6M=",
       "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-1.0.7.tgz",

+ 70 - 0
pbl.js

@@ -19,6 +19,76 @@ const _getmysqlLabor = ["172.16.12.7", "pbl"]; //edu數據庫信息
 const _getmysqluser = ["172.16.12.7", "cocorobouser"]; //edu數據庫信息
 var crypto = require("crypto");
 var https = require("https");
+const { Pool } = require('pg');
+const axios = require('axios');
+
+const pool = new Pool({
+    user: 'cocorobo',
+    host: '34.228.204.21',
+    database: 'cocorobo-postgres',
+    password: 'cocorobo',
+    port: 5433,
+    max: 20000, // 设置最大连接数
+    idleTimeoutMillis: 30000 // 设置空闲连接超时时间
+});
+
+async function calculateVectors(content) {
+    try {
+        const response = await axios.post('http://34.228.204.21:11434/api/embeddings', {
+            "model": "smartcreation/bge-large-zh-v1.5",
+            "prompt": `${content}`
+        });
+        const contentVector = response.data.embedding;
+        return contentVector;
+
+    } catch (error) {
+        console.error('Error calculating vectors:', content);
+        return null;
+    }
+}
+
+async function findMatchingContents(content) {
+    const contentVector = await calculateVectors(content);
+    const formattedContentVector = contentVector.length > 0 ? `[` + contentVector.join(', ') + `]` : null;
+
+    const client = await pool.connect();
+    try {
+        // 使用余弦相似度进行内容向量匹配
+        const query = `
+            SELECT title, id, content,
+                   1 - (content_vector <#> $1::vector) AS contentSimilarity
+            FROM knowledgefiles
+            ORDER BY contentSimilarity DESC
+            LIMIT 5;
+        `;
+
+        const result = await client.query(query, [formattedContentVector]);
+
+        /*
+        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(); // 确保释放数据库连接
+    }
+}
+
+/*检索文件*/
+router.route("/findMatchingContents").all(async (req, res, next) => {
+    var json = queryString(req.url);
+    const titleMatches = await findMatchingContents(json["string"]);
+    res.end(JSON.stringify(titleMatches));
+});
 
 //統壹處理區域
 router.use(async function(req, res, next) {