|
@@ -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) {
|